# 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 簡單計算器(后綴式+棧)