| UTPC Spring 2026 Open Contest |
|---|
| Закончено |
Chief Scientist Randward Lirili want to explore a $$$N \times N$$$ grid on Larila using rovers.
Due to the high cost, each rover is programmable, but only with a sequence of $$$M$$$ instructions moving the rover North (N), East (E), South (S), or West (W). The rover repeats this sequence of instructions infinitely.
Some of the tiles on the grid are too precarious for the rover to travel through, if the rover needs to execute an instruction that will cause the rover to travel onto one of these tiles, the rover will skip this instruction and go to the next one. Moving outside of the grid is also considered precarious.
The rover is said to have returned to base if the rover reaches one of the ships that have landed in this grid. When the rover reaches the ship, it stops.
Given a sequence of instructions, determine whether a rover will return to base for every starting tile on the grid. If a rover starts on a tile that is precarious, it crashes and burns and automatically fails to return to base.
The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 \le N, M \le 500$$$).
Each of the next $$$N$$$ lines contains a string of $$$N$$$ characters describing a row of the grid, from north to south. Each character is one of . (safe), # (precarious), or S (ship).
The last line contains a string of $$$M$$$ characters describing the move sequence. Each character is one of N (north), S (south), E (east), or W (west).
Print $$$N$$$ lines, each containing $$$N$$$ characters. The character at row $$$i$$$, column $$$j$$$ should be 1 if a rover starting at that cell returns to base, and 0 otherwise.
3 4S..#.#...NNWW
111 010 010
| Название |
|---|


