HDOJ HDU 1010 Tempter of the Bone
来源:互联网 发布:snh48是什么玩意知乎 编辑:程序博客网 时间:2024/06/05 19:56
HDOJ 1010 Tempter of the Bone
题目
点击查看 HDOJ 1010 Tempter of the Bone
题意
小狗找到骨头发现陷入了迷宫,狗是否能逃出迷宫
迷宫大小 N 行 M 列 出口的门仅在T秒时打开迷宫元素
- ‘x’ 墙,小狗不能进入;
- ‘S’ 小狗的起始点
- ‘D’ 门
- ‘.’ 空快,小狗可以行走
题解
迷宫问题
dfs遍历即可
本题不同的是门只开1s
所以dfs时,时间超过的直接剪枝即可
但是这样解有时会超时
技巧
剪枝技巧
奇偶剪枝
原理解释
先看这个5 * 5的矩阵
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
从任意 0 走到任意 1 始终是奇数步
0 走到任意 0 始终是偶数步所以 设步数为 t ,起点坐标为(sx, sy),终点坐标为(ex, ey),则:
t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达部分摘自百度百科 奇偶剪枝
代码
#include <iostream>#include <cstdlib>#include <cstring>#define maxrlen 8using namespace std;char map[maxrlen][maxrlen];bool vis[maxrlen][maxrlen]; int r,c,dx,dy;int d[4][2] = {1,0,0,1,-1,0,0,-1};bool dfs(int x,int y,int t);int main(){ int t,sx,sy; while(cin >> r >> c >> t &&(r || c || t)) { memset(vis,false,sizeof(vis)); if(r == 0 || c == 0) { cout << "NO" << endl; continue; } if(t == 0) { cout << "NO" << endl; continue; } for(int i =0;i < r;i++) for(int j =0;j < c;j++) { cin >> map[i][j]; if(map[i][j] == 'S') { sx = i; sy = j; } if(map[i][j] == 'D') { dx = i; dy = j; } } vis[sx][sy] = true; dfs(sx,sy,t) ? cout << "YES" << endl : cout << "NO" << endl; } return 0;}bool dfs(int x,int y,int t){ if(map[x][y] == 'D' && t == 0) return true; int z = (t - abs(dx - x) + abs(dy - y)) % 2; if(z < 0 || z != 0) return false;// 奇偶剪枝 if(t <= 0) return false;// 时间不够剪枝 for(int i = 0;i < 4;i++) { if(x + d[i][0] < 0 || x + d[i][0] >= r || y + d[i][1] < 0 || y + d[i][1] >= c) continue; if(map[x + d[i][0]][y + d[i][1]] == 'X') continue; if(!vis[x + d[i][0]][y + d[i][1]]) { vis[x + d[i][0]][y + d[i][1]] = true; if(dfs(x + d[i][0],y + d[i][1],t-1)) return true; vis[x + d[i][0]][y + d[i][1]] = false; } } return false;}
阅读全文
0 0
- HDOJ HDU 1010 Tempter of the Bone
- hdoj 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- ACM HDOJ 1010 (Tempter of the Bone)
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDoj-1010-Tempter of the Bone-DFS
- 【HDOJ 1010】Tempter of the Bone
- hdoj 1010 Tempter of the Bone
- hdoj 1010Tempter of the Bone【DFS】
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010-Tempter of the Bone【dfs】
- HDOJ 1010 Tempter of the Bone
- HDOJ.1010 Tempter of the Bone (DFS)
- hdoj 1010 Tempter of the Bone
- HDU 6140 Hybrid Crystals(坑题+水题)
- 《ThinkPHP5.0+小程序商城构建全栈应用》视频教程下载
- Hyperledger Fabric译文之Membership Service Providers (MSP)
- 硬件系统工程师宝典-目录
- android 按下返回键 一段时间内两次点击结束程序
- HDOJ HDU 1010 Tempter of the Bone
- MVP的实现,后续更新mvp和databinding结合
- ubuntu安装mysql
- H5前端性能测试点及优化方法
- windows下c++如何读取主机名
- HDU 2665 / POJ 2104 静态区间第k小 主席树
- 【学习笔记】你不得不知道的跨域小知识
- Centos5和6 启动流程和故障恢复
- C++拷贝构造函数详解