关于dfs+剪枝第一篇:hdu1010
最近进入了dfs关于剪枝方面的学习,遇到的第一道题就是hdu的1010。一道很基础的剪枝。。可我不幸地wa了很多次(待会再解释wa的原因吧QAQ),首先我们来看一下题目。
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.
'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.
if((t>h*l-sum-)||(t<abs(xz-x0)+abs(yz-y0)))printf("NO\n");
其次,我们考虑在一个矩阵中,如果到达出口需要奇数步,而我们到达出口会走偶数步时,狗狗出不去;同理,若到达出口需要偶数步,而我们会走奇数步时,狗狗同样出不去。
用一个1和0组成的矩阵作为例子:
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从图中便可以清楚地看到,奇数步所能走到的,偶数步必然走不到。
那么我们通过判断起点的奇偶性和终点的奇偶性,再将它们加和与指定时间的奇偶性进行比较,便可以完成第二个剪枝:奇偶剪枝。
else if(((x0+y0)%+(xz+yz)%)%!=t%)printf("NO\n");
接下来介绍几个技巧:
void dfs(int p,int q,int num){
int i,x,y;
if(tmp)return ;//如果可以到达,便不再dfs,一直退层,直到退出dfs;
for(i = ; i < ; ++i){
x=p+u[i];y=q+r[i];
if(x>=&&x<=h&&y>=&&y<=l&&a[x][y]){
a[x][y]=false;
if(x==xz&&y==yz&&num==t){
tmp=;
return ;
}
else if(num<t)dfs(x,y,num+);
a[x][y]=true;
}
}
}
以上便是1010这道题的所有注意点了,然后贴出代码~
#include<cstdio>
#include<cstring>
#include<stdlib.h>
int u[]={,-,,},r[]={,,,-};
bool a[][];
int h,l,xz,yz,tmp,t;
void dfs(int p,int q,int num){
int i,x,y;
if(tmp)return ;
for(i = ; i < ; ++i){
x=p+u[i];y=q+r[i];
if(x>=&&x<=h&&y>=&&y<=l&&a[x][y]){
a[x][y]=false; if(x==xz&&y==yz&&num==t){
tmp=;
return ;
}
else if(num<t)dfs(x,y,num+);
a[x][y]=true;
}
}
}
int main(){
int x0,y0;
char w[];
while(scanf("%d%d%d",&h,&l,&t)!=EOF){
int sum=;
if(h==&l==&t==)return ;
memset(a,,sizeof(a));
for(int i = ; i <= h ; ++i)
{
scanf("%s",w+);
for(int j = ; j <= l ; ++j){
if(w[j]=='X'){
a[i][j]=false;
++sum;
}
else if(w[j]=='S'){
x0=i;
y0=j;
a[i][j]=false;
}
else if(w[j]=='D'){
xz=i;
yz=j;
}
}
}
if((t>h*l-sum-)||(t<abs(xz-x0)+abs(yz-y0)))printf("NO\n");
else if(((x0+y0)%+(xz+yz)%)%!=t%)printf("NO\n");
else {
dfs(x0,y0,);
if(tmp==)printf("YES\n");
else printf("NO\n");
}
tmp=;
}
}
至于我wa的原因。。。是把t当成sum一直在用(尴尬)。。。更可怕的是,样例数据竟然过了...
犯这样的错误也给我自己留下深刻的教训,以后写代码要认真写,以免再犯这样的错误,大家也是一样。
以上便是对1010这道题的所有分析,若有讲的漏洞或是错误请大家纠正出来,希望一次做的比一次好~bye~~
关于dfs+剪枝第一篇:hdu1010的更多相关文章
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 国内第一篇详细讲解hadoop2的automatic HA+Federation+Yarn配置的教程
前言 hadoop是分布式系统,运行在linux之上,配置起来相对复杂.对于hadoop1,很多同学就因为不能搭建正确的运行环境,导致学习兴趣锐减.不过,我有免费的学习视频下载,请点击这里. hado ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- poj 1011 :Sticks (dfs+剪枝)
题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...
- Black And White(DFS+剪枝)
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others ...
- 历届试题 邮局(dfs+剪枝)
历届试题 邮局 时间限制:1.0s 内存限制:256.0MB 问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...
- hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...
- AcWing:167. 木棒(dfs + 剪枝)
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮助乔 ...
随机推荐
- 【LeetCode】67. Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- eclipse在多modules项目结构下避免模块间依赖引用的场景
这个在单一classLoader时,不会有问题.如果多classloader下会有问题. 假设工程有两个模块,module2 依赖module1 当我们执行mvc eclipse:eclipse后,然 ...
- html路径问题
1.绝对路径 绝对路径是指文件在硬盘上真正存在的路径.例如"bg.jpg"这个图片是存放在硬盘的"E:\book\网页布局代码\第2章"目录下,那么 ...
- 从.src.rpm包中提取出完整的源码的方法
1 什么是完整的源码 就是说,最初始的源码加上打了所有的patch后的源码,即最新的源码. 2 过程 2.1 从.src.rpm中提取完整的rpm工程文件 2.1.1 rpm to cpio rpm2 ...
- angularJS 源码阅读之一:toDebugString
简介: 这个函数返回调试字符串: number,boolean,string,null,undefined,都会转为字符串. function 中括号前面有空格的,会去除函数体,没空格的,会输出函数的 ...
- KBEngine简单RPG-Demo源码解析(2)
七:服务端资产库文件夹结构http://kbengine.org/cn/docs/concepts/directorys.html看assets, 注意:demo使用的不是默认的assets资产目录, ...
- 今天get到的两个angular知识点
angular 控制器$scope依赖注入的正确写法 <div ng-controller="ctrl"> {{name}} {{age}} <div ng-co ...
- HTML 头标签的 <title> <base> <meta> <link> <script> 的内容意思
头标签都放在<head></head>头部分之间.包括:title base meta link <title>:指定浏览器的.(标题) <base>: ...
- RecyclerView-------之GridView模式加载更多
随着RecyclerView的出现,Listview.GridView的使用率相对有些减少,废话不多说,先看一下效果: 代码如下: 1.自定义的RecyclerView(根据自己的需要) public ...
- python打印万年历
1.输入年份,输入月份 2.格式化输出本月的日历 3.思路 输入年,月,打印对应年月的日历.1,首先1970年是Unix系统诞生的时间,1970年成为Unix的元年,1970年1月1号是星期四,现在大 ...