Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131619    Accepted Submission(s):
35432

Problem 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
 
题意是恰好走k步从S点到达D点,所以我们需要剪枝。
1:s点的坐标减去d点的坐标要恰好等于k..即:abs(ax-bx)+abs(ay-by)==k;
2:判断奇偶:假设
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
我们发现从0走一步一定走到1,从1走一步一定走到0。
也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
 
也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。
两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可
#include<iostream>
#include<string.h>
using namespace std;
#include<math.h>
char s[][];
int ax,ay,bx,by,n,m,k;
int t[][]={,,-,,,,,-},visit[][],flag;
void dfs(int x,int y,int count)
{
int mx,my,i;
if(x==bx&&y==by)
{
if(k==count){ flag=;
}
return;
}
if(count>=k)
return;
if(s[x][y]!='X')
{
for(i=;i<;i++)
{
mx=x+t[i][];
my=y+t[i][];
if(s[mx][my]!='X'&&mx>=&&mx<=n&&my>=&&my<=m&&!visit[mx][my])
{
visit[mx][my]=;
dfs(mx,my,count+);
visit[mx][my]=;
if(flag) //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点,就会超时
return;
}
}
}
}
int main()
{
while(cin>>n>>m>>k)
{
if(n==&&m==&&k==)
return ;
int i,count=;
flag=;
for(i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
cin>>s[i][j];
if(s[i][j]=='S')
{
ax=i;ay=j;
}
if(s[i][j]=='D')
{
bx=i;by=j;
} }
}
getchar();
memset(visit,,sizeof(visit));
if((abs(ax-bx)+abs(ay-by))>k||(ax+ay+bx+by+k)%==)//剪枝干
{
//cout<<"*"<<endl;
cout<<"NO"<<endl;
continue;
}
visit[ax][ay]=;
count=;
dfs(ax,ay,count);
if(flag==)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

hdu2010(dfs+剪枝)的更多相关文章

  1. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  3. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  4. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  5. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  6. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. 创建Android环境并且安装cordova

    需要eclipse.Andriod SDK.java.Apache ant.Node.js.Genymotion 目录链接: 1.安装adt-eclipse 2.安装JAVA 3.安装Apache a ...

  2. C# 方法中的参数类型

    二.方法中的参数类型 1. 值参数 值参数是指不带修饰符只带数据类型的形参. 值参数在使用值向方法传递参数时,编译程序会把实参的值做一份拷贝,并且将此拷贝传递给该方法,被调用的方法不会修改内存中实参的 ...

  3. js最佳实践

    JavaScript使用windows对象的open()方法来创建新的浏览器窗口,这个方法有三个参数:windows.open(url,name,features) 参数一:url:是想在新窗口里打开 ...

  4. typeid操作符

    typeid() operator返回type_info,返回值不可拷贝.不可赋值 // Illustrates the typeid operator. #include <iostream& ...

  5. Docker虚拟化容器的使用

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ...

  6. ethereum(以太坊)(九)--global(全局函数)

    pragma solidity ^0.4.0; contract modifierTest{ bytes32 public blockhash; address public coinbase; ui ...

  7. ethereum(以太坊)(五)--Bool

    pragma solidity ^0.4.0; contract Bool{ uint num1 = 100; uint num2 = 200; bool _c = true; // &&am ...

  8. PHP 微信公众号真正正确的客服头像上传

    首先我们来看官方文档 这TM的搞笑呢 什么破玩意儿! 需要条件 1 需要有一个客服的账号 (废话) 2 一致jpg格式的图片(扯蛋) 完整流程 1 获取access_token 2获取账号 3 $ur ...

  9. 数据结构学习-AVL平衡树

    环境:C++ 11 + win10 IDE:Clion 2018.3 AVL平衡树是在BST二叉查找树的基础上添加了平衡机制. 我们把平衡的BST认为是任一节点的左子树和右子树的高度差为-1,0,1中 ...

  10. 呕心沥血写的python猜数字

    #猜数字 import random num_rd=random.randint(0,100) count=1 while 1<=count<=10: num_ip=input('请输入0 ...