# Tempter of the Bone
**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95890 Accepted Submission(s): 25982**
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
~~~
1.奇偶剪枝:http://baike.baidu.com/view/7789287.htm 百度百科,假设起点是sx,sy终点是ex,ey那么abs(ex-sx)+abs(ey-ey)为起点到终点的最短步数。起点到终点的步数要么是最短步数(最短步数+0),要么是最短步数+一个偶数(偏移路径)。
2.代码:
~~~
#include<cstdio>
#include<cstring>
using namespace std;
char mat[10][10];
bool vis[10][10];
int n,m,t;
int xs,ys,xd,yd;
bool flag;
int dir[4][2]={1,0,-1,0,0,-1,0,1};//up,lower,left,right
int ABS(int a)
{
if(a<0)
return -a;
else
return a;
}
void dfs(int x,int y,int time)
{
if(x<0||x>=n||y<0||y>=m)
return;
if(mat[x][y]=='X')
return;
if(vis[x][y]==1)
return;
vis[x][y]=1;
if(x==xd&&y==yd&&time==t)
{
flag=true;
return;
}
if(time==t&&(x!=xd||y!=yd))
return;
if(time>t)
return;
for(int i=0;i<4;i++)
{
int xt=x+dir[i][0];
int yt=y+dir[i][1];
if(xt<0||xt>=n||yt<0||yt>=m||mat[xt][yt]=='X'||vis[xt][yt]==1)
continue;
dfs(xt,yt,++time);
vis[xt][yt]=0;
time--;
if(flag==1)
return;
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
{
for(int i=0;i<n;i++)
{
scanf("%s",mat[i]);
for(int j=0;j<m;j++)
{
if(mat[i][j]=='S')
{
xs=i;
ys=j;
}
if(mat[i][j]=='D')
{
xd=i;
yd=j;
}
}
}
int ms=ABS(xd-xs)+ABS(yd-ys);
if(t<ms)
{
printf("NO\n");
continue;
}
if((t-ms)%2)
{
printf("NO\n");
continue;
}
flag=false;
memset(vis,0,sizeof(vis));
dfs(xs,ys,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
~~~
- 前言
- The 12th Zhejiang Provincial Collegiate Programming Contest - D
- 用邻接表存储n个顶点m条弧的有向图
- hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)
- hdu 1358 Period(给定一个字符串,求有多少个前缀(包括自己本身),它是由k(k&gt;2,并且尽量大)个循环节组成的)
- hdu 1806 Frequent values(给定一个非降序数组,求任意区间内出现次数最多的数的次数)
- poj 3264 Balanced Lineup(查询区间最大值与最小值的差)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1015 Safecracker(第一次用了搜索去遍历超时,第二次用for循环可以了,思路一样的)
- HDU 1016 Prime Ring Problem(DFS)
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
- HDU 1072 Nightmare(BFS)
- HDU 1237 简单计算器(后缀式+栈)