POJ3009 Curling 2.0(DFS)
题目链接。
分析:
本题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)的更多相关文章
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- POJ3009:Curling 2.0(dfs)
http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...
- poj3009 Curling 2.0 (DFS按直线算步骤)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14563 Accepted: 6080 Desc ...
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- 【POJ - 3009】Curling 2.0 (dfs+回溯)
-->Curling 2.0 直接上中文 Descriptions: 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎.但是规则和我们的有点不同.这个游戏是在一个冰游戏板上玩的,上面有一个正 ...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- poj3009 Curling 2.0(很好的题 DFS)
https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs ...
- Curling 2.0(DFS简单题)
题目链接: https://vjudge.net/problem/POJ-3009 题目描述: On Planet MM-21, after their Olympic games this year ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
随机推荐
- .Net Framework 4.0安装cmd命令
在安装系统以后和.Net FrameWork 后,通过cmd编译编写的程序时总是提示编译错误.可以通过cmd命令安装相应的.net framework版本. 具体步骤如下: 1.以管理员身份打开cmd ...
- pythonQQ邮件
http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html http://blog.csdn.net/imzoer/art ...
- Android 模仿微信启动动画(转)
本文内容 环境 项目结构 演示微信启动动画 本文演示微信启动动画.请点击此处下载,自行调试. 顺便抱怨一下,实践性(与研究性质的相对)技术博的“七宗罪”: 第一宗罪,错字连篇,逻辑不清: 第二宗罪,文 ...
- Swift还是Objective-C
Swift还是Objective-C Swift还是Objective-C? Swift语言发布已经两年了,iOS开发需要学习C或者Objective-C.此外,人们似乎还在迷惑Swift到底适合 ...
- 每天一条Linux命令(OS X系统上操作)
Linux菜鸟必学的60个命令 安装和登录命令:login.shutdown.halt.reboot.install.mount.umount.chsh.exit.last: 文件处理命令:file ...
- RxJava RxAndroid【简介】
资源 RxJava:https://github.com/ReactiveX/RxJava RxAndroid :https://github.com/ReactiveX/RxAndroid 官网:h ...
- oracle 的变量的定义和赋值
第一种 :先定义后赋值 代码 : declare V_AgeingType varchar2(500); begin V_AgeingType :='111'; end 第二种 ...
- Asp.net Core 部署到Azure.cn的一个小问题
前一段尝试在azure.cn上部署Aps.net Core未成功,报503错误!在网上查到是Azure.cn的问题,未能完美支持Asp.net Core! Asp.net Core发表正式版了,又尝试 ...
- (多对象)Json转换成List
写的不好,请大家见谅. 1.Json 格式{"packages":[{“type”:”aaa”}],"zone_packages":[{"ticket ...
- Wpf Binding.Path设置
Binding.Path 获取或设置绑定源属性的路径. 每个绑定通常都具有四个组件:绑定目标对象.目标属性.绑定源,以及要使用的绑定源值的路径.有关这些数据绑定概念的更多信息,请参见数据绑定概述. 使 ...