HDU1010 Tempter of the Bone (DFS入门)
2017-09-20 14:25
477 查看
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129718 Accepted Submission(s): 35018
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
#include <stdio.h> #include <stdlib.h> int di,dj,n,m,t; int map[10][10]; int flag=0; void dfs(int si,int sj,int cnt) { if(si<=0||sj<=0||si>n||sj>m)return; if(si==di&&sj==dj&&cnt==t)flag=1; if(flag)return; if((t-cnt)%2==(abs(si-di)+abs(sj-dj))%2) if(map[si+1][sj]!='X') { map[si+1][sj]='X'; dfs(si+1,sj,cnt+1); map[si+1][sj]='.'; } if(map[si-1][sj]!='X') { map[si-1][sj]='X'; dfs(si-1,sj,cnt+1); map[si-1][sj]='.'; } if(map[si][sj+1]!='X') { map[si][sj+1]='X'; dfs(si,sj+1,cnt+1); map[si][sj+1]='.'; } if(map[si][sj-1]!='X') { map[si][sj-1]='X'; dfs(si,sj-1,cnt+1); map[si][sj-1]='.'; } return; } int main(int argc, char *argv[]) { while(scanf("%d %d %d",&n,&m,&t),n+m+t) { getchar(); int i,j,si,sj; int wall=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { si=i; sj=j; } if(map[i][j]=='D') { di=i; dj=j; } if(map[i][j]=='X') { wall++; } } getchar(); } if(n*m-wall<=t) { printf("NO\n"); continue; } map[si][sj]='X'; flag=0; dfs(si,sj,0); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }DFS搜索入门,本来是直接从当前的点直接四方搜索,结果错了,原来判定条件map[si][sj]!=‘X’出错,应该是从下一个非X得点开始搜索所以过了
2.0版本
#include <stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; char map[100][100]; int n,m; int num; bool judge; int di,dj; int step[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; void dfs(int x,int y,int cnt) { if(x<=0||y<=0||x>n||y>m) return ; if(judge)return ; if(x==di&&y==dj&&cnt==num) { judge=1; return ; } int i; if((num-cnt)%2==(abs(x-di)+abs(y-dj))%2) { for(i=0;i<4;i++) { if(map[x+step[i][0]][y+step[i][1]]!='X') { map[x+step[i][0]][y+step[i][1]]='X'; dfs(x+step[i][0],y+step[i][1],cnt+1); map[x+step[i][0]][y+step[i][1]]='.'; } } } return ; } int main(int argc, char *argv[]) { while(scanf("%d %d %d",&n,&m,&num),n+m+num) { int i,j,si,sj; int max=0; getchar(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='X') max++; if(map[i][j]=='D') { di=i; dj=j; } if(map[i][j]=='S') { si=i; sj=j; } } getchar(); } if(n*m-max<=num) { printf("NO\n"); } else { map[si][sj]='X'; judge=0; dfs(si,sj,0); if(judge==0) printf("NO\n"); else printf("YES\n"); } } return 0; }
相关文章推荐
- hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
- HDU1010:Tempter of the Bone(DFS)
- 【搜索-DFS】hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone (DFS & 奇偶剪枝)
- 【hdu1010】Tempter of the Bone——dfs
- hdu1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
- hdu1010 Tempter of the Bone(DFS)
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- HDU1010:Tempter of the Bone(DFS)
- hdu1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU1010-Tempter of the Bone(DFS+各种剪枝)
- HDU1010_Tempter of the Bone(DFS+奇偶剪枝+回溯)
- hdu1010 Tempter of the Bone(dfs)
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
- hdu1010 Tempter of the Bone(dfs) java
- hdu1010 Tempter of the Bone(dfs)
- hdu1010 Tempter of the Bone (DFS)
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- HDU1010 Tempter of the Bone(搜索,dfs)