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. ES6初识-Proxy和Reflect

    { let obj={ time:'2017-03-11', name:'net', _r:123 };   let monitor=new Proxy(obj,{ // 拦截对象属性的读取 get( ...

  2. Oracle中文乱码解决

    查看当前Oracle字符集 select userenv('language') from dual; USERENV('LANGUAGE') ---------------------------- ...

  3. Python——合集

    合集 主要功能是对比列表中的信息,进行关系测试. 特点:1. 去重,把一个列表变成合集,就自动去重了.2. 关系测试,测试两组数据之间的交集.差集.并集等关系.3. 没有插入功能,只能添加.4. 一个 ...

  4. tp3.2 excel导出

    //导出操作 function exportExcel($expTitle,$expCellName,$expTableData,$names,$width){ $xlsTitle = iconv(' ...

  5. MySQL - Mac下安装MySQL

    1. 去官网下载dmg的安装文件. 2. 下载完成后,运行安装文件,按步骤进行安装,安装完成后会弹出一个框显示临时密码! 3. 编辑~/.bashrc文件,配置快速启动/停止/重启/cdhome/别名 ...

  6. 【操作系统作业-lab4】 linux 多线程编程和调度器

    linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...

  7. bootloader svc 模式

    bootloader 和操作系统都是工作在svc模式下 /* * set the cpu to SVC32 mode */ mrs r0,cpsr bic r0,r0,#0x1f orr r0,r0, ...

  8. react+redux状态管理实现排序 合并多个reducer文件

    这个demo只有一个reducer 所以合并reducer这个demo用不到 ,但是我写出来这样大家以后可以用到,很好用,管理多个reducer,因为只要用到redux就不会只有一个reducer所以 ...

  9. Parameter 'limit' not found. Available parameters are [arg1, arg0, pa

    mybatis代码报错,这是因为mapper识别不了limit,需要替换成 LIMIT #{arg0},#{arg1}

  10. Windows下zookeeper注册中心的安装和启动

    zookeeper的安装支持单机模式和集群模式 下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,当前稳定版本为3.4.8 单机模式 修改zoo ...