hdu 1010 Tempter of the Bone(DFS)
来源:互联网 发布:网络控制器感叹号 编辑:程序博客网 时间:2024/06/05 01:58
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
题 意:从S处出发看是否能在刚好t秒时间时到达D处
思 路:从S开始进行DFS遍历
注:
奇偶剪枝:
是数据结构的搜索中,剪枝的一种特殊小技巧。
现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,
s | | | +———e
如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;
s——— ——+ |+ | +———e
如图,为一般情况下非最短路径的任意走法举例,step2=14;
step2-step1=6,偏移路径为6,偶数(易证);
故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;
返回,false;
反之亦反。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <queue>#include <algorithm>int vis[8][8];char a[8][8];int n, m, t, x1, x2, y3, y2,ans;int dx[4]= {1,-1,0,0},dy[4]= {0,0,1,-1};void dfs( int x, int y, int time ){ if(ans) return ; if( time == t ) { if( x == x2 && y == y2 ) ans = 1; return ; } if ( t < time ) return; int temp = abs(x-x2)+abs(y-y2)-abs(time - t); //奇偶性剪枝 ,起点和终点确定以后就可以确定走的步数是奇数还是偶数,通过这个特点来剪枝 if(temp>0||temp&1) return; for ( int i = 0; i < 4; i ++ ) { int xx = x+dx[i]; int yy = y+dy[i]; if( xx >= 0 && xx < n && yy >= 0 && yy < m && a[xx][yy] != 'X' ) { a[xx][yy]='X'; dfs(xx,yy,time+1); a[xx][yy]='.'; } }}int main(){ while( scanf ( "%d %d %d", &n, &m, &t ) != EOF ) { int w=0; if( n == 0 && m == 0 && t == 0 ) break; for( int i = 0; i < n; i ++ ) for( int j = 0; j < m; j ++ ) { scanf ( " %c", &a[i][j] ); if( a[i][j] == 'S' ) { x1=i; y3=j; } if( a[i][j] == 'D' ) { x2=i; y2=j; } if( a[i][j] == '.' ) w++; } if( w+1<t )//到达D处也要时间,所以要加一,否则会错 { printf("NO\n"); continue; } memset( vis,0,sizeof(vis)); ans=0; a[x1][y3]='X'; dfs(x1,y3,0); if( ans ) 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)
- dfs hdu 1010 Tempter of the Bone
- 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-1010Tempter of the Bone-dfs
- hdu 1010 Tempter of the Bone (dfs)
- HDU 1010 Tempter of the Bone(dfs)
- Java深入 - Spring 异常处理HandlerExceptionResolver
- Palindrome Partitioning II
- 数据结构之线索二叉树
- 转:nutch相干框架安装使用最佳指南
- 25-沙盒 文件管理者
- hdu 1010 Tempter of the Bone(DFS)
- HDU 1548 A strange lift
- Unity3d角色控制器Character Controller
- 欢迎使用CSDN-markdown编辑器
- Java内存区域与内存溢出异常
- MyEclipse连接MySQL数据库
- 数据结构之还没有解决的任务
- 二叉树的三种迭代遍历方法
- 26-JSON解析