hduoj1010,Tempter of the Bone,持续dfs剪枝剪枝
2012-11-07 20:12
337 查看
Tempter of the Bone
Problem DescriptionThe 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.以走的最短路径小于时间
2.可以把map看成这样:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子
从为 1 的格子走一步,必然走向为 0 的格子
即:
0 ->1或1->0必然是奇数步
0->0 走1->1必然是偶数步
直接判断行与列相加对2取余的值来判断0,1
code:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<cstdlib> using namespace std; char area[10][10]; int n,m,t,si,sj,di,dj; int dir[4][2]={-1,0,0,-1,1,0,0,1}; bool ok; void dfs(int si,int sj,int step) { if(ok==true) return; if(step==t) { if(si==di&&sj==dj) { ok=true; printf("YES\n"); return; } else return; } for(int i=0;i<4;i++) { if(si+dir[i][0]>0&&si+dir[i][0]<=n&&sj+dir[i][1]>0&&sj+dir[i][1]<=m&&area[si+dir[i][0]][sj+dir[i][1]]!='X') { area[si][sj]='X'; dfs(si+dir[i][0],sj+dir[i][1],step+1); if(!ok) area[si][sj]='.'; } } } int main() { while(scanf("%d %d %d",&n,&m,&t),n||m||t) { ok=false; memset(area,'X',sizeof(area)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>area[i][j];//cin可以忽略换行符,避免了getchar()的使用 if(area[i][j]=='S') {si=i;sj=j;} else if(area[i][j]=='D') {di=i;dj=j;} } if(abs(si-di)+abs(sj-dj)>t||(t%2!=0&&((si+sj)%2==(di+dj)%2))||t%2==0&&((si+sj)%2!=(di+dj)%2)) ok=false; else dfs(si,sj,0); if(!ok) printf("NO\n"); } return 0; }
相关文章推荐
- HDU 1010 Tempter of the Bone (dfs + 剪枝)
- HDU 1010 Tempter of the Bone(dfs + 奇偶性剪枝)
- hdu 1010 Tempter of the Bone (dfs+剪枝)
- hdu 1010 Tempter of the Bone(DFS + 奇偶剪枝 + 路径剪枝)
- HDU 1010 Tempter of the Bone(逃离地狱,DFS,剪枝,错题集)
- HDU 1010 Tempter of the Bone【dfs剪枝】
- HDU-1010-Tempter of the Bone【DFS剪枝】
- HDU - 1010(Tempter of the Bone)(dfs+剪枝)
- HDU 1010 Tempter of the Bone DFS基础剪枝题 被剪枝伤害到了 好烦
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- HDU_(1010) Tempter of the Bone(DFS,剪枝)
- HDUOJ-1010 Tempter of the Bone(深搜+剪枝优化)
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- HDU 1010 Tempter of the Bone (DFS+剪枝)
- HDU-1010 Tempter of the Bone (dfs+剪枝)
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- HDU 1010 Tempter of the Bone(DFS回溯+剪枝)
- hdu 1010 Tempter of the Bone DFS+奇偶性剪枝
- hdu 1010 Tempter of the Bone (dfs+剪枝)