冰壶2.0

  题目大意:就是给你一个冰壶和一个地图,地图上有石头,冰壶只能沿着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)的更多相关文章

  1. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  2. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  3. 【POJ】3009 Curling 2.0 ——DFS

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Desc ...

  4. 【POJ - 3009】Curling 2.0 (dfs+回溯)

    -->Curling 2.0 直接上中文 Descriptions: 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎.但是规则和我们的有点不同.这个游戏是在一个冰游戏板上玩的,上面有一个正 ...

  5. poj 3009 Curling 2.0

    题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...

  6. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  7. POJ 3009:Curling 2.0 推箱子

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14090   Accepted: 5887 Desc ...

  8. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  9. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

随机推荐

  1. 【HDU 1228】A + B

    题 Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出.    Input 测试输入包含若干测试用例,每个测试用例占一行,格式 ...

  2. 11.Android之常用对话框AlertDialog学习

    (1)首先我们写个简单的AlertDialog对话框,要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,然后创建对话框可以设置对话框的属性,比如 ...

  3. opencv笔记3:trackbar简单使用

    time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...

  4. 中间件、MetaQ入门学习

    目录 . 中间件技术 . MetaQ中间件 . MetaQ编程实践 1. 中间件技术 0x1: 中间件简介 中间件(Middleware)是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的 ...

  5. 单机redis多端口实例+keepalived高可用

    一.实验环境说明 192.168.115.21(keepalived+redis) 192.168.115.95(keepalived+redis) VIP:192.168.115.99 二.安装re ...

  6. 搭建的SSH 框架

    公用JDBC 方法,如果要保存数据,不许再service 中写,而且必须带save*  update* 的方法名才受事物控制,ajax 返回json 控制,登录拦截器, 用户体系我没有建立,个人需要不 ...

  7. HttpClient教程

    2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...

  8. Python socket编程之五:更新分时图

    f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ########## ...

  9. Python序列的切片操作与技巧

    切片操作 对于具有序列结构的数据来说,切片操作的方法是:consequence[start_index: end_index: step]. start_index: 表示是第一个元素对象,正索引位置 ...

  10. ExtJS学习之路第七步:contentEl与renderTo的区别

    上回在Panel的应用中我们应该能大致区分开conteEl和renderTo,这回我们从定义中区分. 在Panel的API中, contentEl:String指定一个现有的HTML元素或者id作为此 ...