POJ 3009

题意:

给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物也会随之消失,如果行动时超出方格的界限或行动次数超过了10则会game over .如果行动时经过3则会win,记下此时行动次数(不是行动的方格数),求最小的行动次数
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e5 + 10; int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
int ei, ej;
int map[25][25];
int w, h, steps, MIN;
#define MAX 99999999
void dfs(int si, int sj)
{
int i, pi, pj;
if (steps >= 10) return; for (i = 0; i<4; i++)
{
pi = si, pj = sj;
while (1)
{
pi += dir[i][0];
pj += dir[i][1];
if (pi <= 0 || pi>h || pj <= 0 || pj > w) break;//如果越界,选择其他方向
if (pi == ei&&pj == ej)
{
steps++;
if (MIN > steps) MIN = steps;
steps--;
return;
}
else if (map[pi][pj] == 1)//如果遇到障碍物
{ if(pi-dir[i][0]!=si||pj-dir[i][1]!=sj)//如果不是起步
{
map[pi][pj] = 0;//消除障碍物
steps++;//前进一步
dfs(pi - dir[i][0], pj - dir[i][1]);//递归查找该点到终点的最小步数
map[pi][pj] = 1;//还原障碍物
steps--;//还原步数
}
break;
}
}
}
}
int main()
{
int si, sj, i, j;
while (scanf("%d%d", &w, &h) == 2 && (w || h))
{
for (i = 1; i <= h; i++)//输入并找到起点和终点
for (j = 1; j <= w; j++)
{
scanf("%d", &map[i][j]);
if (map[i][j] == 2)
si = i, sj = j;
else if (map[i][j] == 3)
ei = i, ej = j;
}
MIN = MAX;//记录最小步数
steps = 0;//初始化步数
dfs(si, sj);//深搜
if (MIN == MAX) puts("-1");
else printf("%d\n", MIN);
}
return 0;
}

POJ 3009 Curling 2.0【带回溯DFS】的更多相关文章

  1. poj 3009 Curling 2.0 (dfs )

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

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

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

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

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

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

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

  5. poj 3009 Curling 2.0( dfs )

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

  6. poj 3009 Curling 2.0

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

  7. 【POJ】3009 Curling 2.0 ——DFS

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

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

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

  9. POJ P3009 Curling 2.0 题解

    深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙.到达终点就输出,没到就回溯. #include<iostream> #include<cstring> #include ...

随机推荐

  1. Windows 7 OpenGL配置

    http://blog.csdn.net/qingyang8513/article/details/45155245

  2. webservice 测试窗体只能用于来自本地计算机的请求

    Question: WebService部署成站点之后,如果在本地测试webservice可以运行,在远程却显示“测试窗体只能用于来自本地计算机的请求”或者"The test form is ...

  3. AVPlayer

    AVPlayer     AVPlayerLayer是CALayer的一个子类,由于AVPlayer这个播放器只能安置在AVPlayerLayer 这个图层之上,所以我们需要实例化一个UIView,并 ...

  4. python 调用dll中c或c++语言的带指针方法,

    在项目开发中遇到了,python需要去调用一个动态链接库dll中的c++方法.这个方法的参数为一个指针类型的参数,一个bool类型参数, 在python中并未对数字类型进行区分. int LP_Agc ...

  5. 安装 sublime2 (包括插件)

    1.下载地址:http://www.sublimetext.com/2,注意选择不同的平台: 2.安装后,打开sublime,在菜单栏  help -- enter license 打开一个窗口,复制 ...

  6. git clone出现的error: The requested URL returned error: 401 Unauthorized

    error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.net/.. ...

  7. python 装饰器的理解

    一. 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷 ...

  8. paintEvent(QPaintEvent*)是系统自动调用的

    qt中函数paintEvent(QPaintEvent*)是被系统自动调用. paintEvent(QPaintEvent *)函数是QWidget类中的虚函数,用于ui的绘制,会在多种情况下被其他函 ...

  9. qt添加cef库嵌入web [转]

    qt cef嵌入web 原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium ...

  10. 变量声明---let,const,解构

    let在很多方面与var是相似的,但是可以帮助大家避免在JavaScript里常见一些问题. const是对let的一个增强,它能阻止对一个变量再次赋值. 块作用域 当用let声明一个变量,它使用的是 ...