hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接: pid=1010">点击打开链接
题目描写叙述:给定一个迷宫,给一个起点和一个终点。问是否能恰好经过T步到达终点?每一个格子不能反复走
解题思路:dfs+剪枝
剪枝1:奇偶剪枝,推断终点和起点的距离与T的奇偶性是否一致,假设不一致,直接剪掉
剪枝2:假设从当前到终点的至少须要的步数nt加上已经走过的步数ct大于T,即nt+ct>t剪掉
剪枝3:假设迷宫中能够走的格子小于T直接剪掉
启示:剪枝的重要性
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n,m,t;
char g[10][10];
int sx,sy,dx,dy;
bool flag[10][10];
const int nx[]= {0,1,0,-1};
const int ny[]= {1,0,-1,0};
bool dfs(int x,int y,int ct)
{
if(x==dx&&y==dy)
{
if(t==ct)
return true;
else
return false;
}
if(abs(dx-x)+abs(dy-y)+ct<=t)
{
for(int i=0; i<4; ++i)
{
int ntx=x+nx[i];
int nty=y+ny[i];
if(ntx<=n&&ntx>=1&&nty<=m&&nty>=1&&g[ntx][nty]=='.'&&!flag[ntx][nty])
{
flag[ntx][nty]=true;
if(dfs(ntx,nty,ct+1)) return true;
flag[ntx][nty]=false;
}
}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)==3&&(n!=0||m!=0||t!=0))
{
int cut=0;
for(int i=1; i<=n; ++i)
{
scanf("%s",&g[i][1]);
for(int j=1; j<=m; ++j)
{
if(g[i][j]=='S') sx=i,sy=j;
if(g[i][j]=='D') dx=i,dy=j;
if(g[i][j]=='.') cut++;
}
}
if(abs(dx-sx)+abs(dy-sy)>t||cut<t-1||(abs(dx-sx)+abs(dy-sy))%2!=t%2)
{
printf("NO\n");
continue;
}
memset(flag,false,sizeof(flag));
flag[sx][sy]=true;
g[dx][dy]='.';
if(dfs(sx,sy,0))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
hdu1010Tempter of the Bone(dfs+奇偶剪枝)的更多相关文章
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- 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 ...
- Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 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 ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- Problem B: 查找某一个数
#include<stdio.h> int main(void) { ]; int i; char ch='n'; while(scanf("%d %d",&x ...
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- PS 2019 | Photoshop CC 2019 的安装激活
文章目录 写在前面 安装步骤 软件激活 关闭"主页"屏幕 写在前面 Photoshop CC 2019(64位)下载地址: 链接:https://pan.baidu.com/s/1 ...
- PHP数组和数据结构(下)未完。。。。
1.数组的遍历 (1)each(): 接受一个数组作为参数,返回数组中当前元素的键/值对,并向后移动数组指针到下一个元素的位置 键/值对被返回为带有四个元素的关联和索引混合的数组,键名分别为0,1,k ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) E. Robot Arm 线段树
E. Robot Arm 题目连接: http://www.codeforces.com/contest/618/problem/E Description Roger is a robot. He ...
- 使用hosts.allow和hosts.deny实现简单的防火墙
说明:我建议学习防火墙只单一学习一种就够了,这种方式虽然简单和快速,但也有些不太灵活,所以如果要深入防火墙建议转iptables 一.背景简介 在Linux上多用iptables来限制ssh和tel ...
- Netty游戏服务器之六服务端登录消息处理
客户端unity3d已经把消息发送到netty服务器上了,那么ServerHandler类的public void channelRead(ChannelHandlerContext ctx, Obj ...
- 如何在windows下的Python开发工具IDLE里安装其他模块
以安装Httplib2模块为例 1 下载模块 到 “https://code.google.com/p/httplib2/” 下载一款适合你的压缩包“httplib2-0.4.0.zip” 2 解压下 ...
- Java集合类理解
深入Java集合学习系列:http://zhangshixi.iteye.com/blog/674856 http://blog.csdn.net/shf4715/article/details/47 ...
- 新手遇到的问题:Easy UI的对话框老是在页面载入完毕后自己主动弹出
因为是第一次接触Easy UI,还不是非常熟悉,尝试了一下对话框功能,还是非常不错的.但问题是页面载入完毕后.全部的对话框都自己主动弹出来了,百度了好久,也没有详细说明确的,貌似别人都没有这个问题哦 ...