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 ...
随机推荐
- 九度OnlineJudge之1001:A+B for Matrices
题目描述: This time, you are supposed to find A+B where A and B are two matrices, and then count the num ...
- android之interpolator的用法详解
Android:interpolator Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repea ...
- Android的Touch系统简介(一)
一.Android touch事件的相关概念 用户的Touch事件被包装成MotionEvent 用户当前的touch事件主要类型有: ACTION_DOWN: 表示用户开始触摸. ACTION_MO ...
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...
- INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误
在eclipse编译好文件之后,往AVD中安装apk,报错如下:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED一般来说只需要检查AndroidManifest.xml中 ...
- Linq101-Aggregate
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Aggrega ...
- (转)dedecms入门
学dedecms一段时间了,把我的入门体会和大家分享一下. 什么是dedecm cms(内容管理系统):现在有各种内容模型,如书评(包括书名,出版社,评论等字段).cms一般有用户后台,网页的用户可以 ...
- java mail 使用 gmail smtp 发送邮件
smtp 服务器:smtp.gmail.com 使用ssl的端口:465 用户名:username@gmail.com 密码:password** 基本配置没有问题,关键在于Google对安全性要求非 ...
- AutoLayout学习之理解intrinsicContentSize,Content Hugging Priority,Content Compression Resistance Priority
TableViewCell的高度计算应该是所有开发者都会使用到的东西,之前都是用代码计算的方法来计算这个高度.最近有时间看了几个计算Cell高度的方法.基本上都用到了AutoLayout,这篇首先介绍 ...
- CSS 布局Float 【3】
float 属性定义元素在哪个方向浮动. 浮动元素会生成一个块级框,而不论它本身是何种元素. 如果浮动非替换元素,则要指定一个明确的宽度:否则,它们会尽可能地窄. 注释:假如在一行之上只有极少的空间可 ...