Decide whether a princess can reach the single escape hatch on a grid while any number of soldiers chase her simultaneously and catch her by sharing a cell.
Hard8GraphBFSGame theorySimulationNo attempts yetTime limit2sMemory limit512 MBThe people of a kingdom have risen up against the princess and her misrule. The revolutionary army has stormed the royal palace where she lives, and its soldiers are searching the palace to catch her. Write a program that decides whether the princess can escape from the palace.
The floor of the palace is a rectangle divided into a grid. There are two kinds of cells. A cell that the princess and the soldiers can enter is called an empty cell, and a cell that nobody can enter is called a wall. At the start, the princess and the soldiers stand on distinct empty cells. The grid has exactly one escape hatch, and the princess escapes the palace as soon as she reaches it. There are zero or more soldiers.
In every unit of time the princess and all soldiers act simultaneously, so each of them must choose an action without knowing what the others will do next. In one unit of time the princess or a soldier may move to a horizontally or vertically adjacent empty cell or stay in the current cell, and nobody may leave the floor of the palace. If, after the move, the princess and one or more soldiers are in the same cell, the princess is caught. It is guaranteed that the princess could reach the escape hatch through empty cells alone if every soldier were removed from the palace.
The princess can escape the soldiers if there is a route for her on which the soldiers cannot catch her no matter how they move. Note that if the princess and a soldier reach the escape hatch at the same time, the princess is caught. Can the princess escape from the palace?
The input has the following format.
H W
map1
.
.
.
mapH
The first line contains two integers H and W separated by a space, the height and the width of the grid. (2≤H,W≤200)
The i-th of the next H lines contains a string mapi of length W that describes the floor of the palace. The j-th character of mapi is the state of the cell in row i, column j.
'@', '$', '%', '.', and '#' stand for the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that the grid contains exactly one '@', exactly one '%', and zero or more '$'.
Print one line containing "Yes" if the princess can escape from the palace, and "No" otherwise.