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 ...
随机推荐
- 【poj1236】 Network of Schools
http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...
- codevs1003 电话连线
题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...
- p2p软件如何穿透内网进行通信
http://blog.chinaunix.net/uid-22326462-id-1775108.html 首先先介绍一些基本概念: NAT(Network Address Translators) ...
- JAVA敏捷开发环境搭建
前面介绍了创业型软件公司的工作模式,这里详细介绍下如何实施,第一步是先要搭建环境,有了环境才能开展工作. 整个软件项目分为四个环境 开发本地环境.开发环境.测试环境.IDC环境.和传统C++开发不一样 ...
- AI顶级会议以及期刊
AI顶级会议以及期刊 Upcoming Top Conferences NIPS 2009 UAI 2009 ICML 2009 COLT 2009 AISTATS 2009 CVPR 2009 IC ...
- cmd+lcx+nc+sc提权工具总结
cmd:执行命令的载体cmdshell lcx:端口映射工具 1.在自己的host上的cmd下运行:lcx.exe -listen 51 3389 //意思是监听51端口并转发到3389端口 2.在服 ...
- Jquery 表单操作
文本框,文本区域: 获取值: 1.$("#txt").attr("value"); 2. $("txt").val(); 单选按钮: 获取值 ...
- Visual Studio Online Integrations-Build and release
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-dire ...
- We7<001>--We7 CMS之报错: HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用。
根据教程--http://wenku.baidu.com/link?url=8_Jcl0TY-n1RPWRBzfvrFZNrik0YIrqJAE_IFbNk-ibqLA7kZIkOu1efaYvmGr ...
- Spring AOP使用整理:各种通知类型的介绍
2.PersonImpl类的源码 public class PersonImpl implements Person { private String name; private int age; p ...