hdu 1010 Tempter of the Bone(dfs+枝剪)
来源:互联网 发布:农村淘宝怎么解绑 编辑:程序博客网 时间:2024/06/03 20:47
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意:n*m的地图,规定恰好在第t步到达终点'D',起点是‘S’,墙是‘X’
思路:还是走迷宫的题,每次上下左右走,暴力所有情况,找到恰好是t步的路径,每次回溯再回到初始状态。
但还有一个奇偶枝剪,意思就是要求的最短路径是|ex-sx|+|ey-sy|,若要到达终点,必须多走偶数步。
什么奇偶枝剪,在main里判断一下即可。
#include<bits/stdc++.h>using namespace std;int n,m,t,sx,sy,ex,ey,xnum;bool visit[10][10],flg;char mp[10][10];const int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};void dfs(int x, int y, int step) { if(flg) return; //一种情况即可 if(step == t && x == ex && y == ey) { flg=true; return; } if(step >= t) return; //搜索的路径大于t时返回 //int dis = abs((double)x-ex) + abs((double)y-ey); //dis = t-dis-step; //if(dis<0 || dis%2) return;//奇偶剪枝 for( int i=0; i<4; ++i) { int tx = x+dir[i][0]; int ty = y+dir[i][1]; int tstep = step+1; if(tx>=0 &&tx<n &&ty>=0 &&ty<m &&!visit[tx][ty] &&mp[tx][ty] != 'X') { visit[tx][ty] = true; dfs(tx,ty,tstep); visit[tx][ty] = false;//回溯 } } return;}int main(){ while(scanf("%d%d%d",&n,&m,&t)&&(n+m+t)){ xnum=0; flg=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ cin>>mp[i][j]; visit[i][j]=false; if(mp[i][j]=='S'){ visit[i][j]=true; sx=i; sy=j; } if(mp[i][j]=='D'){ ex=i;ey=j; } if(mp[i][j]=='X') ++xnum; } int dis = abs((double)sx-ex) + abs((double)sy-ey); if(t%2==0&&dis%2==1|| t%2==1&&dis%2==0){ cout<<"NO"<<endl; continue; } if(n*m-xnum>t) //这点也很着重要,如果可走的步数比赛t还小直接pass dfs(sx,sy,0); if(flg) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
阅读全文
0 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)
- dfs hdu 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(DFS)
- hdu 1010 Tempter of the Bone dfs
- hdu 1010Tempter of the Bone(DFS)
- HDU-1010-Tempter of the Bone【DFS】
- hdu-1010Tempter of the Bone-dfs
- hdu 1010 Tempter of the Bone (dfs)
- 网络基础知识
- 阿里云主机用QQ邮箱发送邮件
- UIStepper
- css input[type=file] 样式美化,input上传按钮美化
- 关于ubuntu16.04的几个免密操作
- hdu 1010 Tempter of the Bone(dfs+枝剪)
- 实验4 无向图的深度优先搜索
- C++ 指针数组 & 指向数组的指针
- Linux下安装Zookeeper
- (转) char c=128;printf("%d",c);问题
- Freso的图片缓存
- Android动画解析(一)-----逐帧动画和补间动画
- 文本批量处理方法大全
- Hibernate Validation使用示例及讲解