关于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个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮助乔 ...
随机推荐
- group by和count联合使用问题
要根据用户发布的产品数量来排序做分页,使用group ) FROM( SELECT uid,COU 工作中要根据用户发布的产品数量来排序做分页,使用group by uid 用count(uid) 来 ...
- mysqlclient和PyMySQL对比
环境:Python 3.5+, Django 1.9+ 最初用django时,搜索时发现PyMySQL的文章很多,然而在django的官方文档中python3版的mysql客户端驱动确没有提到PyMy ...
- [leetcode-573-Squirrel Simulation]
There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...
- Dagger2学习笔记
Dagger2是第一个使用生成代码的方式实现依赖注入的框架.作为Dagger的升级版本,自然有它的优势,优先注重的是执行效率.本文着重介绍Dagger2.官方据点传送门: https://google ...
- django-将数据库数据转换成JSON格式(ORM和SQL两种情况)
最近打算搞一个自动化运维平台,所以在看Django的知识. 在实际项目开发中,遇到一个小问题:前后端发生数据交互主流采用的是JSON.前端传数据到服务器端比较简单,稍微麻烦的是服务器端传JSON到前端 ...
- IIS 部署WCF服务注意事项
IIS部署WCF服务的时候经常会出现如下错误: System.ServiceModel.EndpointNotFoundException”类型的未经处理的异常在 WinformWcfHost.exe ...
- JavaScript一个生成文档目录的实例
执行结果: <body> <script type="text/javascript"> /** * 这个模块注册一个可在页面加载完成后自动运行的匿名函数, ...
- ES6简介
function fn(){ return 100; } let name='sui'; let age=19; let sui={ name, age, ["pro"+fn()] ...
- 扩展SQLite使其能从apk文件中读取db
游戏中会大量使用到配置文件,每个项目组根据自己不同的需求会选择不同的存储格式,比如使用Json或者SQLite来存储数据.此处我们只对使用SQLite的情况来做讨论.一般情况下会选择把它放在可读写目录 ...
- HDU 5547 Sudoku(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others ...