<更新提示>

<第一次更新>


<正文>

ice(USACO)

Description

Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000。

湖面上的N(1 <= N <= 20,000)个位置有石块(编号分别为1到N),其它位置是冰面。

由于Bessie滑冰技术不够好,她通过推动自己旁边的石块,依靠反作用力向某一个方向前进,在碰到一个新的石块之前,Bessie是不会停下来的。(当然,最后会停留在某块石块的前一个格子里)由于Bessie无法计算复杂的角度,她只能够向东南西北四个方向前进。

很显然,Bessie不能够穿越石块,因此,Bessie仅仅可以向三个方向滑。

滑冰不是没有风险,Bessie滑向某个方向后必须能碰到某个石块,因此她必须很小心。

考虑下面的一个情况,Bessie希望到达在她东面的目标位置(x=5,y=1),(. = 冰块,* = 石头, B = Bessie, G = 目的位置)如果她直接向东滑,那么她会滑过目标位置,因为她通过撞上某块石头来停下来,一个能够到达目标位置的方案是这样的:


   (a)              (b)             (c)              (d)

4 .....*.         .....*.         .....*.          .....*.

3 ..*....  slide  ..*....  slide  ..*....   slide  ..*....

2 ......*  north  ..B...*  east   .....B*   south  ......*

1 .*B..G. ------> .*...G. ------> .*...G.  ------> .*...B.

0 *....*.         *....*.         *....*.          *....*.

在(a)中,Bessie 只能朝向北,东,或者南三个方向,但是只有在北面能撞上石头从而停下来,在(b)中,类似地,她只能向东走。

对于输入,石头 i位于坐标为X_i,Y_i的位置,(-1,000,000,000<= X_i <= 1,000,000,000; -1,000,000,000 <= Y_i <= 1,000,000,000),没有任何两块石头位于同一个位置,Bessie从Bx,By的位置出发(出发点一定与某个石头相邻),Bessie的目标位置是Gx,Gy(-1,000,000,000 <= Gx <= 1,000,000,000; -1,000,000,000 <= Gy <=1,000,000,000).

Bessie 并不介意长时间滑冰,但是,不停地推石头依靠反作用力前进很累。FJ 非常关心Bessie的健康,因此他希望知道Bessie最少要推多少次石头才能到达终点。

Input Format

  • 第一行: 五个用空格隔开的整数: N, Bx, By, Gx, and Gy

  • 第二行到第N+1行: 第i+1行用两个空格隔开的整数来描述第i个石头

Output Format

  • 第一行: 一个整数表示Bessie至少要推多少次石头才能够到达终点

Sample Input

6 2 1 5 1
5 4
2 3
1 1
6 2
5 0
0 0

Sample Output

3

解析

简单地来看的话,这道题显然是一个广搜,每一个状态之间的转移的花费都是\(1\)。

但是问题就是这个"地图"太稀疏了,石头个数只有\(20000\),而坐标却全是\(int\)范围内的,需要考虑离散化一下。

二维离散化?这样太复杂了些,最好的办法是利用\(STL\)。我们利用\(map\)和\(set\)建立形如map< int,set<int> >这样的一对多的索引数组。对于每一行,我们存下行内所有石头的纵坐标,对于每一列,我们也存下列内所有石头的横坐标。然后对于每一个位置我们就能广搜了。利用\(set\)自带的二分查找函数方便地找到第一个碰撞到的石头,然后就能得到下一个状态了。

当然,记录当前状态的最小花费的数组也是用\(map\)的。

\(Code:\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define mset(name,val) memset(name,val,sizeof name)
#define filein(str) freopen(str".in","r",stdin)
#define fileout(str) freopen(str".out","w",stdout)
#define node pair<int,int>
#define x first
#define y second
const int N=20020;
int n;
node Begin,End;
map < node,int > dis;
map < int,set < int > > row,col;
inline void input(void)
{
scanf("%d%d%d%d%d",&n,&Begin.x,&Begin.y,&End.x,&End.y);
for(int i=1;i<=n;i++)
{
node t;
scanf("%d%d",&t.x,&t.y);
row[t.x].insert(t.y);col[t.y].insert(t.x);
}
}
inline node findup(node temp)
{
set < int > s=col[temp.y];
set < int > :: iterator it=s.lower_bound(temp.x);
if(it==s.begin()||temp.x-(*(--it))<=1)
return temp;
return (node){(*it)+1,temp.y};
}
inline node finddown(node temp)
{
set < int > s=col[temp.y];
set < int > :: iterator it=s.upper_bound(temp.x);
if(it==s.end()||(*it)-temp.x<=1)
return temp;
return (node){(*it)-1,temp.y};
}
inline node findright(node temp)
{
set < int > s=row[temp.x];
set < int > :: iterator it=s.upper_bound(temp.y);
if(it==s.end()||(*it)-temp.y<=1)
return temp;
return (node){temp.x,(*it)-1};
}
inline node findleft(node temp)
{
set < int > s=row[temp.x];
set < int > :: iterator it=s.lower_bound(temp.y);
if(it==s.begin()||temp.y-(*(--it))<=1)
return temp;
return (node){temp.x,(*it)+1};
}
inline void bfs(void)
{
queue < node > q;
q.push(Begin);
dis[Begin]=0;
while(!q.empty())
{
node temp=q.front();q.pop();
if(temp==End)break;
node next=findup(temp);
if(next!=temp&&!dis[next])
{
dis[next]=dis[temp]+1;
q.push(next);
}
next=finddown(temp);
if(next!=temp&&!dis[next])
{
dis[next]=dis[temp]+1;
q.push(next);
}
next=findleft(temp);
if(next!=temp&&!dis[next])
{
dis[next]=dis[temp]+1;
q.push(next);
}
next=findright(temp);
if(next!=temp&&!dis[next])
{
dis[next]=dis[temp]+1;
q.push(next);
}
}
}
int main(void)
{
filein("ice.");
fileout("ice.");
input();
bfs();
printf("%d\n",dis[End]);
return 0;
}

<后记>

『ice 离散化广搜』的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  3. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  4. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  5. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  8. 双向广搜 codevs 3060 抓住那头奶牛

    codevs 3060 抓住那头奶牛 USACO  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold   题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...

  9. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

随机推荐

  1. FlaskWeb开发:基于Python的Web应用开发实战

    所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/63/ 来源:python黑洞网,专注 ...

  2. 【数据结构】KMP算法

    我还是不太懂... 转2篇大神的解释    1>https://www.cnblogs.com/yjiyjige/p/3263858.html     2>https://blog.csd ...

  3. CF987B - High School: Become Human

    Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But ...

  4. 从零开始学python

    自从20世纪90年代初Python语言诞生至今,它已被广泛应用于系统管理任务的处理和Web编程.今天就来给大家看看学Python的五大优势吧! NO.1 全球三大主流编程语言之一 python是一种面 ...

  5. wsl 子系统 用户目录位置

    C:\Users\DELL\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalS ...

  6. 数据分析 大数据之路 五 pandas 报表

    pandas:  在内存中或对象,会有一套基于对象属性的方法,   可以视为 pandas 是一个存储一维表,二维表,三维表的工具, 主要以二维表为主 一维的表, (系列(Series)) 二维的表, ...

  7. 我的 FPGA 学习历程(08)—— 实验:点亮单个数码管

    数码管是一种常见的用于显示的电子器件,根据数码管大致可以分为共阴极和共阳极两种,下图所示的是一个共阳极的数码管的电路图(摘自金沙滩工作室的 51 开发板电路图),我的 AX301 开发板与这张图的情况 ...

  8. 读 Spring实战 遇到的问题记录(一)

    package soundsystem; import beanConfig.CDPlayerConfig; import org.junit.Rule; import org.junit.Test; ...

  9. input标签实现小数点后两位保留小数

    短短一行代码就可以实现 <input type="number" min="0" max="100" step="0.01& ...

  10. sketch2code 有的叫screenshot to code什么的

    先mark一下项目,回头再深究 https://github.com/mzbac/sketch2code https://www.floydhub.com/emilwallner/datasets/h ...