题目链接

分析:

本题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. pat 1062. Talent and Virtue (25)

    难得的一次ac 题目意思直接,方法就是对virtue talent得分进行判断其归属类型,用0 1 2 3 4 表示 不合格 sage noblemen foolmen foolmen 再对序列进行排 ...

  2. GiB与GB

    Gibibyte(giga binary byte的缩写)是信息或计算机硬盘存储的一个单位,简称GiB.由来“GiB”.“KiB”.“MiB”等是于1999年由国际电工协会(IEC)拟定了" ...

  3. UIView不能使用UITableView的Static表格的解决方法

    在UIView中嵌入一个Container,用Container来包含UITableViewController即可,到storyboard上显示如下:

  4. js动态添加table 数据tr td

    成果库修改:      要求主题列表随成果类型改变而改变      网上查询资料后开工,在成果类型下拉框添加change()事件触发Dwr,查询主题集合——动态创建/编辑Table      概要代码 ...

  5. 修改Calendar(梅花雨)日历控件 兼容IE9 谷歌 火狐

    修改Calendar日历控件 兼容IE9,谷歌,火狐. 只是能用,出现的位置有所不同,希望有高手再帮我改改吧,谢谢   一. [javascript]   this.iframe     = wind ...

  6. Java编程思想-基于注解的单元测试

    Junit的测试方法命名不一定以test开头 上面介绍的atunit已经很老了,现在junit测试框架已经基本注解了

  7. web开发 关于src跳转

    src跳转看传递的参数值,如果参数值一样就不会再次跳转,也不会向后台的服务器提供request. 想要随时都能跳转可以传递不同的参数,如 onClick="this.src='http:// ...

  8. 【USACO 2.4.5】分数化小数

    [描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...

  9. Linux命令学习计划【sed】

    引言: Sed命令是linux里用于文本行处理的命令. 为了便于说明,我在/usr/dict下创建了字典words并以此作为演示模板 先用nl 打印下words内容: *打印篇: Q1:如何打印某一行 ...

  10. RemoteWebDriver使用说明

    1. 本地代码使用RemoteWebDriver启动: public class Testing { public void myTest()throws Exception { WebDriver ...