Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92175 Accepted Submission(s): 25051
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was 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 at 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 at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'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 case is not to be processed.
s | ||||
| | ||||
| | ||||
| | ||||
+ | — | — | — | e |
s | — | — | — | |
— | — | + | ||
| | + | |||
| | ||||
+ | — | — | — | e |
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define mem(x,y) memset(x,0,sizeof(x)) 5 const int MAXN=7; 6 int disx[4]={0,0,1,-1}; 7 int disy[4]={1,-1,0,0}; 8 int N,M,T,e_x,e_y; 9 char map[MAXN][MAXN]; 10 int vis[MAXN][MAXN]; 11 int ans; 12 void dfs(int x,int y,int sec){ 13 if(ans)return; 14 if(map[x][y]=='D'){ 15 // printf("%d**",sec); 16 if(sec==T)ans=1; 17 return; 18 } 19 int temp=T-abs(e_x-x)-abs(e_y-y)-sec;//奇偶剪枝。。。 20 if(temp<0||temp&1)return; 21 22 for(int i=0;i<4;i++){ 23 int nx,ny; 24 nx=x+disx[i];ny=y+disy[i]; 25 if(nx<0||ny<0||nx>=N||ny>=M||vis[nx][ny]||map[nx][ny]=='X')continue;//>=写错了,错了半天。。。 26 if(sec+1>T)continue; 27 vis[nx][ny]=1; 28 dfs(nx,ny,sec+1); 29 vis[nx][ny]=0; 30 } 31 } 32 int main(){ 33 while(scanf("%d%d%d",&N,&M,&T),N||M||T){ 34 for(int i=0;i<N;i++)scanf("%s",map[i]); 35 int sx,sy; 36 int wall=0; 37 for(int x=0;x<N;x++)for(int y=0;y<M;y++) 38 if(map[x][y]=='S')sx=x,sy=y; 39 else if(map[x][y]=='D')e_x=x,e_y=y; 40 else if(map[x][y]=='X')wall++; 41 mem(vis,0);vis[sx][sy]=1; 42 ans=0; 43 if(T<N*M-wall)dfs(sx,sy,0); 44 if(ans)puts("YES"); 45 else puts("NO"); 46 } 47 return 0; 48 }
bfs wa代码扔着留念吧:
#include<stdio.h> #include<string.h> const int MAXN=7; #include<queue> #define mem(x,y) memset(x,y,sizeof(x)) using namespace std; struct Node{ int x,y,sec; }; char map[MAXN][MAXN]; int disx[4]={0,0,1,-1}; int disy[4]={1,-1,0,0}; int vis[MAXN][MAXN]; int N,M,T; bool bfs(int sx,int sy){ queue<Node>dl; Node a,b; mem(vis,0); vis[sx][sy]=1; a.x=sx;a.y=sy;a.sec=0; dl.push(a); while(!dl.empty()){ a=dl.front(); dl.pop(); for(int i=0;i<4;i++){ b.x=a.x+disx[i];b.y=a.y+disy[i];b.sec=a.sec+1; if(b.x<0||b.y<0||b.x>=N||b.y>=M||vis[b.x][b.y]==1||map[b.x][b.y]=='X')continue; if(b.sec>T)continue; if(map[b.x][b.y]=='D'){ if(b.sec==T)return true; continue; } vis[b.x][b.y]=1; dl.push(b); } } return false; } int main(){ while(scanf("%d%d%d",&N,&M,&T),N|M|T){ for(int i=0;i<N;i++)scanf("%s",map[i]); int sx,sy; for(int x=0;x<N;x++)for(int y=0;y<N;y++) if(map[x][y]=='S')sx=x,sy=y; if(bfs(sx,sy))puts("YES"); else puts("NO"); } return 0; }