题目链接

分析:

本题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. android图片缓存框架Android-Universal-Image-Loader(二)

    http://blog.csdn.net/king_is_everyone/article/details/35595515 这篇打算直接告诉大家怎么用吧,其实这个也不是很难的框架,大致使用过程如下: ...

  2. mongoDB的基本使用----飞天博客

    Mongo的介绍:这个mongoDB官网说的好啊,MongoDB是一个开源的基于document的数据库,并且是优秀的NoSQL数据库,并且它是用C++写滴哈,非常有效率.一些什么特点呢? 全索引支持 ...

  3. Windows下配置Nginx使之支持PHP(转)

    平台描述:Windows下,使用PHP套件 xampp,因为是测试玩,所以没在服务器 Linux 环境中配置. 1. 首先,将 nginx.conf 中的 PHP 配置注释去掉. 01 # pass ...

  4. spring 中StoredProcedure的用法--转载

    StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...

  5. [转] 学习HTML/JavaScript/PHP 三者的关系以及各自的作用

    1.What is HTML? When you write a normal document using a word processor like Microsoft Word/Office, ...

  6. C#操作INI配置文件示例

    源文件地址:http://pan.baidu.com/share/link?shareid=2536126078&uk=1761850335创建如图所示的控件: 源代码: using Syst ...

  7. HDU5311

    题意:给一个指定的字符串a,要求分成三段,然后再给定另外一个字符串b,要求a中的三段能否在b中找到. 思路:枚举+模拟,首先枚举给定的字符串a,因为分成三段,所以一共有(1+9)*9/2种情况,对于分 ...

  8. node.js常用的几个模块总结

    /** 一 util *      是 node 里面一个工具模块 ,node 里面几乎所有的模块 都会用到 在这个模块 *  功能: *      1 实现继承 这是主要功能 *      2 实现 ...

  9. ASP.NET5配置

    ASP.NET5支持各种各样的配置,应用程序配置数据可以来自JSON, XML或者INI格式的文件,也能来自环境变量,你也可以自定义你自己的Configuration Provider. 1. 获取和 ...

  10. Asp.Net中的session配置

    一.InProc模式(缺省模式) <sessionState mode="InProc" timeout="20"></sessionStat ...