hdu1010 Tempter of the Bone
来源:互联网 发布:维生素 抑郁症 知乎 编辑:程序博客网 时间:2024/05/14 07:43
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间。S为起点,D为终点。并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷。所以你必须每秒走一步,且到D点时,所用时间为T。用深搜。
之前,误以为就是找到最短路,结果不是,而是要看是否能在所有可能的路中找到刚好满足条件的路程,后来,单用深搜,超时,所以,后来的剪枝非常重要!!!(这道题还是要多看!!!!!)
代码:#include <iostream>#include <cstdio>#include <cstring>using namespace std;int xx[4]={0,1,0,-1};int yy[4]={1,0,-1,0};char map[10][10];void init(){ int i,j; for(i=0;i<10;i++) { for(j=0;j<10;j++) map[i][j]='X'; //X是狗不能过去的。。初始化为X是为了防止出界 }}int s1,s2,e1,e2,t,flag=0;void DFS(int s1,int s2,int time){ if(flag==1) return ; if(s1==e1&&s2==e2) { if(time==t) { flag=1; } return ; } int nowx,nowy,i; for(i=0;i<4;i++) { nowx=s1+xx[i]; nowy=s2+yy[i]; if(map[nowx][nowy]!='X') { time++; map[nowx][nowy]='X'; DFS(nowx,nowy,time); //没有找到目标点(.)就会返回这里 map[nowx][nowy]='.';//然后重新开始 time--; //然后就会减1 } }}int main(){ int n,m; while(~scanf("%d %d %d",&n,&m,&t)&&n!=0&&m!=0&&t!=0) { init(); int i,j,time=0,count=0; getchar(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cin>>map[i][j]; if(map[i][j]=='D') //能出去的门(狗最后在的地方)(剪枝) e1=i,e2=j; else if(map[i][j]=='S') //狗开始在的地方 s1=i,s2=j; else if(map[i][j]=='.') //狗可以走的地方 count++; } } if(count+1<t) //小于规定时间,就不能存活。。 { cout<<"NO"<<endl; continue; } flag=0; map[s1][s2]='X'; DFS(s1,s2,time); if(flag==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
0 0
- HDU1010 Tempter of the Bone
- Tempter of the Bone hdu1010
- (hdu1010)Tempter of the Bone
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010:Tempter of the Bone
- HDU1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010(Tempter of the Bone)
- hdu1010 Tempter of the Bone
- HDU1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone
- hdu1010---Tempter of the Bone
- HDU1010---Tempter of the Bone
- hdu1010 Tempter of the Bone
- html显示一个长的错误单词换行,内容自动换行
- 入木三分学网络第一篇--VRRP协议详解-----(2)
- adb修改hosts方法
- linux 进程管理-----pid哈希链表
- C++内存分配方式详解
- hdu1010 Tempter of the Bone
- 二叉树深度广度遍历
- jQuery ajax - ajax() 方法
- c语言的发展史及特点
- 【转】生命中的最后一天
- 数据归一化和两种常用的归一化方法
- LXC之三—创建模板
- 『黑马程序员』第十话<基础加强01>
- 左浮动菜单