关于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个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮助乔 ...
随机推荐
- mysql in 和 not in 语句用法
1.mysql in语句 select * from tb_name where id in (10,12,15,16);2.mysql not in 语句 select * from tb_name ...
- java注解编程
- H5学习第三周
今天主要总结弹性布局 flex使用 1.给父容器添加display flex/inline-flex;属性 2.父容器可以使用的属性值有 >>>flex-direction 属性决定 ...
- WPF:动态显示或隐藏Listview的某一列
这几天做项目,需要做个listview满足能够动态显示或隐藏某些列,由于自己是菜鸟水平,查了两天资料也没有想出解决办法.就在我山穷水尽的时候看到了Mgen的一篇博客,给了我很大启发,所以我也决定把自己 ...
- Bootstrap警告框
前面的话 在网站中,网页总是需要和用户一起做沟通与交流.特别是当用户操作上下文为用户提供一些有效的警示框,比如说告诉用户操作成功.操作错误.提示或者警告等.在Bootstrap框架有一个独立的组件,实 ...
- Maven下从HDFS文件系统读取文件内容
需要注意以下几点 1.所以的包都是org.apache.hadoop.XXX 2.三个配置文件要放到指定文件夹中等待文件系统读取(src/main/resources):core-site.xml h ...
- OpenStack(企业私有云)万里长征第五步——虚拟机Migrate&Resize
一.前言 上一篇文章讲了OpenStack的部署和简单操作,今天介绍一下如何实现虚拟机的Migrate以及Resize.Migrate操作和Resize操作基本上属于同一种操作,Migrate操作只是 ...
- linux UART
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <errno.h ...
- 钉钉 机器人接入 自定义webhook
钉钉出了个webhook机器人接入,自定义的机器人支持随时post消息到群里: 昨天就尝试着用C#写了个: 一开始用python写,但是莫名的提示 {"errmsg":" ...
- C++学习(五)入门篇——基本类型
面向对象编程的本质是设计并扩展自己的数据类型,让类型和数据匹配. 内置C++分成两种类型:基本类型和复合类型 1.简单变量 程序需要存储信息时,必须记录三个基本属性 (1)信息将存储在哪 (2)要存储 ...