hdu 1010
来源:互联网 发布:mac mini重装系统教程 编辑:程序博客网 时间:2024/06/05 05:46
hdu 1010 Tempter of the Bone(骨头的诱惑)
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
此题我一开始用了bfs,发现不对。(bfs还是做最短路径比较好点~~~)
后来就用dfs+奇偶剪枝
//1010 dfs+剪枝 #include<iostream>#include<queue>#include<cstring>#include<cmath>using namespace std;int visited[7][7];int nexted[4][2]={0,1,1,0,-1,0,0,-1};char a;struct Node{ int col; int row;};Node beg,en;int flag=0;int t;int n,m;void dfs(int x,int y,int count){ int x1,y1; if(x==en.col&&y==en.row)//是y { //cout<<count<<" "; if(t==count) flag=1; return; } if(count>=t) return; // if(visited[x][y]!=1) // { for(int i=0;i<4;i++) { x1=x+nexted[i][0]; y1=y+nexted[i][1]; if(x1>=0&&y1>=0&&x1<n&&y1<m&&!visited[x1][y1]) { visited[x1][y1]=1; dfs(x1,y1,count+1); visited[x1][y1]=0; if(flag) return; } } // } return; }int main(){while(scanf("%d%d%d",&n,&m,&t)==3) { flag=0; if(n==0&&m==0&&t==0) break; memset(visited,0,sizeof(visited)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>a; if(a=='X'){ visited[i][j]=1; } if(a=='S') { beg.col=i; beg.row=j; visited[i][j]=1; } if(a=='D'){ en.col=i; en.row=j; } } } if(abs(beg.col-en.col)+abs(beg.row-en.row)>t||(t-abs(beg.col-en.col)+abs(beg.row-en.row))%2==1) //剪枝 { printf("NO\n"); continue; } dfs(beg.col,beg.row,0); if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
- HDU 1010
- HDU 1010
- hdu 1010
- hdu 1010
- HDU 1010
- HDU 1010
- hdu 1010
- hdu-1010
- hdu 1010
- hdu 1010
- HDU 1010
- hdu 1010
- hdu 1010
- hdu-1010
- hdu-1010
- hdu 1010
- HDU 1010
- (1010)HDU
- myeclipse 10 自带的浏览器弹出脚本错误解决办法
- mysql按照天分区月表
- 文章标题
- windows重启方面的文章
- 53. Maximum Subarray 最大子串和问题
- hdu 1010
- Java语言概述,JDK的下载安装及环境配置
- 利用Unix系统函数进行网络编程(一)
- html链接范例
- Java反射机制(Reflect)
- gulp自动化开发入门指南
- iOS 中获取某个视图的截图
- c语言 数组定义 符号常量定义
- AOP和IOC的实现原理(用到的设计模式)