Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 103212 Accepted Submission(s): 27988
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.
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
Author
ZHANG, Zheng
Source
ZJCPC2004
题解:给你一个n*m的图,上面的标志如题所述,要那只可怜的小狗刚好在T秒内到达D,一秒走一格。
dfs+奇偶剪枝
AC代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,ex,ey,t;
bool success;
char maze[10][10];
/*
stx ---->开始x坐标
sty ---->开始y坐标
dt ---->花掉时间
*/
void dfs(int stx,int sty,int dt )
{
if(stx<=0||stx>n||sty<=0||sty>m)
return ;
if(stx==ex&&sty==ey&&dt==t)
success=true;
if(success) return ;
int temp=(t-dt)-abs(ex-stx)-abs(ey-sty);
if(temp<0||temp&1) //奇偶剪枝
return ;
/*
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
无论是从o 开始还是从1开始,
都是 0--->1 或者1--->0 都是奇数步
0-->0 , 1--->1 都是偶数步
*/
//对上下左右的搜索
if(maze[stx][sty+1]!='X') //向右搜索
{
maze[stx][sty+1]='X';
dfs(stx,sty+1,dt+1);
maze[stx][sty+1]='.';
}
if(maze[stx+1][sty]!='X') //向下搜索
{
maze[stx+1][sty]='X';
dfs(stx+1,sty,dt+1);
maze[stx+1][sty]='.';
}
if(maze[stx][sty-1]!='X') //向左搜索
{
maze[stx][sty-1]='X';
dfs(stx,sty-1,dt+1);
maze[stx][sty-1]='.';
}
if(maze[stx-1][sty]!='X') //向上搜索
{
maze[stx-1][sty]='X';
dfs(stx-1,sty,dt+1);
maze[stx-1][sty]='.';
}
return ;
}
int main()
{
int stx,sty,wall;
while(scanf("%d%d%d",&n,&m,&t),n+m+t)
{
getchar();
wall=0; //统计障碍物的个数
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='S')
{
stx=i; // 标注开始的x轴的位置
sty=j; // 标注开始的y轴的位置
}
else
if(maze[i][j]=='D')
{
ex=i; // 标注结束的x轴的位置
ey=j; // 标注结束的y轴的位置
}
else if(maze[i][j]=='X')
{
wall++;
}
}
getchar();
}
success=false;
maze[stx][sty]='X'; //堵住入口
if( n*m-wall<=t ) //因为只有在t时刻door才打开
printf("NO\n");
else
{
dfs(stx,sty,0);
if(success)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
一时懵逼写出这样的BFS:
#include<bits/stdc++.h>
char Map[12][12];
int marki2; int markj2; //终点
//方向:
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int n,m,t;
void dfs(int x,int y)
{
int tx,ty;
Map[x][y]='F';
for(int i=0;i<4;i++)
{
tx=dx[i]+x;
ty=dy[i]+y;
if(Map[tx][ty]=='D')break;
if(tx>=0&&tx<n&&ty>=0&&ty<m&&Map[tx][ty]=='.'&&Map[tx][ty]!='X'&&Map[tx][ty]!='F')
{
Map[tx][ty]='F';
dfs(tx,ty);
}
}
}
int main()
{
int count;
int marki1; int markj1;
while(cin>>n>>m>>t)
{
if(m==0&&n==0&&t==0) break;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='S')
{
marki1=i; markj1=j;
}
if(Map[i][j]=='D')
{
marki2=i; markj2=j;
}
}
getchar();
}
dfs(marki1,markj1);
count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(Map[i][j]=='F')
count++;
}
}
//cout<<count<<endl;
if(count-1<=t) puts("YES");
else puts("NO");
}
return 0;
}