今天继续dfs的训练,遇到了一道神题,不停地TLE,我DD都快碎了。。。。。好在经过本渣不懈努力,还是弄了出来,不容易啊,发上来纪念一下,顺便总结一下关于用dfs解决规定路程的问题。

先上题目:

Description

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. 
 

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 
0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give
 the maze layout, with each line containing M characters. A character is one of the following: 



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

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise. 
 

Sample Input

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

Sample Output

NO
YES
 

附上本渣题解:

//基本思路:通过回溯(即return回来后)把标记的改为未标记的从而得到所有路径
#include<stdio.h>
#include<cstdlib>
int r,c,t,x,y,x1,y1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char point[7][7];
bool flag;
int count;
int map[7][7];//能够帮助去掉不可能的情况,不用就TLE
void dfs(int x0,int y0,int r,int c,int step)//step记录当前点在当前路径的步数
{
int temp;
temp=count-step-abs(x0-x1)-abs(y0-y1)+1;//通过对当前点和最终点的位置判断当前点有无可能满足条件,不用就TLE
if (temp<0||temp%2==1)//这是通过奇偶判断,数学知识,易忽视。。
return;
for(int i=0;i<4;i++)
{
int tempx=x0+dx[i],tempy=y0+dy[i];
if(point[tempy][tempx]=='D'&&step==count)//判断步数与路径长度是否相同
flag=true;
if(tempx>=0&&tempx<c&&tempy>=0&&tempy<r&&point[tempy][tempx]=='.')
{
point[tempy][tempx]='X';//通过使当前点变为X来标记当前路径是否经过
dfs(tempx,tempy,r,c,step+1);//探索邻接点
point[tempy][tempx]='.';//回溯还原,使其得能够探索全部路径
if(flag==true)//如果存在,就直接返回,不用此判断就TLE。。。
return ;
}
}
return ;
}
int main()
{
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
{
if((i+j)%2)
map[i][j]=0;
else
map[i][j]=1;
}
while(1)
{
scanf("%d%d%d",&r,&c,&t);
getchar();
if(r==0&&c==0&&t==0)
break;
count=t;
for(int i=0;i<r;i++)
{
scanf("%s",point[i]);
}
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
if(point[i][j]=='S')
{
y=i;
x=j;
}
else if(point[i][j]=='D')
{
y1=i;
x1=j;
}
}
if(abs(map[y1][x1]-map[y][x])%2!=t%2)//map判断起点终点的位置是否有可能,不用就TLE。。
{
printf("NO\n");
continue;
}
flag=false;
dfs(x,y,r,c,1);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

map的用处:

v可以把map看成这样:
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v从为 0的格子走一步,必然走向为
1的格子
v从为 1的格子走一步,必然走向为
0的格子
v即:
v0->1或1->0必然是奇数步
v0->0走1->1必然是偶数步 

所以当遇到从 0走向0或从1走向1但是要求时间是奇数的,或者,从1
走向 0
但是要求时间是偶数的都可以直接判断不可达!

所以map表能从一开始就减少可能的情况。

利用dfs解决规定路程问题的更多相关文章

  1. 数据结构:关键路径,利用DFS遍历每一条关键路径JAVA语言实现

    这是我们学校做的数据结构课设,要求分别输出关键路径,我查遍资料java版的只能找到关键路径,但是无法分别输出关键路径 c++有可以分别输出的,所以在明白思想后自己写了一个java版的 函数带有输入函数 ...

  2. 用深度优先搜索(DFS)解决多数图论问题

    前言 本文大概是作者对图论大部分内容的分析和总结吧,\(\text{OI}\)和语文能力有限,且部分说明和推导可能有错误和不足,希望能指出. 创作本文是为了提供彼此学习交流的机会,也算是作者在忙碌的中 ...

  3. 利用闭包解决for循环里onclick事件不能捕捉实时i值问题

    问题描述 我们都知道,如果我们对于一组元素(相同的标签)同时进行onclick事件处理的时候(在需要获取到索引的时候),一般是写一个for循环,但是onclick是一个异步调用的,所以会带来一个问题, ...

  4. 利用Readability解决网页正文提取问题

    分享: 利用Readability解决网页正文提取问题   做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以用SS为各种网站写脚本做解析, 但是 ...

  5. 利用gulp解决微信浏览器缓存问题

    做了好多项目,这次终于要解决微信浏览器缓存这个令人头疼的问题了.每次上传新的文件,在微信浏览器中访问时,总要先清除微信的缓存,实在麻烦,在网上搜罗了很多解决办法,终于找到了方法:利用gulp解决缓存问 ...

  6. 利用Json_encode解决中文问题

    利用Json_encode解决中文问题       public function return_json($data=array()){         echo json_encode($data ...

  7. 利用Filter解决跨域请求的问题

    1.为什么出现跨域. 很简单的一句解释,A系统中使用ajax调用B系统中的接口,此时就是一个典型的跨域问题,此时浏览器会出现以下错误信息,此处使用的是chrome浏览器. 错误信息如下: jquery ...

  8. 利用NSProxy解决NSTimer内存泄漏问题

    之前写过一篇利用RunTime解决由NSTimer导致的内存泄漏的文章,最近和同事讨论觉得这样写有点复杂,然后发现有NSProxy这么好用的根类,根类,根类,没错NSProxy与NSObject一样是 ...

  9. 利用dynamic解决匿名对象不能赋值的问题

    原文:利用dynamic解决匿名对象不能赋值的问题 关于匿名对象 匿名对象是.Net Framework 3.0提供的新类型,例如: }; 就是一个匿名类,搭配Linq,可以很灵活的在代码中组合数据, ...

随机推荐

  1. 并行cmd

    #!/bin/bash ]; then echo -e "usage : \n\t$0 hostList REMOTE_CMD" echo -e "example: \n ...

  2. 高度-宽度关系,同一div、不同div高度与宽度关系控制函数

    //对象1的高度等于对象2的高度n倍,调用方法:Ht1DivideHt2('#div2','#div1',3)//div2的高度是div1高度的3倍function Ht1DivideHt2(obj1 ...

  3. K:java中properties文件的读写

    Properties类与.properties文件:   Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...

  4. .Net 如何模拟会话级别的信号量,对http接口调用频率进行限制(有demo)

    现在,因为种种因素,你必须对一个请求或者方法进行频率上的访问限制. 比如, 你对外提供了一个API接口,注册用户每秒钟最多可以调用100次,非注册用户每秒钟最多可以调用10次. 比如, 有一个非常吃服 ...

  5. File System 之本地文件系统

    上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...

  6. shell脚本的if语句,判断某程序是否存在,不存在启动该程序!

    想必大家都知道 "如果......那么......" 这种语法的应用吧! 当然呢,linux下对于这种用法也是有所考虑的,很多时候我们都需要写一个shell脚本,难免会避免if语句 ...

  7. 【转】Mac端包管理工具——Homebrew简介及安装

    Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...

  8. 命令行替代工具 - Cmder配置

    Cmder 可搭配使用cmd here ( cmdhere.reg ) 1.   修改config\aliases文件:添加下列几行 l=ls --show-control-chars la=ls - ...

  9. [转]Wing IDE 6.0 安装及算号器注册机代码

    下载安装wing 选择第三个,运行算号器,输入license id 输入request id. Python 2 算号器注册机代码 import string import random import ...

  10. 从零开始开发一个简易的类vue-cli构建工具

    代码地址:https://github.com/cheer4chai/webpack-learning 仿照vue-cli开发这个工具的目的是了解webpack的基本设置,以及vue-cli的工作原理 ...