codeforces#P683J. The Hero with Bombs
The Hero with Bombs
Description
In a new computer game you need to help the hero to get out of the maze, which is a rectangular field of size n × m. The hero is located in one of the cells of this field. He knows where the exit of the maze is, and he wants to reach it.
In one move, the hero can either move to the next cell (i.e. the cell which has a common side with the current cell) if it is free, or plant a bomb on the cell where he is, or skip the move and do nothing. A planted bomb explodes after three moves, that is, after the hero makes 3 more actions but does not have time to make the fourth (all three types of moves described above are considered as actions).
The explosion destroys the obstacles in all the cells which have at least one common point with this cell (i.e. in all the cells sharing with the bomb cell a corner or a side). The explosion must not hurt the cell with the exit or the cell with the hero. The hero can not go beyond the boundaries of the maze.
Your task is to determine the sequence of hero's actions to reach the exit. Note that you haven't to minimize the length of the sequence. The only restriction — the length of the resulting sequence should not exceed 100,000 symbols.
The first line contains two integers n and m (1 ≤ n, m ≤ 100, n·m > 1) — sizes of the maze.
Each of the following n lines contains m characters — description of the maze. The character "." means a free cell, "E" — the hero, "T" — the exit, "X" — the obstacle.
It is guaranteed that there is exactly one hero and exactly one exit in the maze.
Print the hero's actions which will help him get out of the maze ("M" — to plant a bomb, "T" — to skip the move, "S" — to go down, "W" — to go left, "N" — to go up, "E" — to go right). If the hero can not reach the exit, print "No solution" (without quotes).
The length of the resulting sequence should not exceed 100,000 symbols. If there are several solutions it is allowed to print any of them.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100, n·m > 1) — sizes of the maze.
Each of the following n lines contains m characters — description of the maze. The character "." means a free cell, "E" — the hero, "T" — the exit, "X" — the obstacle.
It is guaranteed that there is exactly one hero and exactly one exit in the maze.
Output
Print the hero's actions which will help him get out of the maze ("M" — to plant a bomb, "T" — to skip the move, "S" — to go down, "W" — to go left, "N" — to go up, "E" — to go right). If the hero can not reach the exit, print "No solution" (without quotes).
The length of the resulting sequence should not exceed 100,000 symbols. If there are several solutions it is allowed to print any of them.
Samples
3 5
XEX.X
X.XXT
X.X.X
SSMNNTSSNEMWWTEEEE