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. Oracle修改字段类型方法总结

    有一个表名为tb,字段段名为name,数据类型nchar(20). 1.假设字段数据为空,则不管改为什么字段类型,可以直接执行:alter table tb modify (name nvarchar ...

  2. Mac下Jenkins+SVN+Xcode构建持续

    1 安装Jenkins Jenkins是基于Java开发的一种持续集成工具.所以呢,要使用Jenkins必须使用先安装JDK. JDK安装 JDK 下载地址 jdk 1.8.png 安装JDK的过程略 ...

  3. LPC1769 CAN的自测试模式

    一.背景 客户要了一块单路CAN的板子,他希望在没有其他板子的情况下进行自行测试,然后按照我写的 APP选择自收发测试选项,却无法接收到发送的信息,但是外接了一块板子就可以接收到自己发送的 信息:由于 ...

  4. 第3月第15天 afconvert lame

    1. //CAF 转换成MP3 (可以) afconvert -f mp4f -d aac -b 128000 /Users/amarishuyi/Desktop/sound1.caf/Users/a ...

  5. 博客整理——Alpha版冲刺

    Alpha冲刺 助教链接:2016福州大学软件工程第五.六次团队作业-Alpha阶段成绩汇总 1.Transcend Daily Scrum Meeting --FirstDay Daily Scru ...

  6. 静态界面传值javascript

    一:JavaScript静态页面值传递之URL篇能过URL进行传值.把要传递的信息接在URL上.Post.htm 复制代码代码如下: <input type="text" n ...

  7. Deep Residual Learning

    最近在做一个分类的任务,输入为3通道车型图片,输出要求将这些图片对车型进行分类,最后分类类别总共是30个. 开始是试用了实验室师姐的方法采用了VGGNet的模型对车型进行分类,据之前得实验结果是训练后 ...

  8. getPx function

    function getPX(str){  return str.substring(0,str.indexOf('px'));}

  9. Decode Ways

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  10. 《PHP数组函数》笔记

    ① in_array() 检查数组中是否存在某个值;有两个参数,第一个参数是要查找的值,第二个参数是数组名,返回值为布尔,找到则ture否则false; ② array_search 在数组中搜索给定 ...