HDU 1180 weird STAIR (DFS)
Weird stairsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission (s): 6472 Accepted Submission (s): 1525
After the official start of Problem DescriptionHogwarts, Harry found that some stairs in Hogwarts were not static. On the contrary, they changed their directions every minute.
For example, in the following example, the first stair is in the vertical direction. One minute later it moves to the horizontal direction, and another minute it returns to the vertical direction. harry found it hard for him to find the path that would allow him to reach his destination as quickly as possible. At this time, Ron (Harry's best friend) told Harry that he had a magic item to help him find the path, the magic spell on that magic item is exactly written by you.
There are multiple groups of Input test data. Each group is described as follows:
The first row has two numbers, M and N. Next is a map of N columns in the M row. '*' indicates an obstacle ,'. 'indicates a corridor,' | 'or'-'indicates a stair and its position at the beginning:' | 'indicates that the stair is in the vertical direction at the beginning, '-' indicates that the stair is in the horizontal direction at the beginning. another 's' in the map is the starting point, 't' is the target, 0 <= M, N & lt; = 20, there will be no two connected ladders in the map. harry can only stay '. 'or' S 'and' T.
Output has only one row, containing a number T, indicating the shortest time to reach the target.
Note: Harry can only walk to the adjacent grid each time, but cannot walk diagonally. Each movement happens to be a minute, and it takes only one minute for Harry to climb the stairs and pass the stairs to the opposite side, harry never stays on the stairs. and every time the stairs were moved, Harry changed his direction.
Sample Input
5 5**..T**.*...|...*.*.S....
Sample Output
7HintThe Hint map is as follows: note that after the stairs are used once, they can be used again. Because they can be used to walk horizontally and vertically, it does not mark whether the stairs have passed # include
# Include
Response + response Bytes Pipeline = "* '| flag [xx] [yy] = 1) continue; if (map [xx] [yy] = 'T') return next. step; if (ma P [xx] [yy] = '|') {if (now. step % 2 = 0 & (I = 1 | I = 3) | now. step % 2 = 1 & (I = 0 | I = 2) {next. x = next. x + dir [I] [0]; next. y = next. y + dir [I] [1]; if (map [next. x] [next. y] = '*' | flag [xx] [yy] = 1) continue; if (map [next. x] [next. y] = 'T') return next. step; flag [next. x] [next. y] = 1;. push (next);} else {next. x = next. x + dir [I] [0]; next. y = next. y + dir [I] [1]; next. step = next. step + 1; next. f = 1; if (map [next. x] [next. y] = '*' | flag [Xx] [yy] = 1) continue; if (map [next. x] [next. y] = 'T') return next. step; flag [next. x] [next. y] = 1;. push (next) ;}continue ;}if (map [xx] [yy] = '-') {if (now. step % 2 = 0 & (I = 0 | I = 2) | now. step % 2 = 1 & (I = 1 | I = 3) {next. x = next. x + dir [I] [0]; next. y = next. y + dir [I] [1]; if (map [next. x] [next. y] = 'T') return next. step; if (map [next. x] [next. y] = '*' | flag [xx] [yy] = 1) continue; flag [next. x] [next. y] = 1;. push (next );} Else {next. x = next. x + dir [I] [0]; next. y = next. y + dir [I] [1]; if (map [next. x] [next. y] = '*' | flag [xx] [yy] = 1) continue; next. step = next. step + 1; next. f = 1; if (map [next. x] [next. y] = 'T') return next. step; flag [next. x] [next. y] = 1;. push (next) ;}continue;} flag [xx] [yy] = 1;. push (next) ;}}int main () {int n, m, I, j; while (~ Scanf ("% d", & n, & m) {sum = 0; memset (flag, 0, sizeof (flag); memset (map, '*', sizeof (map); clear (); for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) {cin> map [I] [j]; if (map [I] [j] = 's') {x1 = I; z1 = j ;}} flag [x1] [z1] = 1; sum = bfs (x1, z1); cout <