题目链接

分析:

本题BFS A不了。

00100

00001

01020

00000

00010

00010

00010

00010

00030

对于这样的数据,本来应当是 5 步,但bfs却 4 步。具体原因可以仔细看一下上面的样例。

应当dfs穷举所有的可能,找出最短的。

#include <iostream>
#include <cstdio>
#include <queue> using namespace std; const int maxn = ;
const int INF = (<<); int dx[] = {, -, , };
int dy[] = {, , -, }; int h, w, G[maxn][maxn], min_step; void dfs(int x, int y, int step) {
int nx, ny; if(step >= ) return ; for(int d=; d<; d++) {
nx = x; ny = y;
nx = x+dx[d];
ny = y+dy[d]; if(nx < || ny < || nx >= h || ny >= w) continue ;
if(G[nx][ny] == ) continue; //靠着墙 while(!(G[nx][ny] == || G[nx][ny] == )) {
nx += dx[d];
ny += dy[d];
if(nx < || ny < || nx >= h || ny >= w) break;
} if(nx < || ny < || nx >= h || ny >= w) continue; //这个判断有必要 if(G[nx][ny] == ) { //终点
min_step = min(min_step, step+);
}
else if(G[nx][ny] == ){ //墙
G[nx][ny] = ;
dfs(nx-dx[d], ny-dy[d], step+);
G[nx][ny] = ;
}
}
} int main(){
int sx, sy; while(scanf("%d%d", &w, &h) == ) {
if(w == && h == ) break; min_step = INF; for(int i=; i<h; i++) {
for(int j=; j<w; j++) {
scanf("%d", &G[i][j]);
}
} for(int i=; i<h; i++) {
for(int j=; j<w; j++) {
if(G[i][j] == ) {
sx = i; sy = j;
}
}
} dfs(sx, sy, ); if(min_step != INF) {
printf("%d\n", min_step);
}
else printf("-1\n");
} return ;
}

POJ3009 Curling 2.0(DFS)的更多相关文章

  1. POJ-3009 Curling 2.0 (DFS)

    Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...

  2. POJ3009:Curling 2.0(dfs)

    http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...

  3. poj3009 Curling 2.0 (DFS按直线算步骤)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14563   Accepted: 6080 Desc ...

  4. poj 3009 Curling 2.0 (dfs )

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

  5. 【POJ - 3009】Curling 2.0 (dfs+回溯)

    -->Curling 2.0 直接上中文 Descriptions: 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎.但是规则和我们的有点不同.这个游戏是在一个冰游戏板上玩的,上面有一个正 ...

  6. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  7. poj3009 Curling 2.0(很好的题 DFS)

    https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs ...

  8. Curling 2.0(DFS简单题)

    题目链接: https://vjudge.net/problem/POJ-3009 题目描述: On Planet MM-21, after their Olympic games this year ...

  9. ****Curling 2.0(深搜+回溯)

    Curling 2.0 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

随机推荐

  1. Gson解析json数据(转)

    一. www.json.org这是JSON的官方网站. 首先,我,我们需要在code.google.com/p/google-gson/downloads/list下载JSON的jar包,解析后把gs ...

  2. Android(java)学习笔记243:多媒体之视频播放器

    1.这里我们还是利用案例演示视频播放器的使用: (1)首先,我们看看布局文件activity_main.xml,如下: <RelativeLayout xmlns:android="h ...

  3. Android运行时注解

    Android的注解有编译时注解和运行时注解,本文就介绍下运行时注解. 其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用fin ...

  4. Python简易爬虫

    经常需要下载论文,每次都需要去网页上搜索,然后点击下载,实在麻烦,正好最近刚入门Python,心血来潮,想着写一个爬虫 经过一天查阅资料,基本算是完成了,但是还是不足,比如对知网和万方暂时还不行,但是 ...

  5. Java中的浅复制和深复制 Cloneable clone

    先看一个简单案例 public class Test {     public static void main(String args[]) {         Student stu1 = new ...

  6. ie下面兼容性问题的一些总结

    最后一次搞ie兼容性问题,以后都可以不管了0.0 1.浮动兼容性 1.1IE6下的双边距BUG 在IE6下,块元素有浮动和横向margin的时候,最边上元素的横向margin值会被放大成两倍 解决办法 ...

  7. Ci 分页类的所有属性总结

    //#######################自定义分页 $config['uri_segment'] = 3;//分页方法自动测定你 URI 的哪个部分包含页数 $config['num_lin ...

  8. (转)JS的parent对象

    ---http://blog.sina.com.cn/s/blog_a15aa5690101a5yz.html top:该变更永远指分割窗口最高层次的浏览器窗口.如果计划从分割窗口的最高层次开始执行命 ...

  9. MySQL被Oracle并购后的409个日日夜夜

    2009年4月20日,Oracle并购了Sun,这也意味着MySQL归属到甲骨文的旗下.四百多天过去了,究竟这场并购结局如何?请看本文. 去年对Sun的收购,让甲骨文顺利的将一个潜在的对手MySQL收 ...

  10. 你好,C++(9)坐216路公交车去买3.5元一斤的西红柿——C++中如何表达各种数值数据 3.3 数值数据类型

    3.3  数值数据类型 从每天早上睁开眼睛的那一刻开始,我们几乎每时每刻都在与数字打交道:从闹钟上的6点30分,到上班坐的216路公共汽车:从新闻中说的房价跌到了100元每平米到回家买菜时的西红柿3. ...