POJ-3009 Curling 2.0---DFS求最短路
题目链接:
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求最短路的更多相关文章
- 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 ...
- 分层图 (可以选择K条路的权为0,求最短路)
分层图可以处理从图中选取k条边使其边权变为0,求最短路 Description 在你的强力援助下,PCY 成功完成了之前的所有任务,他觉得,现在正是出去浪的大好时光.于是,他来到高速公路上,找到一辆摩 ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
随机推荐
- maven set MAVEN_OPTS
http://juvenshun.iteye.com/blog/240257 https://docs.alfresco.com/5.1/tasks/alfresco-sdk-install-mave ...
- JavaScript中使用ActiveXObject操作本地文件夹的方法
转载地址 http://www.jb51.net/article/48538.htm 在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实 ...
- ymPrompt.js消息提示组件
转载:https://www.cnblogs.com/linzheng/archive/2010/11/15/1878058.html 使用说明: 1.在页面中引入ymPrompt.js.如:< ...
- Gone Fishing
原题网址 代码已经写出来了,自己测试的时候没有问题,提交上去之后反馈了我一个Runtime error 一口老血啊! 找了半天还是没找到可能越界啊啥的地方 import java.util.Scan ...
- Web前端常见问题
一.理论知识 1.1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务器端响应http请求,浏览器 ...
- DB Intro - MySQL and MongoDB
mysql> CREATE TABLE tutorials_tbl( tutorial_id INT, tutorial_title VARCHAR(100), tutorial_author ...
- Sphinx Building Docs in horizon
Building Contributor Documentation This documentation is written by contributors, for contributors. ...
- 【密码学】CSP的概念
CSP加密服务提供者(Cryptographic Service Provider)具有一下几个特点: CSP是真正执行密码运算的独立模块 物理上一个CSP由两部分组成:一个动态连接库,一个签名文件 ...
- type='button'和'submit'的区别
今天在对表单的项目进行删除时出现了问题,原因就出现在点击input按钮时,这个input属性是type='button'还是type='submit'. 代码大致如下: <script type ...
- 为什么document.write()会清空原来的内容
为什么document.write()会清空原来的内容: 可能很多朋友都遇到过这样的情况,那就是使用document.write()函数向网页中写内容的时候,会把文档中的原来的内容给清空,这一点对于初 ...