HDU 1010 —— Tempter of the Bone DFS+剪枝
来源:互联网 发布:淘宝网卖家版怎么下载 编辑:程序博客网 时间:2024/05/03 15:30
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意:从S走到D,其中X是墙,不能走;给定时间 t ,问能否从S到D正好花费 t 时间(一步即为一秒);
思路:用DFS+剪枝;
此处有两种剪枝:
1、从S到D的最短时间是 tmp = abs(sx-dx)+abs(sy-dy),如果 tmp > t,显然是“NO”;
2、如果 tmp <= t,那么我们要走到D就要往回走几步,如果你往左多走了一步,那么势必就要往右也走一步,才能又回到D;所以剩余步数 cnt = t - tmp 就一定是偶数;
上述的第2种剪枝方法叫做“奇偶性剪枝”;
#include<bits/stdc++.h>using namespace std;const int maxn = 10;int n, m, t;string Map[maxn];int sx, sy, dx, dy;bool flag;bool vis[maxn][maxn];void DFS(int x, int y, int step){ if(x < 0 || x >= n || y < 0 || y >= m) return; if(x == dx && y == dy){ if(step == t) flag = true; return; } if(x-1 >= 0 && Map[x-1][y] != 'X' && !vis[x-1][y]){ vis[x-1][y] = true; DFS(x-1, y, step+1); vis[x-1][y] = false; } if(flag) return; if(Map[x+1][y] != 'X' && x+1 < n && !vis[x+1][y]){ vis[x+1][y] = true; DFS(x+1, y, step+1); vis[x+1][y] = false; } if(flag) return; if(y-1 >= 0 && Map[x][y-1] != 'X' && !vis[x][y-1]){ vis[x][y-1] = true; DFS(x, y-1, step+1); vis[x][y-1] = false; } if(flag) return; if(Map[x][y+1] != 'X' && y+1 < m && !vis[x][y+1]){ vis[x][y+1] = true; DFS(x, y+1, step+1); vis[x][y+1] = false; } return;}int main(){ while(cin>>n>>m>>t){ memset(vis, false, sizeof vis); if(n == 0 && m == 0 && t == 0) break; for(int i = 0;i<n;i++){ cin>>Map[i]; for(int j = 0;j<m;j++){ if(Map[i][j] == 'S') sx = i, sy = j; if(Map[i][j] == 'D') dx = i, dy = j; } } int tmp = abs(sx-dx)+abs(sy-dy); if(tmp > t || (t-tmp) % 2 != 0){ cout<<"NO"<<endl; continue; } flag = false; vis[sx][sy] = true; DFS(sx, sy, 0); if(flag) 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+经典奇偶剪枝]
- HDU 1010Tempter 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 (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 + 奇偶剪枝)
- 微信小程序之toast等弹框提示
- mysql自动定时备份数据库的最佳方法-支持windows系统
- ckplayer快速入门
- 汉诺塔问题
- 以域管理账户连接到TFS或git时,设置IE允许Cookies
- HDU 1010 —— Tempter of the Bone DFS+剪枝
- notes 鸟哥-鸟哥的Linux私房菜 服务器架设篇 3ed
- CORS——跨域请求那些事儿
- 坚持不懈的事例
- 【linux】Linux压缩打包命令
- python数据分析准备(安装python和canopy用于科学计算)
- adb通过wifi或usb方式完成调试
- SAP中Product Orders(生产订单)相关函数BAPI
- MapReduce 倒排索引的实现