Please Take My Gift
Time limit2sMemory limit512 MB
Each cell of a grid holds a direction; every walk follows those arrows forever. Find the fewest cells to mark so every walk visits a marked cell.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
Wookje is a devoted fan of Gusagwa. Today Wookje wants to deliver a gift to Gusagwa. After several days of observation, Wookje has worked out Gusagwa's movement pattern completely.
The area where Gusagwa lives is a rectangular map of size , divided into square cells of size . Gusagwa's position is written as , which means the cell in the -th row from the top and the -th column from the left.
Each cell of the map holds one of the letters N, W, E, S, and Gusagwa moves according to that letter. When Gusagwa stands on cell , the letter N teleports Gusagwa to , S to , W to , and E to . Gusagwa never gets tired and keeps moving forever.
Wookje does not know where Gusagwa is right now, so Wookje wants a way to deliver the gift no matter which cell Gusagwa starts from. When Gusagwa arrives at a cell that holds a gift, Gusagwa always takes it. Write a program that finds the minimum number of cells on which gifts must be placed so that Gusagwa always takes a gift, regardless of the starting cell.
Input
The first line contains the height and the width of the map. (, )
Each of the next lines contains one row of the map. Each line is a string of length made only of the letters N, W, E, S.
Following the letters on the map never leads outside the map.
Output
Print on the first line the minimum number of cells on which gifts must be placed.