题目大意:

输入n,(x,y);n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置

接下来n行每行一个坐标(a,b);为各个草堆的坐标

输出拖拉机要回到原点(0,0)需要移动的草堆数量

Sample Input

7 6 3
6
2
5 2
4 3
2 1
7 3
5 4
6 4

Sample Output

1

Hint

INPUT DETAILS:

The tractor starts at
(6,3).  There are 7 bales of hay, at positions (6,2), (5,2), (4,3), (2,1),
(7,3), (5,4), and (6,4).

OUTPUT
DETAILS:

Farmer John only needs to
remove one bale of hay to free his tractor.

 
直接从拖拉机的位置往外广搜整个矩阵 遇到边界就结束  遇到边界前的路径中最少草堆的一条有几个
优先队列升序排序可直接取出当前草堆数量最少的一条路
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <queue>
#include <functional>
using namespace std;
struct NODE
{
int sp,x,y;
NODE(){};
NODE(int spi,int xi,int yi):
sp(spi),x(xi),y(yi){};
bool operator<(const NODE& p)const{
return sp>p.sp; /// 升序排序这里应该是 大于号!!
}
};
int mov[][]={{,},{,-},{,},{-,}};
int G[][];
int st,ed,n;
bool bound(int x,int y)
{
return x==||y==||x==||y==;
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%d%d",&st,&ed);
memset(G,,sizeof(G));
for(int i=;i<=n;i++)
{
int a,b; scanf("%d%d",&a,&b);
G[a][b]=;
} priority_queue < NODE > q;
q.push(NODE(,st,ed));
G[st][ed]=-;
while(!q.empty())
{
NODE tmp=q.top(); q.pop(); if(bound(tmp.x,tmp.y))
{
printf("%d\n",tmp.sp);
break;
}//printf("%d %d %d\n",tmp.x,tmp.y,tmp.sp); for(int i=;i<;i++)
{
int nowx=tmp.x+mov[i][],
nowy=tmp.y+mov[i][],
nowsp=tmp.sp; if(G[nowx][nowy]==-) continue; if(G[nowx][nowy]) nowsp++;
G[nowx][nowy]=-; q.push(NODE(nowsp,nowx,nowy));
//printf("%d %d %d\n",nowx,nowy,nowsp);
}
}
} return ;
}

USACO 2012 March Silver Tractor /// 优先队列BFS oj21567的更多相关文章

  1. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  2. USACO翻译:USACO 2014 MARCH Silver三题

    USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooo ...

  3. [USACO 2012 Mar Silver] Landscaping【Edit Distance】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126 好题啊好题,一开始就输给了这道题的想法! 先把原始状态以及目标状态换 ...

  4. [USACO 2012 Jan Silver] Bale Share【DP】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=107 没想到太不应该了,真的不应该啊! f[i][j][k]表示前i个包, ...

  5. [USACO 2012 Jan Silver] Delivery Route【拆点】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=106 这道题还真是完全没思路,真的不知道怎么做,但是看了题解后恍然大悟. ...

  6. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  7. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. poj 3254 Corn Field

    Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  2. NX二次开发-UFUN多按钮模态对话框UF_UI_message_dialog

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //多按钮模态对话框 ; char title_ ...

  3. 移动端多选插件-jquery

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. MPU-6000 与 MPU-6050

    VLOGIC 是什么呢?

  5. 2014 0416 word清楚项目黑点 输入矩阵 普通继承和虚继承 函数指针实现多态 强弱类型语言

    1.word 如何清除项目黑点 选中文字区域,选择开始->样式->全部清除 2.公式编辑器输入矩阵 先输入方括号,接着选择格式->中间对齐,然后点下面红色框里的东西,组后输入数据   ...

  6. Deep Dive into Neo4j 3.5 Full Text Search

    In this blog we will go over the Full Text Search capabilities available in the latest major release ...

  7. 1.springboot+ActiveMQ

    1.项目结构如下 pom.xml文件如下 <dependencies> <dependency> <groupId>junit</groupId> &l ...

  8. MySQL更新指定分组中最大值记录

    数据表中存储着某个状态字段,当分组中所有数据入库后,需要根据相应的最大值更新状态字段,如表: 现在要将no=11的分组中把最大值记录的status更新为1,可以直接这么写: ; done~

  9. protobuf文档翻译-安装,数据格式及编码规范

    Install Download protobuf: https://github.com/protocolbuffers/protobuf/releases unzip protoc-3.8.0-l ...

  10. 查看ubuntu系统的版本信息

    显示如下 Linux version 4.10.0-28-generic (buildd@lgw01-12) linux内核版本号 gcc version 5.4.0 20160609 gcc编译器版 ...