题目链接:

https://vjudge.net/problem/POJ-3009

题目大意:

问题:打冰球。冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失。当冰球紧贴墙时,不能将冰球往那个方向打。冰球出界就当输,超过10次还没将冰球打到目标位置也当输。求用最小次数将冰球打到目标位置,或输出-1表示输了。

思路:

分析:一般来说,求最小步数之类的迷宫问题都是用BFS解决的,但这题涉及到迷宫状态的变化(墙),BFS要不断记录状态的变化很复杂,不过网上好像也有人用BFS做的。DFS更加适合这种状态一直变化的,只不过要保存最优值而已,其实最优值也方便剪枝(当前步数已经是当前最优值大小但还没到达目的地也就没必要进行下去了)。需要注意的是,这题并不是移动一格,而是一直移动直到遇到障碍物。特别是目标点可能在移动的过程中到达。具体看代码。

 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m, d;
const int maxn = 1e5 + ;
int Map[][], ans;//Map[i][j]表示ij这个点的最短
int dir[][] = {,,,,-,,,-};
bool judge(int x, int y)
{
return (x >= && x < n && y >= && y < m);
}
void dfs(int x, int y, int d)
{
if(Map[x][y] == )
{
ans = min(ans, d);
return;
}
if(d >= )return;
for(int i = ; i < ; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(!judge(xx, yy) || Map[xx][yy] == )continue;
while()
{
if(!judge(xx, yy))break;
if(Map[xx][yy] == )
{
Map[xx][yy] = ;
dfs(xx-dir[i][], yy-dir[i][], d + );
Map[xx][yy] = ;
break;
}
else if(Map[xx][yy] == )
{
dfs(xx, yy, d + );
break;
}
xx += dir[i][];
yy += dir[i][];
}
}
}
int main()
{
while(cin >> m >> n && (n + m))
{
int sx, sy;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
cin >> Map[i][j];
if(Map[i][j] == )
sx = i, sy = j, Map[i][j] = ;
}
ans = INF;
dfs(sx, sy, );
if(ans > )cout<<"-1"<<endl;
else cout<<ans<<endl;
}
return ;
}

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. 分层图 (可以选择K条路的权为0,求最短路)

    分层图可以处理从图中选取k条边使其边权变为0,求最短路 Description 在你的强力援助下,PCY 成功完成了之前的所有任务,他觉得,现在正是出去浪的大好时光.于是,他来到高速公路上,找到一辆摩 ...

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

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

随机推荐

  1. MB Star C5 Functions

    The MB STAR C5 notion and brand combination for hardware and software program components will be the ...

  2. Linux UDEV提权过程

    1.下载攻击脚本 [test@H0f ~]$ wget http://www.extmail .org/source/exploit-udev-8478 --2018-04-02 01:21:00-- ...

  3. php连接数据库mysql数据库

    查找数据 $con = mysqli_connect('localhost', 'root', '', 'mydb'); if (!$con) { die('数据库连接失败' . mysqli_con ...

  4. 德国生活tips

    提要: 在德国生活也近7个月的时间了,简单给准备来德国留学,生活或者是旅游的人写一些小tips.想到什么就写什么咯. (1)德国交通篇 在德国,交通是第一要点,一般大家都会看到城市里有Straßenb ...

  5. 安装Chrome插件Markdown Preview Plus

    1.在谷歌应用商店,安装Chrome插件Markdown Preview Plus   2.设置Markdown Preview Plus (1)鼠标左键该拓展插件 (2)鼠标右键该插件 3.将mar ...

  6. Engineer Assignment HDU - 6006 状压dp

    http://acm.split.hdu.edu.cn/showproblem.php?pid=6006 比赛的时候写了一个暴力,存暴力,过了,还46ms 那个暴力的思路是,预处理can[i][j]表 ...

  7. inventor卸载不干净

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  8. Linux网卡操作

    单个网卡操作 [root@localhost ~]# ifdown eth0 #关闭网络 [root@localhost ~]# ifup eth0 #启动网络 网络服务: [root@localho ...

  9. 连接MySql的时候报1130的错误解决办法

    部署了一个 数据库采用Mysql的程序,sqlyog连接非本地的Mysql服务器的数据库,居然无法连接很奇怪,报1130错误,ERROR 1130: Host 192.168.3.100 is not ...

  10. jQuery的表单验证

    jQuery的表单验证 直接上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...