HDU 2822
Dogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2208 Accepted Submission(s): 838
We know the farmland is divided into a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each other in any case, they belong to the same house. Then the little Tim start from his home located at (x0, y0) aim at his friend's home ( x1, y1 ). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but he must dig some distance to reach another house. The farmland will be as big as 1000 * 1000, and the up left corner is labeled as ( 1, 1 ).
..X...
XXX.X.
....X.
X.....
X.....
X.X...
3 5
6 3
0 0
Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).
/*****
题意:给出一个矩阵,然后给出两个点,让求链接这两个点需要打的洞的最小值
‘.’代表空位置,‘X’代表是洞;
做法:bfs + 优先队列
*****/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
using namespace std;
#define maxn 1100
#define INF 1000000
int Edge[maxn][maxn];
char ch[maxn][maxn];
int dx[] = {,,-,};
int dy[] = {,-,,,};
int n,m;
int temp = ;
int sx,sy,ex,ey;
int vis[maxn][maxn];
struct Node
{
int x;
int y;
int step;
Node()
{
x = ;
y = ;
step =;
}
};
struct cmp
{
bool operator () (const Node &a,const Node &b)
{
return a.step>b.step;
}
};
int check(Node a)
{
if(a.x >= && a.x <=n && a.y>= && a.y <=m) return ;
return ;
}
priority_queue<Node,vector<Node>,cmp >que;
int bfs()
{
while(!que.empty()) que.pop();
Node now,tmp;
now.x = sx;
now.y = sy;
now.step = ;
que.push(now);
vis[sx][sy] = ;
while(!que.empty())
{
tmp = que.top();
que.pop();
if(tmp.x == ex && tmp.y == ey) return tmp.step;
for(int i=; i<; i++)
{
now.x = tmp.x + dx[i];
now.y = tmp.y + dy[i];
if(check(now) && vis[now.x][now.y] == )
{
if(Edge[now.x][now.y] == ) now.step = tmp.step;
else
{
now.step = tmp.step + ;
}
que.push(now);
vis[now.x][now.y] = ;
}
}
}
} int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in1.txt","r",stdin);
//#endif
while(~scanf("%d %d",&n,&m))
{
if(n == && m == ) break;
memset(Edge,INF,sizeof(Edge));
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
scanf("%s",ch[i]);
for(int j=; j<m; j++)
{
if(ch[i][j] == 'X') Edge[i][j+] = ;
}
}
getchar();
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
if(Edge[i][j] == ) continue;
Edge[i][j] = min(Edge[i-][j],min(Edge[i+][j],min(Edge[i][j-],Edge[i][j+]))) + ;
}
}
temp = ;
scanf("%d %d",&sx,&sy);
scanf("%d %d",&ex,&ey);
getchar();
int temp = bfs();
printf("%d\n",temp);
}
return ;
}
HDU 2822的更多相关文章
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu 2822 Dogs
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2822 Dogs Description Prairie dog comes again! Someda ...
- hdu - 2822 Dogs (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2822 给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到 . 才需要挖一次. #includ ...
- hdu 2822 Dogs(优先队列)
题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...
- hdu 2822 ~!!!!!!坑死我
首先 在此哀悼... 为我逝去的时间哀悼... 每一步都确定再去写下一步吧...日狗 不过还是有点收获的.. 对优先队列的使用 有了进一步的理解 先上代码 #include<iostrea ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- CF487E Tourists 【圆方树 + 树剖 + 堆】
题目链接 CF487E 题解 圆方树 + 树剖 裸题 建好圆方树维护路径上最小值即可 方点的值为其儿子的最小值,这个用堆维护 为什么只维护儿子?因为这样修改点的时候就只需要修改其父亲的堆 这样充分利用 ...
- 解题:HNOI 2008 玩具装箱
题面 搞了一晚上斜率优化,大概懂了一点,写写 原来常用的优化dp方法:做前缀和,预处理,数据结构维护 现在有转移方程长这样的一类dp:$dp[i]=min(dp[i],k[i]*x[j]+y[j]+c ...
- 【HEOI 2018】制胡窜
转载请注明出处:http://www.cnblogs.com/TSHugh/p/8779709.html YJQ的题解把思路介绍得很明白,只不过有些细节说得还是太笼统了(不过正经的题解就应该这个样子吧 ...
- 【目标检测大集合】R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
R-FCN.SSD.YOLO2.faster-rcnn和labelImg实验笔记 转自:https://ask.julyedu.com/question/7490 R-FCNpaper:https:/ ...
- php求一维数组的排列
<?php class CombinationsGenerator { public function generate(array $list) { if (count($list) > ...
- ACE服务端编程4:ACE跨平台之运行时初始化和关闭
参考APG里的说法:平台差异及不兼容性的一个特别的方面,是对象的运行时初始化和程序关闭时这些对象的相应析构. ACE为了明确管理对象的清理,定义了ACE_Object_Manager类,这个类不仅涉及 ...
- div 当高度较小时指定高度,当高度较大时自适应
在该元素或标签的样式中加入:{min-height:500px;height:auto;},其中min-height:是最小高度,auto是自适应内容.
- Bootstrap自学笔记
<!DOCTYPE html><html lang="zh-cn"> <head> <meta charset="utf-8&q ...
- mysql主从同步碰到的问题
一.mysql 安装https://www.cnblogs.com/jxrichar/p/9248480.html二.主从配置参考https://www.cnblogs.com/superfat/p/ ...
- 「LibreOJ β Round #4」多项式 (广义欧拉数论定理)
https://loj.ac/problem/525 题目描述 给定一个正整数 kkk,你需要寻找一个系数均为 0 到 k−1之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x)modk= ...