题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

题目大意:

  输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四类元素组成.

  S'代表是开始位置; 'D'表示结束位置;'.'表示可以走的路;'X'表示是墙。

  问:从‘S’  能否在第 t 步 正好走到 'D'.

解题思路:

   平常心态做dfs即可,稍微加个奇偶剪枝,第一次做没经验,做过一次下次就知道怎么做了。最后有代码注释解析。

AC Code:

 #include<bits/stdc++.h>
using namespace std;
int n,m,t;
int doorX,doorY;
char ca[][];
int to[][] = {{,-},{,},{-,},{,}};
int dfs(int x,int y,int cnt)
{
if(x>n||y>m||x<=||y<=)return ;
if(cnt==t&&x==doorX&&y==doorY)
return ;
int tem=t-cnt-abs(x-doorX)-abs(y-doorY);
if(tem< || tem& )return ;
for(int i=; i<; i++)
{
if(ca[x+to[i][]][y+to[i][]]!='X')
{
ca[x+to[i][]][y+to[i][]]='X';
if(dfs(x+to[i][],y+to[i][],cnt+))return ;
ca[x+to[i][]][y+to[i][]]='.';
}
}
return ;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF&&n+m+t)
{
int i,j,wall=,stratX,stratY;
getchar();
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
scanf("%c",&ca[i][j]);
if(ca[i][j]=='S')
stratX=i,stratY=j;
else if(ca[i][j]=='X')
wall++;
else if(ca[i][j]=='D')
doorX=i,doorY=j;
}
getchar();
}
if(n*m-wall<=t)
{
printf("NO\n");
continue;
}
ca[stratX][stratY]='X';
if(dfs(stratX,stratY,))
printf("YES\n");
else
printf("NO\n");
}
return ;
}

代码解析:

 #include<bits/stdc++.h>
using namespace std;
int n,m,t;//n*m 矩阵 和 走的步数 t
int doorX,doorY;//门的位置
char ca[][];//矩阵大小
int to[][] = {{,-},{,},{-,},{,}};
int dfs(int x,int y,int cnt)
{
if(x>n||y>m||x<=||y<=)return ;//位置越界
if(cnt==t&&x==doorX&&y==doorY)//已走步数 == 要走的步数 t,且位置刚好是门的位置,成功找到一条路
return ;
//! { 奇偶性剪枝:
int tem=t-cnt-abs(x-doorX)-abs(y-doorY);
if(tem< || tem& )return ;
// 剩余的步数小于0 || tem 为奇数 都不满足 return 0 即可!} //@{ 遍历八个方向 递归dfs 如果有条件满足 return 1,否则结束 return 0;
for(int i=; i<; i++)
{
if(ca[x+to[i][]][y+to[i][]]!='X')
{
ca[x+to[i][]][y+to[i][]]='X';
if(dfs(x+to[i][],y+to[i][],cnt+))return ;
ca[x+to[i][]][y+to[i][]]='.';
}
}
return ;
// @}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF&&n+m+t)
{
int i,j,wall=,stratX,stratY;
getchar();
// 001 { 记录开始位置,墙的个数,以及门的位置
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
scanf("%c",&ca[i][j]);
if(ca[i][j]=='S')
stratX=i,stratY=j;
else if(ca[i][j]=='X')
wall++;
else if(ca[i][j]=='D')
doorX=i,doorY=j;
}
getchar();
}
// 001 }
//002 {剪一下枝: 如果墙的个数+走的步数>=矩阵单元的个数,则肯定无法完成此任务,因为还要有开始和门的位置存在
if(n*m-wall<=t)
{
printf("NO\n");
continue;
}
//002 }
ca[stratX][stratY]='X';//003 { 将开始位置置为墙不可再次访问 } if(dfs(stratX,stratY,))
printf("YES\n");
else
printf("NO\n");
}
return ;
}

HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)的更多相关文章

  1. 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 ...

  2. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

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

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

  4. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  5. hdu 1010 Tempter of the Bone 深搜+剪枝

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

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

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

  7. M - Tempter of the Bone(DFS,奇偶剪枝)

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

  8. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  9. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

随机推荐

  1. supervisor program配置实例

    program 配置 上面我们已经把 supervisrod 运行起来了,现在可以添加我们要管理的进程的配置文件.可以把所有配置项都写到 supervisord.conf 文件里,但并不推荐这样做,而 ...

  2. Qt学习中遇到的问题

    问题: 一个Qt小项目,编译成功并成功运行,但应用程序输出中出现如下异常:FTH: (9892): *** Fault tolerant heap shim applied to current pr ...

  3. Shell脚本_启动停止重启sh脚本

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. 【POJ 1556】The Doors 判断线段相交+SPFA

    黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...

  5. java的执行与加载的过程

    第一.我们编写一个.java源文件: 第二.通过编译器javac.exe把.java源文件编译为.class字节码文件并装入类装载器里: 第三.java虚拟机java.exe把字节码文件解释为各个平台 ...

  6. js学习笔记6----作用域及解析机制

    1.作用域: 域:空间.范围.区域… 作用:读.写 script  全局变量,全局函数 自上而下 函数 由里到外 {} 2.js解析: ⑴   “找一些东西”:var. function. 参数…… ...

  7. MySQL的Sleep进程

    php的垃圾回收机制,其实只针对于php本身. 对于mysql,php没权利去自动去释放它的东西. 如果你在页面执行完毕前不调用mysql_close(),那么mysql那边是不会关闭这个连接的. 如 ...

  8. 整站网页doc下载wget (转)

    -x -np -p -m -k -t -X/upload/ http://网址 为了让这个命令行的各选项意义更加明确,它还可以写成: --force-directories --no-parent - ...

  9. ECMall /app/buyer_groupbuy.app.php SQL Injection Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link: 2. 漏洞触发条件 0x1: POC ht ...

  10. C#获取文件的Md5值

            private string GetMd5(Stream fileStream)         {             MD5CryptoServiceProvider md5P ...