Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1010
Topic Prerequisites: Let you enter an array that contains a starting point S, an end point D, and a time t. (where x represents the wall,.) )
Title: In the specified T-second, ask if you can get out of the maze. (it takes 1s to take each step).
Problem Solving Method: dfs+vis[][]+flag+ (new technique) parity pruning.
#include <iostream>#include<string.h>#include<cmath>;using namespacestd;Charmap[8][8];intvis[8][8];//(new) An array of Access tokensintn,m,t; intSx,dx,sy,dy;intFlag//(new) Judging statusintK//(new) record the number of obstaclesintAbsintAintb) { if(a<b) {returnB-A; } Else { returnA-b; }}voidDfsintXintYintDep//use "return;" in void () function means to jump out of this function. { if(dep>T) {return ; } if(x<0|| x>=n| | y<0|| y>=M) {return ; } if(flag) {return; } if(map[x][y]=='D'&&dep==T) {flag=1; return ; } intTemp=abs (X-DX) +abs (Y-dy);//(new) odd-even pruningtemp=t-temp-DEP; if(temp&1)//if (can't go down) { return; } //enumerates the next situation, DFS (..., dep+1); if(!vis[x-1][y]&&map[x-1][y]!='X')//(new) if (the next point satisfies the situation){vis[x-1][y]=1; DFS (x-1, y,dep+1); Vis[x-1][y]=0; } if(!vis[x+1][y]&&map[x+1][y]!='X')//(new) if (the next point satisfies the situation){vis[x+1][y]=1; DFS (x+1, y,dep+1); Vis[x+1][y]=0; } if(!vis[x][y-1]&&map[x][y-1]!='X')//(new) if (the next point satisfies the situation){vis[x][y-1]=1; DFS (x, y-1, dep+1); Vis[x][y-1]=0; } if(!vis[x][y+1]&&map[x][y+1]!='X')//(new) if (the next point satisfies the situation){vis[x][y+1]=1; DFS (x, y+1, dep+1); Vis[x][y+1]=0; }}intMain () { while(cin>>n>>m>>T) {if(n==0&&m==0&&t==0) { Break; } k=0; memset (Vis,0,sizeof(VIS));//(new) The initialization of the access token array. for(intI=0; i<n;i++) { for(intj=0; j<m;j++) {cin>>Map[i][j]; if(map[i][j]=='S') {SX=i; Sy=J; VIS[I][J]=1;//(added) to mark the starting point as "visited" } if(map[i][j]=='D') {DX=i; Dy=J; } if(map[i][j]=='X') {k++;//(new) record the number of obstacles} }} flag=0;//(new) The initial state is false; if(n*m-k-1>=T)//(new) t+k+1 (the position of beginning s) must be <= n*m (total number of squares){dfs (Sx,sy,0); } if(flag) {cout<<"yes\n"; } Else{cout<<"no\n"; } }}
Maze (iii): Under the XX limit conditions, whether to walk.