DFS:Curling 2.0(POJ 3009)

题目大意:就是给你一个冰壶和一个地图,地图上有石头,冰壶只能沿着x方向和y方向运动,并且要一直运动直到撞到石头为止,并且沿着此方向撞过来会把挡住的石头撞没,冰壶在停的时候可以扔出去一次,最多只能扔10次,问你冰壶能否到指定点?
这一题原题非常长,需要很细心地读题(比如我一开始的时候就是没看到只能扔10次,导致好几次tle),而且这一题一开始我还做复杂了,一开始的时候用的是dfs+dp去储存最短距离,可是我发现冰壶会撞烂石头,所以储存最短距离是不行的,所以只能直接dfs了
直接dfs的方法就是沿着一个方向,如果没有障碍物,我们就继续往下一个搜索,如果遇到障碍物,那么我们就在这个方向再沿着4个方向再搜索(如果没有石头挡着的话),遇到终点我们就更新minstep,如果扔的次数大于10次,那么就不用再搜索了,minstep初始化为11就好了
PS:这一题一开始我是256ms过的(因为我用的INT_MAX返回来标记的,非常耗时间),后来删掉了返回值,直接用step标记,速度直接快了一倍,然后还更简洁了
另外WA好几次,还是边界判断的问题,看来我还是要多注意一下

#include <iostream>
#include <algorithm> using namespace std;
typedef int Direction;//0表示左,1表示右,2表示上,3表示下
typedef int Position; static int w, h;
static int map[][];//0联通,1阻挡,2起点,3终点
static pair< int, int >start;
static pair< int, int >goal;
int minstep; void Search(const Position, const Position, const Direction, const int);
void Find_Ans(Position, Position, Direction, const int);
void Read_Map(const int,const int); int main(void)
{
while (~scanf("%d%d", &w, &h))
{
if (w == && h == )
break;
Read_Map(h, w);
minstep = ;
for (int i = ; i < ; i++)
{
if (i == && start.second - >= && map[start.first][start.second - ] != )
Search(start.first, start.second, i, );
else if (i == && start.second + < w && map[start.first][start.second + ] != )
Search(start.first, start.second, i, );
else if (i == && start.first - >= && map[start.first - ][start.second] != )
Search(start.first, start.second, i, );
else if (i == && start.first + < h && map[start.first + ][start.second] != )
Search(start.first, start.second, i, );
}
if (minstep > )
cout << - << endl;
else
cout << minstep << endl;
}
return ;
} void Read_Map(const int h, const int w)
{
for (int y = ; y < h; y++)
{
for (int x = ; x < w; x++)
{
scanf("%d", &map[y][x]);
if (map[y][x] == )
{
start.first = y;//y坐标
start.second = x;//x坐标
}
if (map[y][x] == )
{
goal.first = y;//y坐标
goal.second = x;//x坐标
}
}
}
} void Find_Ans(Position y, Position x, Direction dir,const int step)
{
for (int i = ; i < ; i++)
{
if (i == && x - >= && map[y][x - ] != )
Search(y, x - , i, step + );
else if (i == && x + < w && map[y][x + ] != )
Search(y, x + , i, step + );
else if (i == && y - >= && map[y - ][x] != )
Search(y - , x, i, step + );
else if (i == && y + < h && map[y + ][x] != )
Search(y + , x, i, step + );
}
} void Search(const Position y, const Position x, const Direction dir,const int step)
{
if (step > minstep)
return;
if (x == goal.second && y == goal.first)//如果这个点就是正确的点,返回0步
{
minstep = min(minstep, step);
return;
}
if (dir == )
{
if (x - < )
return;
else if (map[y][x - ] != )
Search(y, x - , dir, step);
else
{
map[y][x - ] = ;//先撞烂
Find_Ans(y, x, dir, step);
map[y][x - ] = ;//然后补回来
}
}
else if (dir == )
{
if (x + >= w)
return;
else if (map[y][x + ] != )
Search(y, x + , dir, step);
else
{
map[y][x + ] = ;//先撞烂
Find_Ans(y, x, dir, step);
map[y][x + ] = ;//然后补回来
}
}
else if (dir == )
{
if (y - < )
return;
else if (map[y - ][x] != )
return Search(y - , x, dir, step);
else
{
map[y - ][x] = ;//先撞烂
Find_Ans(y, x, dir, step);
map[y - ][x] = ;//然后补回来
}
}
else
{
if (y + >= h)
return;
else if (map[y + ][x] != )
Search(y + , x, dir, step);
else
{
map[y + ][x] = ;//先撞烂
Find_Ans(y, x, dir, step);
map[y + ][x] = ;//然后补回来
}
}
}
DFS:Curling 2.0(POJ 3009)的更多相关文章
- 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 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- 【POJ - 3009】Curling 2.0 (dfs+回溯)
-->Curling 2.0 直接上中文 Descriptions: 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎.但是规则和我们的有点不同.这个游戏是在一个冰游戏板上玩的,上面有一个正 ...
- poj 3009 Curling 2.0
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 3009:Curling 2.0 推箱子
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14090 Accepted: 5887 Desc ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
随机推荐
- 【SGU 104】Little shop of flowers
题意 每个花按序号顺序放到窗口,不同窗口可有不同观赏值,所有花都要放上去,求最大观赏值和花的位置. 分析 dp,dp[i][j]表示前i朵花最后一朵在j位置的最大总观赏值. dp[i][j]=max( ...
- Codevs2157 配对
题目描述 Description 给出2个序列A={a[1],a[2],…,a[n]},B={b[1],b[2],…,b[n]},从A.B中各选出n个元素进行一一配对(可以不按照原来在序列中的顺序), ...
- rqnoj71 拔河比赛
题目描述 superwyh的学校要举行拔河比赛,为了在赛前锻炼大家,老师决定把班里所有人分为两拨,进行拔河因为为锻炼所以为了避免其中一方的实力过强老师决定以体重来划分队伍,尽 量保持两个队伍的体重差最 ...
- Bsoj 1322 第K小数
第K小数 Description 现在已有N个整数,你有以下三种操作: 1 A:表示加入一个值为A的整数: 2 B:表示删除其中值为B的整数: 3 K:表示输出这些整数中第K小的数: Input 第一 ...
- SQL增加、删除、更改表中的字段名
1. 向表中添加新的字段 ) not null 2. 删除表中的一个字段 delete table table_name column column_name 3. 修改表中的一个字段名 alter ...
- ActivityInfo taskAffinity
通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...
- TP中二维数组的遍历输出
例子分析 <volist name="list" id="vo"> <volist name="vo['sub']" id ...
- PHP输出表格的方法
<?php function table($n) //几行几列表 { echo'<table border="1" width="500" heig ...
- linux 运行级别与chkconfig
一.Linux的运行级别 在装MySQL的时候,才知道了Linux的运行级别这么一回事.汗…自己太水了…下面总结一下: 什么是运行级别呢?简单点来说,运行级别就是操作系统当前正在运行的功能级别.级别是 ...
- ASP.NET MVC中通过Request.IsAjaxRequest()来判断是否要加载公共视图
个人目测 Request.IsAjaxRequest()这个东西是判断前台提交过来的header中的 X-Requested-With:XMLHttpRequest来区分是不是ajax请求的. ASP ...