POJ 3009 Curling 2.0 {深度优先搜索}
原题
$On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.
Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.
The movement of the stone obeys the following rules:
At the beginning, the stone stands still at the start square.
The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
Once thrown, the stone keeps moving to the same direction until one of the following occurs:
The stone hits a block (Fig. 2(b), (c)).
The stone stops at the square next to the block it hit.
The block disappears.
The stone gets out of the board.
The game ends in failure.
The stone reaches the goal square.
The stone stops there and the game ends in success.
You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.
Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.
With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).$
这次我就不翻译了。简单的说,你的球在方框里面滚动。注意是滚动不是一格一格地移动,仅仅有遇到障碍才会停止。
有相邻的4个方向能够移动。不能是斜对角等方向。
球假设出界了,则失败;假设滚动了10次以上相同失败。
主要就是这个意思,看看图3就懂了。
思路
这道题和之前有一道一样,都是关于广度优先搜索的,大家能够看看这个:POJ 1979 Red and Black(红与黑)
这里在二维数组中移动的方向和之前的定义一样,图示:
首先须要输入二维数组:
for (int row = 0; row < H; ++row) {
for (int col = 0; col < W; ++col) {
cin >> room[row][col];
}
}
找到当中为2的即为起点。将当前的row和col复制对应的起始点(start),然后结束循环。
// 为2的点为起始点
for (int row = 0; row < H; ++row) {
for (int col = 0; col < W; ++col) {
if (room[row][col] == 2) {
sRow = row;
sCol = col;
break;
}
}
}
由于此时已经不须要这个起始点了,将其又一次设置为0。表示能够走。由于有完毕的步数要求。所以须要加上一个推断,超过10此输出-1。当中的dfs函数为核心。
room[sRow][sCol] = 0;
minStep = 11;
dfs(sRow, sCol, 0);
if (minStep > 10) {
minStep = -1;
}
// 输出结果
cout << minStep << endl;
在dfs函数的开头须要做推断。
if (step >= 10 || step > minStep) {
return;
}
这里的d有4个值,表示4个方向(上、右、下、左)。之所以当前的(current)row和col都在for循环開始处,是由于假设走到不能走的地方能够马上返回并又一次获得当前(原本)的位置。
for (int d = 0; d < 4; ++d) {
int cRow = row;
int cCol = col;
}
不管怎样都得让点处于该范围之中。其次是推断这个点表示的意思。
while (cRow >= 0 && cRow < H && cCol >= 0 && cCol < W) {
switch (room[cRow][cCol]) {
}
}
0表示为空,所以继续往该(d)方向走。
case 0: {
cRow += direc[d][1];
cCol += direc[d][0];
break;
}
3表示为终点,假设step加上当前步骤比之前最小的步数还小。将其赋值给最小步数。
case 3: {
if (step + 1 < minStep) {
minStep = step + 1;
}
cRow = -1;
break;
}
1表示为障碍,还原走多的这一步然后进行下一次递归,步数加1。位置还原。
case 1: {
if (!(cRow - direc[d][1] == row && cCol - direc[d][0] == col)) {
room[cRow][cCol] = 0;
dfs(cRow - direc[d][1], cCol - direc[d][0], step + 1);
room[cRow][cCol] = 1;
}
cRow = -1;
break;
}
默认的情况。
default: {
break;
}
代码
#include <iostream>
using namespace std;
// 题目中给出的最大宽度和高度
#define MAX_W 20
#define MAX_H 20
// 待输入的宽度和高度以及已走的步数
int W, H;
int step = 0;
int minStep;
int sRow, sCol;
// 待写入的二维数组
int room[MAX_W][MAX_H];
// 顺时针的可走方向
const int direc[4][2] = {
{ 0, -1 },
{ 1,0 },
{ 0, 1 },
{ -1 ,0 },
};
void dfs(const int& row, const int& col, int step) {
if (step >= 10 || step > minStep) {
return;
}
for (int d = 0; d < 4; ++d) {
int cRow = row;
int cCol = col;
while (cRow >= 0 && cRow < H && cCol >= 0 && cCol < W) {
switch (room[cRow][cCol]) {
case 0: {
cRow += direc[d][1];
cCol += direc[d][0];
break;
}
case 3: {
if (step + 1 < minStep) {
minStep = step + 1;
}
cRow = -1;
break;
}
case 1: {
if (!(cRow - direc[d][1] == row && cCol - direc[d][0] == col)) {
room[cRow][cCol] = 0;
dfs(cRow - direc[d][1], cCol - direc[d][0], step + 1);
room[cRow][cCol] = 1;
}
cRow = -1;
break;
}
default: {
break;
}
}
}
}
}
int main()
{
while (cin >> W >> H, W > 0) {
// 输入
for (int row = 0; row < H; ++row) {
for (int col = 0; col < W; ++col) {
cin >> room[row][col];
}
}
// 为2的点为起始点
for (int row = 0; row < H; ++row) {
for (int col = 0; col < W; ++col) {
if (room[row][col] == 2) {
sRow = row;
sCol = col;
break;
}
}
}
room[sRow][sCol] = 0;
minStep = 11;
dfs(sRow, sCol, 0);
if (minStep > 10) {
minStep = -1;
}
// 输出结果
cout << minStep << endl;
}
}
POJ 3009 Curling 2.0 {深度优先搜索}的更多相关文章
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- 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
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- 【POJ】3009 Curling 2.0 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ P3009 Curling 2.0 题解
深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙.到达终点就输出,没到就回溯. #include<iostream> #include<cstring> #include ...
随机推荐
- BZOJ3376: [Usaco2004 Open]Cube Stacking 方块游戏
[传送门:BZOJ3376] 简要题意: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000 ...
- PowerDesigner删除外键关系,而不删除外键列[转] 及编码格式
PowerDesigner删除外键关系,而不删除外键列[转] 数据库 database -> generate database ->format 设置为utf-8 PowerDesi ...
- 互联网金融研究组:P2P借贷平台:性质、风险与监管(上)
互联网金融研究组(): P2P借贷平台:性质.风险与监管(上) 目 录 一.性质与合法性 1. P2P网络借贷 1.1 概念重新界定 1.2 发展概况与特点 2. 延伸模式及其合法性浅析 2. ...
- Vue总结(一)
vue总结 构建用户界面的渐进式框架 渐进式:用到什么功能即可使用转么的框架子模块. 两个核心点 向应的数据绑定 当时图发生改变->自动跟新视图,利用Object.defindProperty中 ...
- 为什么我们须要复杂的password
前两天我打开邮箱一看.收到公司1331一封要求改动邮箱password的邮件. 为什么我们须要一个复杂的password呢?尽管我一直以来设置的password都非常复杂.可是公司这次要求改动pass ...
- CentOS下编译安装Apache
与Apache 2.2.x相比,Apache 2.4.x提供了很多性能方面的提升,包括支持更大流量.更好地支持云计算.利用更少的内存处理更多的并发等.除此之外,还包括性能提升.内存利用.异步 I/O的 ...
- 小项目: low版本的 员工信息程序:
### 附加两个文件1 user_info 和worker_info flag = False def logon(): #登录函数 global flag usr = input('Username ...
- Oracle新建表字段,如何使字段自增
oracle的自增需要依靠序列和触发器共同实现 比如 新建一张表 create table test (id int primary key, name varchar2(10)); 创建一个序列 ...
- tomcat:web容器
Web容器和应用服务器除了你所说的request response之外还有很多其他重要的功能. 1. 多线程和并发处理,估计你写的socket程序支持不了几个并发访问吧 2. 安全性 3. 生命周期管 ...
- java(面向对象 )
java面向对象的语言 对象:真实存在唯一的事物. 类:实际就是对某种类型事物的共性属性与行为的抽取. 面向对象的计算机语言核心思想: 找适合的对象做适合的事情. 如何找适合的对象: 1.sun已经定 ...