您的位置:首页 > 其它

ACM--steps--4.3.1--Tempter of the Bone

2015-03-13 18:42 183 查看

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1190 Accepted Submission(s): 390
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

NO
YES


Author

ZHANG, Zheng

Source

ZJCPC2004

Recommend

JGShining

#include<iostream>
#include<cstdlib>
using namespace std;
struct dyx
{
int x,y;
};
char map[10][10];
int n,m,t;//地图的长和宽,门会在第t秒打开。
int destx,desty,sum;
bool wyx;//判断条件;
//方向量的输入。
int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};
void dfs(int nowx,int nowy,int nowt)
{
//进行判断的时候要进行奇偶性的剪枝。
//计算出两点之间的最短路径,然后不管中间的怎么曲折的绕路;
//两点之间的距离都是:最短路径+一个偶数,所以两点之间的路径的奇偶性不改变.
//根据整数的加减特性,偶数=奇数+-奇数,偶数=偶数+-偶数,奇数=偶数+-奇数。进行判断;
//if(nowt>t)
//return;
if(nowt==t&&nowx==destx&&nowy==desty)
wyx=true;
if(wyx)
return;
//t-nowt和abs(end.x-start.x)-abs(end.y-start.y)的奇偶性一定相同。
//t-nowt为到目标还有多少步,且必须在第t秒到达;
int temp=t-nowt-abs(destx-nowx)-abs(desty-nowy);//两点之间的最短步数
//if((abs(end1.x-start.x)+abs(end1.y-start.y))%2!=(t-nowt)%2)
//return;
if(temp&1)//temp一定为偶数,若为奇数则不可能.
return;
for(int i=0;i<4;i++)
{
int kx=nowx+dx[i];
int ky=nowy+dy[i];
if(kx>=0&&kx<n&&ky>=0&&ky<m&&map[kx][ky]!='X')
{
map[kx][ky]='X';
dfs(kx,ky,nowt+1);
//回溯点成为'.';
map[kx][ky]='.';
}
}
}
int main()
{
while(cin>>n>>m>>t)
{
if(n==0&&m==0&&t==0)
break;
int curx,cury;
//int sum;//用来计数可以走的格子一共有多少个;
//题目要求的是在第t秒到达,就是前面有t-1秒/
//sum=0;
wyx=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
curx=i;
cury=j;
}
else if(map[i][j]=='D')
{
++sum;
destx=i;
desty=j;
}
else if(map[i][j]=='.')
{
++sum;
}
}
}
map[curx][cury]='X';//走过一步的格子就塌了,不能够回头;
if(sum>=t)
dfs(curx,cury,0);
if(wyx)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: