Tempter of the Bone HDU
来源:互联网 发布:大数据需要掌握的技术 编辑:程序博客网 时间:2024/05/21 10:27
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.
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.
'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.
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
NOYES
题意很明确问你能否在时刻t恰好到达门口,过程中走过的点不能再走.
思路: 超时了很多次,百度了才知道有一种方法叫奇偶性剪枝,推荐博客:点击打开链接.
先说一下奇偶性剪枝:
图片说的很明确了,当然这道题目需要转化一下.
当然这里面还有一些细节要自己考虑一下.
上代码:
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int n,m,t;int ex,ey;int s[10][10];int vis[10][10];// 标记那些点不能走 int nex[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; // 四个方向 bool dfs(int x, int y, int tmp){if(tmp > t)return false;if(tmp != t && s[x][y] == 'D')return false;if(tmp == t && s[x][y]=='D')return true;if(tmp == t && s[x][y]!='D')return false; // 剪枝 int left = t - tmp; // 剩余的时间 int maxn = abs(ex-x) + abs(ey-y); // 剩余需要走的步数 if(left < maxn) return false; // 如果剩余的时间小于至少需要走的步数,那么剪枝 if(abs(maxn-left) %2 == 1)return false; // 根据奇偶行剪枝 for(int i=0; i<4; i++) { int xx = x + nex[i][0]; int yy = y + nex[i][1]; if(xx>=1 &&xx<=m && yy>=1 && yy <=n && vis[xx][yy]==0 && s[xx][yy]!='X') { vis[xx][yy] = 1;if(dfs(xx,yy,tmp+1))return true;vis[xx][yy] = 0;// 回溯 }}return false;}int main(){while(scanf("%d %d %d", &m,&n, &t)!= EOF){ if(m+n+t==0)break; memset(vis, 0, sizeof(vis)); int x,y; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { scanf(" %c", &s[i][j]);if(s[i][j]=='S'){ x=i; y=j;}if(s[i][j]=='D'){ex=i; ey=j;} } } vis[x][y] = 1; // 起点只能走一次,所以以后是不可能走起点了 if(dfs(x,y,0))cout << "YES" << endl;else cout << "NO" << endl;}return 0;}
水波.
阅读全文
0 0
- [hdu]Tempter of the Bone
- HDU Tempter of the Bone
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- HDU 1010 Tempter of the Bone
- HDU 2128 Tempter of the Bone II
- 迷宫 hdu 1010 Tempter of the Bone
- HDU 1786 Tempter of the Bone again
- HDU 1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone(深搜)
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing
- 阿里云服务器linux(centos)常用命令
- mongodb--java连接数据库实现增删改查
- 【Linux】学会 core dump 事后调试 快速定位段错误
- Maven编译报错[ERROR] Fatal Error: Unable to find package java.lang in classpath or bootclasspath
- Tempter of the Bone HDU
- nmap搭建及命令简介
- 习题5-9 找bug(Bug Hunt, UVa1596)
- mysql 存储过程 游标
- 创建、编译ros package
- WebView里面调用打电话
- lintcode 整数排序 | 希尔排序算法(Java)
- 斐波那契数列的余数周期问题
- SSH之传统方式接入分布式会话(Spring Session)篇