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.
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.
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
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.
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.
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
代码中T-step为剩余的步数或者说时间
dis1+dis2为最小剩余步数
从一个点到另一个点的步数可以不一样,但奇偶性一样
如果最短走偶数步,说明从这里到终点一定为偶数步,如果剩余的的时间是奇数,不可行
如果最短走奇数步,说明从这里到终点一定为奇数步,如果剩余的的时间是偶数,不可行
轻易得出:
奇数-偶数 = 奇数,偶数-奇数 = 奇数
奇数-奇数= 偶数,偶数-偶数=偶数
所以T-step-(dis1+dis2)为偶数才有机会满足规定时间到终点
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char map[10][10];
int book[10][10];
int startx,starty;//起点
int endx,endy;//终点
int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int flag;//判断可不可按照规定到达
int N,M,T;
void dfs(int x,int y,int step)
{
if(flag)
return;
if(x==endx&&y==endy&&step!=T)
return;
if(x==endx&&y==endy&&step==T){
flag=1;
return;
}
int dis1=x-endx,dis2=y-endy,temp;//奇偶性剪枝
if(dis1<0)
dis1=-dis1;
if(dis2<0)
dis2=-dis2;
temp=T-step-(dis1+dis2);//dis1+dis2为此时到终点的最短距离
if(temp<0||temp%2==1)//temp%2==1说明temp为奇数
return;
int xnext,ynext;
for(int i=0;i<4&&!flag;i++){
xnext=x+next[i][0];
ynext=y+next[i][1];
if(xnext>=1&&xnext<=N&&ynext>=1&&ynext<=M&&book[xnext][ynext]==0&&(map[xnext][ynext]=='.'||map[xnext][ynext]=='D')){
book[xnext][ynext]=1;
dfs(xnext,ynext,step+1);
book[xnext][ynext]=0;
}
}
return;
}
int main()
{
while(scanf("%d%d%d",&N,&M,&T)!=EOF&&N&&M&&T){
memset(book,0,sizeof(book));
flag=0;
getchar();
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='S'){
startx=i;
starty=j;
}
else if(map[i][j]=='D'){
endx=i;
endy=j;
}
}
getchar();
}
book[startx][starty]=1;
dfs(startx,starty,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
后来写的代码:
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> using namespace std; int n,m,times,startx,starty,endx,endy; int map[10][10],book[10][10]; int Next[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int check(int x,int y){ return (x>=0 && x<n && y>=0 && y<m && !book[x][y] && map[x][y] != 'X'); } int dfs(int x,int y,int step){ if(x == endx && y == endy){ return step == times; } int dis = times - step - (int)fabs(endx - x) - (int)fabs(endy - y); if(dis < 0 || dis % 2) return 0; for(int i = 0;i < 4;i++){ int tx,ty; tx = x + Next[i][0],ty = y + Next[i][1]; if(check(tx,ty)){ book[tx][ty] = 1; if(dfs(tx,ty,step+1)){ return 1; } book[tx][ty]=0; } } return 0; } int main(){ while(scanf("%d%d%d",&n,&m,×)!=EOF&&n){ getchar(); for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ scanf("%d",&map[i][j]); if(map[i][j] == 'S') startx = i,starty= j; if(map[i][j] == 'D') endx = i,endy = j; } getchar(); } memset(book,0,sizeof(book)); book[startx][starty]=1; if(dfs(startx,starty,0)) puts("YES"); else puts("NO"); } return 0; }