1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5typedefstructNode6 {7 intx, y;8 }node;9 Ten Const intMAX =10000; One Const intN = the; A Const intdir[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1} };//Move Direction - BOOLVisit[n][n];//Tag Array - CharBoard[n][n];//Chess Board the intTurn[n][n];//The turn array is used to record the minimum number of turns known on the Board . -Node origin, destination;//Start, End - intMaxturncount;//the maximum number of turns the road fetish can withstand - intM, N;//the rows and columns of a chessboard + - +InlineBOOLInboard (Const int&row,Const int&Col) A { at return 0<= row && Row < m &&0<= Col && Col <N; - } - - /*DFS Search (parameter meaning: row, column, direction of last move)*/ - BOOLDfsConst int&row,Const int&col,intdirection) - { in //reach the end of the road where the fool can withstand the number of turns - if(row = = destination.x && col = = destination.y && Turn[row][col] <=maxturncount) to return true; + - //the number of turns is more than the number of times the road fetish can withstand, the road is dizzy, so not to take the if(Turn[row][col] >maxturncount) * return false; $ Panax Notoginseng //the location of the road is not the same row as the end point, then at least one more turn, but the current number of turns is equal to the maximum number of turns the road fetish can withstand - if(Row! = destination.x && col! = destination.y && Turn[row][col] = =maxturncount) the return false; + A for(inti =0; I <4; ++i) the { + intR = row + dir[i][0]; - intC = col + dir[i][1]; $ if(true= = Inboard (r, c) &&'.'= = Board[r][c] &&false==Visit[r][c]) $ { - /*Turn[r][c] Records the shortest number of turns (r,c) used in history, Turn[r][c] The initial value of Max*/ - /*pruning 1: From (row, col) position to (R, c) position, if TURN[R][C] < Turn[row][col], description before the Road Crazy has been here, and the last time the number of turns than this small, last time did not go to the end, this time more can not go. - the same situation cannot be cut off, for a chestnut: from the starting point (1, 1), Toward the End (3, 5), one way is to go right, one wayWuyi is to go down. When walking to the right, go to (3, 4) when the number of turns is 1, then turn[3][4] = 1, in the turn once the only to go, then need to turn two times, go down, and go to (3, 4), turn is also 1, because before walked, - However, the number of turns is the same two times, the latter case (go to the end only need to turn one time) was pruned cut off, if the topic to Wu the maximum number of turns is 1, then you will not get the correct answer. - ....* About .**.* $ ..... - */ - if(Turn[r][c] <Turn[row][col]) - Continue; A + /*pruning 2: Similar to the principle of pruning 1, from (Row, col) to (R, c), because the direction has changed, so the number of turns the You need to add one, turn the number of times and if there are more turns in history than (R, c), you need to cut out the situation. - the same situation cannot be cut off.*/ $ if(-1! = Direction && I! = Direction && Turn[r][c] < Turn[row][col] +1) the Continue; the the if(-1! = Direction && I! =direction) theTURN[R][C] = Turn[row][col] +1;//The direction of movement is different from the previous, turning number +1 - Else inTURN[R][C] = Turn[row][col];//The direction of movement is the same as before, and the number of turns is constant the theVISIT[R][C] =true; About the if(true= = DFS (r, c, i))return true; the the //Backtracking +VISIT[R][C] =false; - } the }Bayi return false; the } the - voidInputvoid) - { thescanf"%d%d", &m, &n);//Enter rows, columns the for(inti =0; I < m; ++i)//Input Board thescanf"%s", Board[i]); the - //Here are the pits: "5 integers k, x1, y1, x2, y2 (1≤k≤10, 1≤x1, X2≤n, 1≤y1, y2≤m)" the //1≤x1, x2≤n = x1,x2 is the column; 1≤y1, y2≤m = Y1,y2 is the line the //The man dug the hole, do not be bound by the inertia of thinking thescanf"%d %d%d%d%d", &maxturncount, &ORIGIN.Y, &origin.x, &DESTINATION.Y, &destination.x);94 the //Initializing Data the for(inti =0; I < m; ++i) the for(intj =0; J < N; ++j)98TURN[I][J] =MAX; Aboutmemset (Visit,0,sizeof(visit)); -origin.y--;//The title gives the coordinates starting at 1, so the coordinates need to be processed, starting from 0101origin.x--;102destination.y--;103destination.x--;104 } the 106 intMainvoid)107 {108 intT//number of test groups109scanf"%d", &t); the for(intK =0; K < T; ++k)111 { the input ();113VISIT[ORIGIN.X][ORIGIN.Y] =true; theTURN[ORIGIN.X][ORIGIN.Y] =0; the if(true= = DFS (origin.x, ORIGIN.Y,-1)) theprintf"yes\n");117 Else118printf"no\n");119 } - return 0;121}
Hdu 1728 dfs+ pruning escape maze