FixImage
SRM 396 · 2008-04-03 · by asal1
Problem Statement
The manhattan distance between two pixels A and B with coordinates (xA, yA) and (xB, yB), respectively, is the sum of the (absolute) differences of their coordinates(i.e. |xA - xB| + |yA - yB|).
Our friend sent us an image where all the black blocks are smooth. Due to some transmission errors, some black pixels were transmitted as white (but all white pixels remained white). Your task is to retrieve the original image. Find the minimum number of pixels you have to change from white to black so that every black block is smooth and return the original image in the same format as the altered one. The solution with the minimum number of changes will always be unique.
Constraints
- alteredImage will contain between 1 and 50 elements, inclusive.
- Each element of alteredImage will contain between 1 and 50 elements, inclusive.
- Each element of alteredImage will contain the same number of characters.
- alteredImage will contain only the characters '.' and '#'.
Statement by TopCoder, Inc. — view the original on the archive.
{"....",
".##.",
".##.",
"...."}
Returns: {"....", ".##.", ".##.", "...." }
This block is smooth.
{".....",
".###.",
".#.#.",
".###.",
"....."}
Returns: {".....", ".###.", ".###.", ".###.", "....." }
This block is not smooth. We need to make the center pixel black.
{".......",
".###...",
".#..##.",
".###.#.",
".....#."}
Returns: {".......", ".###...", ".#####.", ".#####.", ".....#." }
This image consists of two blocks. The right one is smooth. We make the left one smooth by changing the white pixels inside it, but now our image consists of only one block which is not smooth. To make it smooth we need to change one more white square.
{".................",
"#####.#..#..#####",
"..#...#..#....#..",
"..#...#..###..#..",
"................."}
Returns: {".................", "#####.#..#..#####", "..#...#..#....#..", "..#...#..###..#..", "................." }
These smooth blocks spell out the word "TILT".
{"###.####",
"#.#.#..#",
".#...#.#",
".#####.#",
"......#.",
"########"}
Returns: {"########", "########", "########", "########", "########", "########" }
Submissions are judged against all 52 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class FixImage with a public method vector<string> originalImage(vector<string> alteredImage) · 52 test cases · 2 s / 256 MB per case