小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里。
经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t秒大门会为你敞开”,而他自己所在的棋盘是大小为 N*M 的长方形,他可以向上下左右四个方向移动(不可走有障碍点)。棋盘中有一扇门。根据机关的提示,小明顿时明白了,他和朋友必须在第 t 秒到门口。而这一切,没有回头路!因为一旦他移动了,他刚才所在的点就会消失,并且他不能在一个点上停留超过一秒,不然格子会爆炸。大逃亡开始了,请问小明和朋友能安全的逃出这奇怪的棋盘吗?

Input

输入多组测试数据。每个测试用例的第一行包含三个整数 N、M 和 T ( 1 < N , M < 7 ; 0 < T < 50 ),分别表示棋盘的大小和门打开的时间。接下来的N行给出棋盘布局,每一行包含M个字符。其中
".": 无障碍点
"X": 障碍点
"S": 起点
"D": 门

输入以 3 个 0 结束。这个测试用例不需要处理。

Output

对于每组样例输出一行。
如果小明能够安全逃出,输出 "YES" ,否则输出 "NO"。

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES 代码:
import java.util.Arrays;
import java.util.Scanner; public class Main{
static int n,m,T;
static final int N=10;
static int sx,sy,ex,ey;
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static char map[][]=new char[N][N];
static boolean vis[][]=new boolean[N][N];
static boolean flag; static void dfs(int x,int y,int t){
          //这种多if判断的一定加上return
if(flag) return; //已经找到一种走法
if(t>T) return; //大于要求的时间
if(T-t-Math.abs(x-ex)-Math.abs(y-ey)<0) return; //剩余的步数不足以到达终点
if((T-t)%2!=(Math.abs(x-ex)+Math.abs(y-ey))%2) return; //奇偶性一致
if(t==T && x==ex && y==ey){
flag=true;
} for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0 || yy<0 || xx>=n || yy>=m ||vis[xx][yy]) continue;
vis[xx][yy]=true;
dfs(xx,yy,t+1);
vis[xx][yy]=false;
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
n=scan.nextInt();
m=scan.nextInt();
T=scan.nextInt();
if(n==0 && m==0 &&T==0) break;
for(int i=0;i<n;i++) map[i]=scan.next().toCharArray();
for(int i=0;i<N;i++) Arrays.fill(vis[i],false);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='S'){
sx=i; sy=j;
}
else if(map[i][j]=='D'){
ex=i; ey=j;
}
else if(map[i][j]=='X')
vis[i][j]=true;
vis[sx][sy]=true;
flag=false;
dfs(sx,sy,0);
if(flag) System.out.println("YES");
else System.out.println("NO");
}
}
}

HDU1010 --- Tempter of the Bone(dfs+剪枝)的更多相关文章

  1. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  2. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  3. Tempter of the Bone dfs+剪枝

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  4. B - Tempter of the Bone(DFS+剪枝)

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  5. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  6. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  7. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. C#中 ref 关键字的认识和理解

    之前接手老项目的时候有遇到一些的方法参数中使用了ref关键字加在传参的参数前面的情况.对于新手,这里介绍和讲解一下ref的用法和实际效果. CLR中默认所有方法的参数传递方式都是传值,也就是说不管你传 ...

  2. MySql 部分字段去重

    select * from personal_question_answer where answer_id in ( select min(answer_id) from personal_ques ...

  3. iMacros 入门教程-基础函数介绍(4)

    imacros的TRAY函数用法 这个函数的功能就是隐藏或显示,当执行imacros文件的时候,出现在特定标签的imacros图标 TRAY HIDE 就是隐藏图标 TRAY SHOW 就是显示图标 ...

  4. 【python基础语法】第7天作业练习题

    import keyword ''' # 第一题:简单题 1.什么是全局变量? 2.什么是局部变量? 3.函数内部如何修改全局变量(如何声明全局变量 )? 4.写出已经学过的所有python关键字,分 ...

  5. 『后缀自动机入门 SuffixAutomaton』

    本文的图片材料多数来自\(\mathrm{hihocoder}\)中详尽的\(SAM\)介绍,文字总结为原创内容. 确定性有限状态自动机 DFA 首先我们要定义确定性有限状态自动机\(\mathrm{ ...

  6. W25Q64BV(FLASH)(SPI)中文手册

    64兆位串行SPI FLASH存储器 1.常规介绍 W25Q64BV(64兆位)串行FLASH存储器为一个空间大小,引脚,功耗限制的系统提供解决方案.25Q系列的灵活性和性能良好超越了普通的串行FLA ...

  7. 剑指offer-面试题42-连续子数组的最大和-动态规划

    /*题目; 输入一个整形数组(可能有正数和负数),求数组中连续子数组(最少有一个元素)的最大和. 要求时间复杂度为O(n). 先输入数组的格式,再依次输入数组的值.*//*思路: f(i) = pDa ...

  8. 适合小白的大白话讲解--->Git与Github的区别

    本文由 伯乐在线 - 听风 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Red Radger.欢迎加入翻译组. 本文旨在使用通俗易懂的文字,讲解版本控制背后的理论,以便你能对程序员们如何工作有个 ...

  9. sqlserver数据库重启

    停止:net stop mssqlserver 重启:net start mssqlserver

  10. BFS和队列

    深度优先搜索(DFS)和广度优先搜索(BFS)是基本的暴力技术,常用于解决图.树的遍历问题. 首先考虑算法思路.以老鼠走迷宫为例: (1):一只老鼠走迷宫.它在每个路口都选择先走右边,直到碰壁无法继续 ...