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 ...
随机推荐
- EF连接MySql数据库
Windows要想EF连接MySql,首先要安装两个应用程序 mysql-connector-net-6.8.8.msimysql-for-visualstudio-1.2.7.msi 项目还需要两个 ...
- Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库
概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...
- 【Android】Android 手机忘记锁屏密码的解决办法
对于忘记Android锁屏密码的筒子们,除重新刷包或者wipe data外,还可用adb来删除密码: 方法很简单 1.手机连接电脑,不要打开数据连接,在充电模式下进行. 2.在电脑上,解压adb.zi ...
- Kudu-压缩
随着时间的推移,tablet会积累许多DiskRowSets,并且会在行更新时累积很多增量重做(REDO)文件.当插入一个关键字时,为了强制执行主关键字唯一性,Kudu会针对RowSets查询一组布隆 ...
- MyEclipse 安装插件 Github安装/使用 教程
2016年02月18日 09:45:23 阅读数:4531 本文的目的是 1.在 Myeclipse10.7中 集成Github并使用. 选择的安装方式是:MyEclipse 中设置下文中1后,下载g ...
- Codeforces 354B dp Game with Strings dp
Game with Strings 题意并不是在图上走,看了好久才看出来.. dp[ i ][ mask ]表示从 i 层开始走,起点有mask个, a的个数-b的个数的 最大值或者最小值. #in ...
- B2B、B2C、C2C、O2O
B2B:企业对企业 B2B (也有写成 BTB)是指企业对企业之间的营销关系,它将企业内部网,通过 B2B 网站与客户紧密结合起来,通过网络的快速反应,为客户提供更好的服务,从而促进企业的业务发展(B ...
- redis虚拟内存
对于redis 这样的内存数据库, 内存总是不够用的. 除了可以将数据分割到多个 redis 服务器以外. 另外的能够提高数据库容量的办法就是使用虚拟内存技术把那些不经常访问的数据交换到磁盘上 如果我 ...
- C++学习之 —— 输入输出
案例:输入任意空格和数字,输出其中的数字之和. #include <iostream> using namespace std; int main() { ; cout << ...
- 浅谈云计算SPI(SaaS、PaaS、IaaS)
The other day, I arrived at the SAP LABS CHINA for interview with my pleasure. That gave me a chance ...