A simple nearest neighbour solution in TypeScript.
Write a program which:
- reads the description of the bitmap from the standard input
- for each pixel, computes the distance to the nearest white
- writes the results to the standard output
A rectangular bitmap with only black and white pixels. Atleast ONE pixel is white. All black pixels are represented by 0
and whites by 1
.
- Size:
n * m
- Size constraints::
1<=n <=182, 1<=m<=182
- Pixel:
p(i,j)
- Distance formula:
d(p1,p2) = |i1-i1|+|j1-j2|
Since the problem does not explicitly mention how to find the nearest neighbour, this program addresses two solutions based on the example output provided.
The first solution scans within the same row and tries to find the closest white pixel, like so:
The second solution scans in all four directions and tries to find the closest white pixel, like so:
yarn dev:one
- Run the first solution in dev modeyarn dev:two
- Run the second solution in dev modeyarn build
- Build both solutionsyarn start:one < path/to/file.txt
- Run the production build using solution oneyarn start:two < path/to/file.txt
- Run the production build using solution twoyarn lint
- Lint all files and check for errorsyarn test
- Run all test suitesyarn test:generate
- Generate a random new input file in thegenerated
folder
All the various input files are located tests/inputs
folder. You can optionally generate a random file and run it against any solution