hdu 1010 Tempter of the Bone (dfs + 剪枝)
来源:互联网 发布:办卡软件 编辑:程序博客网 时间:2024/06/05 15:28
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
从S出发每次只能移动一格,且不能走回头路,问能否恰好经过t步到达D
有一种很强的剪枝办法,就是判断在每个位置时距终点的最短步数的奇偶性与剩余步数的奇偶性是否相同,如果相同就继续搜索,否则返回;
#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;const int M = 10;char mapp[M][M];bool book[M][M];int sx, sy, dx, dy, n, m, t;int cnt;bool f;int mv[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};void dfs(int x, int y){ if(f) return; int dis = abs(dx-x)+abs(dy-y);//求出最短步数 if(dis%2!=(t-cnt)%2)//判断奇偶 return; if(x==dx&&y==dy) { if(cnt==t) f = true; return; } int tx, ty; for(int i=0;i<4;i++) { tx = x + mv[i][0]; ty = y + mv[i][1]; if(tx<1||tx>n||ty<1||ty>m) continue; if(mapp[tx][ty]!='X'&&!book[tx][ty]) { book[tx][ty] = true; cnt++; dfs(tx, ty); cnt--; book[tx][ty] = false; } }}int main(){ while(scanf("%d%d%d", &n, &m, &t)&&(n||m||t)) { f = false; memset(book, false, sizeof(book)); cnt = 0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c", &mapp[i][j]); if(mapp[i][j]=='S') { sx = i; sy = j; } else if(mapp[i][j]=='D') { dx = i; dy = j; } } book[sx][sy] = true; dfs(sx, sy); if(f) printf("YES\n"); else printf("NO\n"); } 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 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 + 奇偶剪枝)
- hdu 1010 Tempter of the Bone dfs+剪枝
- 瓦片自定义导出参数说明
- Python网络爬虫之BeautifulSoup库
- Android中的获取控件矩阵gethitrect方法
- Java JSON 标准 JSR-353 相关
- 常用正则----特殊表达式
- hdu 1010 Tempter of the Bone (dfs + 剪枝)
- C++逆波兰表达式转化实现简单计算器
- POI设置word 2007文本下划线样式及文字底纹
- 数据表示——进制的转换
- js原生alert弹框美化
- 170727 逆向-RousterScan(3)
- UNP TCP连接管理
- Oracle 11gR2 RAC 使用scan IP无法连接数据库(ORA-12545)
- mysql兼容emoj表情