[原]poj-3009-Curling 2.0-dfs
题目太长就不贴了,题意:
上下左右四联通块,2表示起点,3表示终点,1为block,0为空地,每动一次冰壶,冰壶就会向推动的方向一直移动,直到碰到block或出界,如果碰到block就在block前停下来,同时block消失,如果出界则失败,输出-1,同时,如果在10次推动内都没达到终点也失败,输出-1。如果成功,则输出最少推动次数。
思路:
有四种情况:1,冰壶起点周围没有空地,不能推动,失败; 2,冰壶周围有空地可以移动,则进行对周围进行dfs,情况有三,①直接遇到终点、②碰到block、③出界。
总结:
被这题难住的地方有 :①冰壶可以一直移动,②block会消失,所以如果dfs不成功,还要复原,③对于一开始就不能移动的冰壶做判断。对于第一种情况用一趟while()一直对一个方向更新坐标直到边界条件或下次dfs起点,就解决了;对于第二种情况,可以看下面的AC代码,其实每次冰壶碰到block之后让block消失,然后重新dfs,但在dfs返回时再复原就好了,这时 ans 已经记录下最少移动次数的信息,对于block存不存在对结果没有影响,自己可以思考一下;情形三,则作一判断 !( nx-dir[i][0]
== x && ny-dir[i][1] == y ),具体看下面的代码。
当把上面的情况理清之后,就是一道很基础的dfs了~
下面是AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 30
#define INF 9000
int d[4][2] = { 0,1, 0, -1, 1, 0, -1, 0 };
int bo[maxn][maxn];
int w, h, ans;
bool check(int a, int b)
{
if(a >= 0 && b >= 0 && a < h && b < w) return true;
return false;
}
int x;
void dfs(int a, int b, int rec)
{
if(rec > 10 || rec > ans) return;
for(int i = 0; i < 4; i++)
{
int dx = a + d[i][0];
int dy = b + d[i][1];
if(check(dx, dy) && (bo[dx][dy] == 0 || bo[dx][dy] == 2|| bo[dx][dy] == 3)){
bool flag = 0;//cout<<i<<endl;
while(bo[dx][dy] != 1 && bo[dx][dy] != 3){
dx += d[i][0];
dy += d[i][1];
if(!check(dx, dy)) {
flag = 1;
break;
}
} if(flag) continue;
if(bo[dx][dy] == 1) {
bo[dx][dy] = 0;
dfs(dx-d[i][0], dy-d[i][1], rec+1);
bo[dx][dy] = 1;
}
if(bo[dx][dy] == 3){
if(ans > rec) ans = rec;
return;
}
}
}
}
void work()
{
int sx, sy;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
scanf("%d", &bo[i][j]);
if(bo[i][j] == 2) sx = i, sy = j;
}
}
ans = INF;
dfs(sx, sy, 1);
if(ans == INF) printf("-1\n");
else printf("%d\n", ans);
}
int main()
{
while(scanf("%d%d", &w, &h) != EOF && h && w){
work();
}
return 0;
}
[原]poj-3009-Curling 2.0-dfs的更多相关文章
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- 【POJ】3009 Curling 2.0 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- poj 3009 Curling 2.0
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ3009——Curling 2.0(DFS)
Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...
随机推荐
- Window.document对象(2)
四.操作样式 首先利用元素的ID找到该元素,存于一个变量中: var a = document.getElementById("id"): 然后可以对该元素的属性进行操作: a.s ...
- proxy server 代理服务器
有时候,我觉得自己需要去搞明白.搞清楚一个概念,帮我打通一下自己的知识体系,或者说,尝试联络起来. 1. 简介 突破自身IP限制,访问国外站点. 访问单位或者团体内部资源. 突破中国电信的IP封锁. ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- C++中定义比较函数的三种方法
原文地址:http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/ C++编程优与Pascal的原因之一是C++中存在ST ...
- Effective Java总结
规则1. 用静态工厂方法代替构造器 例子: public class Example { } public class StaticFactory { //valueOf/Of/getInstance ...
- 引擎设计跟踪(九.14.2i) Android GLES 3.0 完善
最近把渲染设备对应的GLES的API填上了. 主要有IRenderDevice/IShader/ITexture/IGraphicsResourceManager/IIndexBuffer/IVert ...
- eclipse下使用API操作HDFS
1)使用eclipse,在HDFS上创建新目录 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...
- 【四】php字符串操作
1.trim函数,我们队trim函数并不陌生,用于去除字符串两头的空白符.php的trim方法也可以做到这一点,但是还可以使用第二个参数,用于规定你在两头去掉什么.php中还有 ltrim 和 rtr ...
- DF学Mysql(一)——数据库基本操作
1.创建数据库 create Database <数据库名>; 注意:1)数据库名由字母.下划线.@.#和$组成 2)首字母不能是数字和$符号 3)不允许有空格和特殊字符 2.查看数据库 ...
- JS中 判断null
以下是不正确的方法: var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 ...