Tempter of the Bone
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 82702 Accepted Submission (s): 22531
Problem DescriptionThe Doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone is a trap, and he tried desperately to get out of this maze.
The maze is a rectangle with sizes N by M. There is a door in the maze. At the beginning, the door is closed and it would open at the t-th second for a short period of time (less than 1 second) . Therefore the doggie had to arrive in the door on exactly the t-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once He entered a block, the ground of this block would start-to-sink and disappear in the next second. He could not stay on one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Inputthe input consists of multiple test cases. The first line of all test case contains three integers n, m, and T (1 < N, m < 7; 0 < T <), which denote The sizes of the maze and the time at which the door would open, respectively. The next N lines give the maze layout, with all line containing M characters. A character is one of the following:
' X ': a block of wall, which the doggie cannot enter;' S ': The start point of the doggie; ' D ': the Door; or '. ': an empty block.
The input is terminated with three 0 ' s. This test is a not-to-be processed.
Outputfor each test case, print on one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input4 4 5S. X... X... Xd.... 3 4 5S. X... X.... D0 0 0
Sample Outputnoyes
1#include <stdio.h>2#include <string.h>3 intN, M, T;4 BOOLvis[Ten][Ten], flag;5 Charmap[Ten][Ten] ;6 intmove[][2] = {{1,0} , {-1,0} , {0,1} , {0,-1}} ;7 intStep =0;8 9 voidDFS (intSxintSyintStep)Ten { One if(Step = = T +1&& Map[sx][sy] = ='D') { AFlag =1 ; - return ; - } the //printf ("(%d,%d)%c step=%d\n", SX, SY, Map[sx][sy], step); - if(Map[sx][sy] = ='D')return ; - if(Step > T)return ; - for(inti =0; I <4; i + +) { + intx, y; -x = SX + move[i][0] ; +y = sy + move[i][1] ; A if(X <0|| Y <0|| X >= N | | Y >= m)Continue ; at if(Map[x][y] = ='X')Continue ; - if(Vis[x][y])Continue ; -Vis[x][y] =1 ; -DFS (x, y, step +1) ; - if(flag)return ; -Vis[x][y] =0 ; in } - } to + intMain () - { the //freopen ("A.txt", "R", stdin); * while(~ scanf ("%d%d%d", &n, &m, &T)) { $ if(n = =0&&m = =0&& T = =0) Break ;Panax Notoginseng for(inti =0; I < n; i + +) scanf ("%s", Map[i]); -Flag =0 ; theStep =0 ; +memset (Vis,0,sizeof(Vis)); A for(inti =0; I < n; i + +) { the for(intj =0; J < M; J + +) { + if(Map[i][j] = ='S') { -VIS[I][J] =1 ; $DFS (I, J,1) ; $ } - } - } the if(flag) puts ("YES") ; - ElsePuts ("NO") ;Wuyi } the return 0 ; -}View Code
The question is to be pruned, but I have not cut it. (Although the line, 23333333333333333)
Reprint:http://www.2cto.com/kf/201307/229103.html
Parity pruning: According to the topic, doggie must arrive at the door in the first T-second. That is the need to walk t-1 step. Set Doggie start position is (Sx,sy), the target position is (Ex,ey). If ABS (ex-x) +abs (ey-y) is even, ABS (EX-X) and ABS (EY-Y) have the same parity, so you need to take even steps; when ABS (Ex-x) +abs ( EY-Y) is odd, ABS (Ex-x) and ABS (EY-Y) have different parity, so it takes odd steps to reach the target position. It can save a lot of time by first judging the parity and searching, otherwise it is easy to time out. T-sum How many steps it will take to reach the target location. Since the title requires doggie to reach the gate in the first T-second, the parity between (T-step) and ABS (Ex-x) +abs (ey-y) is necessarily the same. therefore temp= (t-step)-abs (ex-x) +abs (ey-y) is necessarily an even number.
Hdu.1010.tempter of the Bone (dfs+-parity pruning)