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 ...
随机推荐
- 【2016北京集训】Mushroom
Portal --> broken qwq Description 一开始有个蘑菇,蘑菇里面有\(n\)个房间,是一棵有根树,\(1\)号是根,每个房间里面都有杂草,现在要支持以下操作:将某个指 ...
- 如何在Linux系统上安装QQ
转载自KKK博客 首先我们需要在wine的官网上安装一个wine. https://wiki.winehq.org/Ubuntu_zhcn 按照上面的提示一步步来,做完之后点下面的链接下载 https ...
- python并行编程学习之绪论
计算机科学的研究,不仅应该涵盖计算处理所基于的原理,还因该反映这些领域目前的知识状态.当今,计算机技术要求来自计算机科学所有分支的专业人员理解计算机处理的基础的关键,在于知道软件和硬件在所有层面上的交 ...
- bootstrap datetimepicker的参数解释
使用bootstrap datetimepicker(日期时间选择器)的过程中,发现中文参数说明和英文参数说明严重不符,所以结合自己使用的情况和英文参数说明,做了如下翻译. $(".form ...
- Ubuntu 14.04.3 window10双系统情遇到'Disconnected: You are now offline'问题
笔电是win10系统,单独开除50G做了一个Ubuntu系统,安装的是14.04.03版本,安装成功后,发现wifi连接不上,选择wifi并输入密码后提示:“Disconnected: You are ...
- 安装配置hexo icarus主题配置
安装部分配置hexo icarus主题配置 安装icarus 直接下载主题模块放到blog项目 ,blog项目根目录执行 git clone https://github.com/ppoffice/h ...
- Gogent相关问题的解决(不断更新)
1:今天早上打开推特,发现进不去了,google浏览器一直提示404……找不到网址,真心郁闷.后来,查了查,才知道,最近google在北京的主干服务器被xx了,某些省就上不了了…… ……乱七八糟的不说 ...
- extjs 省市县级联
Ext.define('State', { extend: 'Ext.data.Model', fields: [ {type: 'string', name: 'nevalue'}, {type: ...
- ACM选修hust 1075 组合+数学+期望值
Description Input Output Sample Input 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 Sample Output 0.500 1.125 ...
- k8s+docker学习连接汇总
http://guide.daocloud.io/dcs/docker-9153982.html http://www.dczou.com/viemall/802.html https://wangl ...