杭电 HDU 1010 Tempter of the Bone
来源:互联网 发布:淘宝卖家子账号设置 编辑:程序博客网 时间:2024/05/16 09:09
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1010
问题:在 N*M的迷宫里,小狗要在‘S’的位置,要再第T秒到达出口 'D' (1 < N, M < 7; 0 < T < 50)。 即求存在路径,果断深搜DFS。
限制条件及剪枝:
1、走过的地方 ‘ . ’不能再走
2、不能通过墙‘X’
3、不能离开地图范围
4、可根据狗到门的最短距离和时间相比较,当时间与最短步骤都为奇数或偶数时,才有可能到达门处,否则永远无法正好在T秒到达门处。(奇偶剪枝,能去掉很多情况,减少时间)
5、找到任何一条存在路径后即可结束搜索并输出。(减少多余的搜索)
一开始第四点没有想到,总是超时,估计没有想到第四点的绝大多数都会超时的。
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;struct Node{ int x,y; int time;};char map[10][10];int t;bool yorn;int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};void Dfs(Node now){ // printf("%d %d %d %d %c\n",now.x,now.y,now.time,yorn,map[now.x][now.y]); Node next; char ch; int i; if(yorn) { return ; } if(now.time > t) { return ; } if(now.time == t) { if(map[now.x][now.y] == 'D') { yorn = 1; } return ; } for(i = 0; i < 4; i++) { next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; next.time = now.time+1; if(map[next.x][next.y] != 'X') { if(map[next.x][next.y] == '.') { map[next.x][next.y] = 'X'; Dfs(next); map[next.x][next.y] = '.'; } else { Dfs(next); } } }}int main(){ int n,m,p; int i,j,x,y; Node now; while(scanf("%d%d%d",&n,&m,&t)) { if(n == 0 && m == 0 && t == 0) { break; } memset(map,'X',sizeof(map)); //初始化都为墙 yorn = 0; for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { cin >> map[i][j]; if(map[i][j] == 'S') { now.x = i; now.y = j; } if(map[i][j] == 'D') { x = i; y = j; } } } now.time = 0; p = abs(now.x-x) + abs(now.y-y); map[now.x][now.y] = 'X';// printf("%d\n",yorn); if(p%2 == t%2) //时间和需要走的步骤都为奇数或偶数则搜索(奇偶剪枝) { Dfs(now); } // printf("%d\n",yorn); if(yorn > 0) { printf("YES\n"); } else { printf("NO\n"); } } return 0;}
- 杭电 HDU 1010 Tempter of the Bone
- 杭电(hdu)ACM 1010 Tempter of the Bone
- 杭电1010-Tempter of the Bone
- 杭电 Tempter of the Bone 1010
- 杭电 1010 Tempter of the Bone
- 杭电1010---Tempter of the Bone
- 杭电 1010 Tempter of the Bone
- 【杭电】[1010]Tempter of the Bone
- 杭电1010 Tempter of the Bone
- 杭电 HDU ACM 1046 Tempter of the Bone
- 杭电acm1010 Tempter of the Bone
- 杭电1010Tempter of the Bone(搜索)
- 杭电OJ:1010 Tempter of the Bone
- 杭电1010 Tempter of the Bone(奇偶性剪枝)
- 杭电1010-Tempter of the Bone(BFS)
- C - Tempter of the Bone 杭电1010
- 杭电-1010 Tempter of the Bone(深搜)
- 杭电ACM 1010 Tempter of the Bone
- 我的年计划
- 0-1背包解析[以NYoj 289 苹果 为例]
- HDU1284钱币兑换问题( 母函数打表)
- 从零开始学C++之IO流类库(三):文件的读写、二进制文件的读写、文件随机读写
- unity3D入门系列十三,如何制作天空盒(SkyBox)(英文)
- 杭电 HDU 1010 Tempter of the Bone
- Oracle Database 11g Release 2 RAC On Oracle Linux 5.8 Using VirtualBox
- Trie树的双数组实现
- v6eval编译问题
- hdu1596 find the safest road
- String和StringBuffer的区别
- UVa 11524 - In-Circle
- hdu2444 判断二分图+最大匹配
- 认清营销、销售和广告的庐山真面目