题目大意就是求在特定规则下的最短路,这个规则包含了消除障碍的操作。用BFS感觉选择消除障碍的时候不同路径会有影响,用DFS比较方便状态的还原(虽然效率比较低),因此这道题目采用DFS来写。

写的第一次提交代码是TLE,原因是忘记总步数>10就应该剪枝的条件。AC代码如下:

 #include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = ;
struct Point{
int r, c;
Point(int r=-, int c=-):r(r),c(c){}
};
Point s,t;
int W,H;
int G[maxn][maxn];
const int dr[] = {,-,,};
const int dc[] = {,,-,};
int ans = ;
void dfs(int r,int c,int k){
if(k >= ) return ;
for(int i = ; i < ; i++){
int nr = r;
int nc = c;
int is_walk = ;
while(G[nr+dr[i]][nc+dc[i]]==){
is_walk=;
nr+=dr[i];
nc+=dc[i];
if(nr == t.r && nc == t.c){
ans = min(ans,k+);
return ;
}
}
if(!is_walk)continue;
if(G[nr+dr[i]][nc+dc[i]] == )continue ;
if(G[nr+dr[i]][nc+dc[i]] == ){
G[nr+dr[i]][nc+dc[i]] = ;
dfs(nr,nc,k+);
G[nr+dr[i]][nc+dc[i]] = ;
}
}
}
int main(){
while(scanf("%d%d ", &W, &H) && (W || H)){
ans = ;
for(int i = ; i <= H; i++){
for(int j = ; j <= W; j++)
scanf("%d",&G[i][j]);
}
for(int i = ; i <= W+; i++)
G[][i] = ,G[H+][i] = ;
for(int i = ; i <= H+; i++)
G[i][] = ,G[i][W+] = ;
for(int i = ; i <= H; i++){
for(int j = ; j <= W; j++){
if(G[i][j] == ){
s = Point(i,j);
G[i][j] = ;
}
else if (G[i][j] == ){
t = Point(i,j);
G[i][j] = ;
}
}
}
dfs(s.r,s.c,);
if(ans != && ans <= )printf("%d\n",ans);
else printf("%d\n",-);
}
return ;
}

poj 3009 (深搜求最短路)的更多相关文章

  1. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  2. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. poj 3009 Curling 2.0

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

  4. POJ 3009

    http://poj.org/problem?id=3009 一个搜索的题目: 大意就是一个冰球,在冰面上滑动,你打击一次,就沿一个反向滑动,知道碰到墙就会停下,而墙则会破碎. 求从起点到终点的最短的 ...

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

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

  6. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  7. poj 3216 Repairing Company(最短路Floyd + 最小路径覆盖 + 构图)

    http://poj.org/problem?id=3216 Repairing Company Time Limit: 1000MS   Memory Limit: 131072K Total Su ...

  8. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  9. poj 3009 Curling 2.0( dfs )

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

随机推荐

  1. font(字体)所使用的属性

    1.font-weight:normal blod bolder lighter  100-900之间 400=normal p:first-child{ padding-top: 50px; pos ...

  2. buck型DC-DC分析

    BUCK型DC/DC电源分析 这种buck型DC/DC电路的拓扑结构:( 1N5822叫续流二极管!) LM2756相当于高速断开和闭合的开关,连接在Vin与Vout脚之间. 1. 在开关闭合时,电流 ...

  3. Learning notes | Data Analysis: 1.2 data wrangling

    | Data Wrangling | # Sort all the data into one file files = ['BeijingPM20100101_20151231.csv','Chen ...

  4. web頁面優化以及SEO

    轉載:https://blog.csdn.net/xustart7720/article/details/79960591 浏览器访问优化浏览器请求处理流程如下图: Etag:實體標籤.ETag是HT ...

  5. IIC总线协议和时序

    IIC标准速率为100Kbit/s,快速模式400Kbit/s,支持多机通信,支持多主控模块,但是同一时刻只允许有一个主控.由数据线SDA和时钟SCL构成串行总线:每个电路模块都有唯一地址.I2C设备 ...

  6. 成都优步uber司机第一组与第二组的区别

    成都优步uber司机被分成了两组,两组的奖励方式不相同,下面我们先来看看官方给出的奖励方式: 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最 ...

  7. springBoot RabbitMq 转换json序列化

    package com.alirm.redis_cache.config.RabbitMQ; import org.springframework.amqp.rabbit.core.RabbitTem ...

  8. ORA-15032、ORA-15033—Linux环境

    SQL> alter diskgroup DATA add failgroup DATA_0000 disk '/dev/raw/raw12'; alter diskgroup DATA add ...

  9. Java连接redis集群操作存储、删除以及获取值

    pom文件添加: <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> &l ...

  10. Appium_Python_API说明

    Appium_Python_API 1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话 ...