Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 93817 Accepted Submission(s): 25480
Problem Description
The 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 was a trap, and he tried desperately to get out of this maze.
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.
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.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each 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 case is not to be processed.
'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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES//题意: 给你一个n*m的迷宫,并给定一个时刻t. 出发点为S,X为墙(不能走),D表示门,门在时刻t开放,问你是否可以出去。 //思路: 刚开始没想那么多,就用bfs写,错了5次(。。。)。 问了队友,队友说用dfs写,并且还得用奇偶剪枝(不剪枝超时,因为要判断的路太多了),#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char a[10][10]; int b[10][10]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int flag; int n,m,t; int sx,sy,ex,ey; void dfs(int x,int y,int l) { if(flag) return ; if(a[x][y]=='D'&&l==t) { flag=1; return ; } int tmp=t-abs(ex-x)-abs(ey-y)-l; if(tmp<0||tmp&1) return ; for(int i=0;i<4;i++) { int nx=x+dx[i]; int ny=y+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&!b[nx][ny]&&a[nx][ny]!='X'&&l+1<=t) { b[nx][ny]=1; dfs(nx,ny,l+1); b[nx][ny]=0; } } } int main() { int i,j; while(scanf("%d%d%d",&n,&m,&t),n|n|t) { for(i=0;i<n;i++) scanf("%s",a[i]); int w=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(a[i][j]=='S') { sx=i;sy=j; } else if(a[i][j]=='D') { ex=i;ey=j; } else if(a[i][j]=='X') w++; } } memset(b,0,sizeof(b)); b[sx][sy]=1; flag=0; if(t<n*m-w) dfs(sx,sy,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
//下面的是我第一次写的bfs(WA、WA、WA)。。。#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; int n,m,t; int b[10][10]; char a[10][10]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; struct zz { int x; int y; int l; }f1,f2; void bfs(int x,int y) { queue<zz>q; memset(b,0,sizeof(b)); f1.x=x;f1.y=y;f1.l=0; q.push(f1); b[f1.x][f1.y]=1; while(!q.empty()) { f1=q.front(); q.pop(); if(a[f1.x][f1.y]=='D'&&f1.l==t) { printf("YES\n"); return ; } for(int i=0;i<4;i++) { f2.x=dx[i]+f1.x; f2.y=dy[i]+f1.y; f2.l=f1.l+1; if(f2.x>0&&f2.x<=n&&f2.y>0&&f2.y<=m&&!b[f2.x][f2.y]&&f2.l<t&&a[f2.x][f2.y]!='X') { b[f2.x][f2.y]=1; q.push(f2); } } } printf("NO\n"); } int main() { int sx,sy; while(scanf("%d%d%d",&n,&m,&t),n|m|t) { for(int i=1;i<=n;i++) scanf("%s",a[i]+1); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]=='S') { sx=i; sy=j; } } } bfs(sx,sy); } return 0; }