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 ...
随机推荐
- mysql 主从单库单表同步 binlog-do-db replicate-do-db
方案一:两边做主从. SELECT SUM(DATA_LENGTH)+SUM(INDEX_LENGTH) FROM information_schema.tables WHERE TABLE_SCHE ...
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)
环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询. 方案一 直接在浏览器中打开Office文档在页面上的链接.会弹出如下窗口: 优点:主流浏览器都支持. 缺点: ...
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- [老文章搬家] [翻译] 深入解析win32 crt 调试堆
09 年翻译的东西. 原文见: http://www.nobugs.org/developer/win32/debug_crt_heap.html 在DeviceStudio的Debug编译模式下, ...
- SVN发布网站
1.问题 2.分析 如果机器先安装.NET framework,后安装IIS就会出现此问题,原因是.NET framework未注册. 3.注册.NET framework
- 4种scope方法
默认作用域,自动加载: default_scope { order(created_at: :desc) } model 调用 find_2时才运行 scope :find_2, ->{ whe ...
- [转]Oracle10g数据库自动诊断监视工具(ADDM)使用指南
第一章 ADDM简介 在Oracle9i及之前,DBA们已经拥有了很多很好用的性能分析工具,比如,tkprof.sql_trace.statspack.set even ...
- HDU4738 tarjan割边|割边、割点模板
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4738 坑点: 处理重边 图可能不连通,要输出0 若求出的结果是0,则要输出1,因为最少要派一个人 #inc ...
- ES5新语法forEach和map及封装原理
### forEach 在es5中提供了forEach方法进行遍历,其实就是模仿了jQuery中each方法,不过将 i 于v进行了调换,下面两种方法进行对比一下 var arr = [ 11, 22 ...