POJ 3009 Curling 2.0【带回溯DFS】
题意:
给出一个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】的更多相关文章
- 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 {深度优先搜索}
原题 $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(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
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- 【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 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ P3009 Curling 2.0 题解
深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙.到达终点就输出,没到就回溯. #include<iostream> #include<cstring> #include ...
随机推荐
- C++ cast
excerpted from Type conversions K&R Section 2.7 p59 对type conversion 的解释: The precise meaning of ...
- Unity StrangeIoC框架
Unity StrangeIoC框架 http://blog.csdn.net/y1196645376/article/details/52746251
- iOS多线程-多线程实现之GCD
什么是GCD? GCD: Grand Central Dispatch (重要的中枢调度器) GCD是纯C语言的, 学习它就是学习一些函数的使用. GCD的核心概念和使用步骤 核心概念 任务 : 执行 ...
- SQL Sever无法打开链接对话框,未将对象引用设置到对象的实例。(AppIDPackage)
前几天刚做完系统,先装的是SQL Sever2008,装完后还试了一下,OK~没问题,然后就继续装VS2012等一些软件.搞到很晚没有继续试试就睡了,第二天运行SSMS出问题了..(如图 1.0 所示 ...
- Gedit 解决中文显示乱码问题
详细请参考:http://wiki.ubuntu.org.cn/Gedit%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81 具体原因是Gedit的默认编码设置没有添加中文编码所 ...
- SLF4J: Class path contains multiple SLF4J bindings.
库冲突导致的异常,由于多次引入SLF4j包导致. It seems you have several implementation of SLF4J; you should exclude all t ...
- git clone出现的error: The requested URL returned error: 401 Unauthorized
error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.net/.. ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- PYTHON lambda表达式
lambda相当于def定义函数 一一对应
- apache 虚拟机配置
<VirtualHost *:80> DocumentRoot /www/htdocs/caipiao ServerName www.aaa.com ServerAlias aaa.com ...