Why the Williamson's Sapsucker Came Up to Information Island
InterviewTime limit1sMemory limit256 MB
On a grid with walls, find which of three food cells is nearest to the start cell 2, and print TAK with that distance, or NIE if none is reachable.
- Level
Medium4 of 10
- Topics
- BFS, Graph, Matrix, Shortest path
- Solved
- No attempts yet
Problem
A family of three Williamson's sapsuckers has come up to Information Island.
The three Williamson's sapsuckers were sitting together somewhere on the second floor of Information Island, resting, when they spotted cheonggukjang, sushi, and mac and cheese far away. The father wants cheonggukjang, the mother wants sushi, and the child wants mac and cheese. So the three decided to go eat the food closest to their current position.
The second floor of Information Island is represented as an grid. If some is 0, it is an empty corridor that can be passed through; if it is 1, it is blocked by an obstacle and cannot be passed through. The Williamson's sapsucker family is 2, cheonggukjang is 3, sushi is 4, and mac and cheese is 5. A Williamson's sapsucker can move one cell up, down, left, or right per unit time. Since 2, 3, 4, and 5 are not obstacles, they can be passed through. You cannot leave the grid, and the distance from the starting point to the starting point is 0. The starting point is the position of the Williamson's sapsucker, that is, the position of 2.
Which food can the Williamson's sapsucker family reach faster?
Input
The first line gives the size of the second floor of Information Island, and . (, )
The following lines of columns give , consisting only of 0, 1, 2, 3, 4, and 5. No space is given between and .
Each of 2, 3, 4, and 5 appears exactly once, and everything other than 2, 3, 4, and 5 is 0 or 1.
Output
On the first line, print "TAK" (meaning YES in Polish), and on the second line, print the shortest distance from the current position to the food that can be reached fastest.
If no food can be eaten, print "NIE" (meaning NO in Polish). Do not print anything else.
When printing TAK and NIE, do not print the quotation marks, and they must be printed in uppercase.