题目太长就不贴了,题意:

上下左右四联通块,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;
}

作者:u011652573 发表于2014-4-15 22:42:28 原文链接
阅读:61 评论:0 查看评论

[原]poj-3009-Curling 2.0-dfs的更多相关文章

  1. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  2. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  3. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  4. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  5. 【POJ】3009 Curling 2.0 ——DFS

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Desc ...

  6. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  7. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  8. poj 3009 Curling 2.0

    题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...

  9. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  10. POJ3009——Curling 2.0(DFS)

    Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...

随机推荐

  1. [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine)

    [转]如何写出高效能TSQL -深入浅出SQL Server Relational Engine (含 SQL 2014 in-memory Engine) http://blogs.technet. ...

  2. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  3. mysql--乱码

    不知道为什么utf8反而会乱码,每次都是设gbk,,唉这样写项目的时候也是有点问题的T  T set names gbk; 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. 802.11 wireless 七

    802.11 wireless 7Wireless Fundamentals : End-to-End Discovering the NetworkGetting Connect Clients i ...

  5. boost 相关

    编译boost: 1.打开Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prom ...

  6. HDOJ 1429 胜利大逃亡(续)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

    准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...

  8. Sql注入一种dump所有数据的方法

    Select exp(~(select*from(select(concat(@:=0,(select count(*)from`information_schema`.columns where t ...

  9. Javascript format方法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. magento后台无法登陆的问题

    解决方法: 打开 magento/app/code/core/Mage/Core/Model/Session/Abstract/varien.php 将// set session cookie pa ...