poj3009 Curling 2.0(很好的题 DFS)
https://vjudge.net/problem/POJ-3009
做完这道题,感觉自己对dfs的理解应该又深刻了。
1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs。
2.在用dfs的时候,mp时一个全局变量,对于平等的走法,每一个走法结束后一定要状态复原!!!(也就是代码36-38行)否则会对其他走法产生影响。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
int n, m, mp[][], si, sj, gi, gj, cnt, mini;
int dir[][] = {, , , -, , , -, };
void dfs(int x, int y, int a, int b, int k, int c)
{
if(k > ||k > mini) return ;
if(x == gi&&y == gj){
mini = min(mini, k);
return ;
}
if(a != ||b != ){//不是刚开始
if(x<||x>=n||y<||y>=m){
return;//失败
}
else if(mp[x][y] == ){
if(c <= ) return ;
c = ;
mp[x][y] = ;//该阻被消去
x -= a;
y -= b;//回到上一状态
for(int i = ; i < ; i++){
dfs(x+dir[i][], y+dir[i][], dir[i][], dir[i][], k+, c+);
}
x += a;
y += b;
mp[x][y] = ;//记住要状态复原!!
}
else{
dfs(x+a, y+b, a, b, k, c+);
}
}
else{
for(int i = ; i < ; i++){
dfs(x+dir[i][], y+dir[i][], dir[i][], dir[i][], k+, c+);
}
}
}
int main()
{
while(cin >> m >> n, n, m){
mini = INF;
cnt=;
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
cin >> mp[i][j];
if(mp[i][j] == ) si=i,sj=j;
if(mp[i][j] == ) gi=i,gj=j;
}
}
dfs(si, sj, , , , );
if(mini == INF) cout << "-1" << endl;
else cout << mini << endl;
}
return ;
}
poj3009 Curling 2.0(很好的题 DFS)的更多相关文章
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- POJ3009——Curling 2.0(DFS)
Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...
- poj3009 Curling 2.0 (DFS按直线算步骤)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14563 Accepted: 6080 Desc ...
- POJ3009 Curling 2.0
正式做POJ的第一题,做出来后又看了别人的代码,就又完善了一下,也通过了.参考 http://blog.sina.com.cn/s/blog_4abcd9bc0100phzb.html 改了之后觉得写 ...
- poj3009 Curling 2.0 深搜
PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样 ...
- POJ3009 Curling 2.0(DFS)
迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...
- POJ3009 Curling 2.0(DFS)
题目链接. 分析: 本题BFS A不了. 00100 00001 01020 00000 00010 00010 00010 00010 00030 对于这样的数据,本来应当是 5 步,但bfs却 4 ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- Curling 2.0 分类: 搜索 2015-08-09 11:14 3人阅读 评论(0) 收藏
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14289 Accepted: 5962 Descript ...
随机推荐
- [转]CR, LF, CR/LF区别与关系
http://weizhifeng.net/talking-about-cr-lf.html 前言 在文本处理中,CR(Carriage Return),LF(Line Feed),CR/LF是不同操 ...
- C# 文件拖放到此程序的操作
问题描述: 怎么写代码可以实现指定类型的文件通过鼠标拖放显示在程序的文本框中,如:选中3个文件(3个文件的格式有MP3和wma)拖到程序,程序的文本框显示这三个文件的路径...解决代码: thi ...
- 删除了原有的offset之后再次启动会报错park Streaming from Kafka has error numRecords must not ...
笔者使用Spark streaming读取Kakfa中的数据,做进一步处理,用到了KafkaUtil的createDirectStream()方法:该方法不会自动保存topic parti ...
- dns-prefetch,新打开页面预抓取
dns-prefetch 对性能提升有多大 转载2016-04-07 12:57:41 标签:网站推广dns-prefetch对性能提 dns-prefetch, 是DNS预获取,也是网页前端的优化的 ...
- NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/9265380.html 题目传送门 - 洛谷P3960 题目传送门 - LOJ#2319 题目传送门 - Vij ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第七集之SSH远程连接——克隆与更改配置】
一.SSH远程连接 OpenSSH的安装 查看是否安装Openssh:rpm -qa | grep ssh搜索openssh安装包:yum search openssh安装openssh:yum in ...
- 000 Security的计划
可重用的,企业级的,认证和授权模块 1.主要涉及的模块 2.最终的目标 3.开发的项目模块结构
- day 52 dom 事件
本文转载自cnblog.liwenzhou.com 官网资料: http://www.w3school.com.cn/htmldom/dom_methods.asp 事件的内容很重要的,基本概念有点类 ...
- day33 网络编程之线程,并发以及selectors模块io多路复用
io多路复用 selectors模块 概要: 并发编程需要掌握的知识点: 开启进程/线程 生产者消费者模型!!! GIL全局解释器锁(进程与线程的区别和应用场景) 进程池线程池 IO模型(理论) 1 ...
- goLand工程结构管理
goLand工程结构管理 转 https://www.jianshu.com/p/eb7b1fd7179e 开始之前请确保安装好了 go语言环境并配置好了gopath环境变量 1.创建一个新目录并打 ...