Dogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2208    Accepted Submission(s): 838

Problem Description
Prairie dog comes again! Someday one little prairie dog Tim wants to visit one of his friends on the farmland, but he is as lazy as his friend (who required Tim to come to his place instead of going to Tim's), So he turn to you for help to point out how could him dig as less as he could.

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 ).

 
Input
The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.
 
Output
For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.
 
Sample Input
6 6
..X...
XXX.X.
....X.
X.....
X.....
X.X...
3 5
6 3

0 0

Sample Output
3

Hint

Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).

 
Source
/*****
题意:给出一个矩阵,然后给出两个点,让求链接这两个点需要打的洞的最小值
‘.’代表空位置,‘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的更多相关文章

  1. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  2. hdu 2822 Dogs

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2822 Dogs Description Prairie dog comes again! Someda ...

  3. hdu - 2822 Dogs (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2822 给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到  . 才需要挖一次. #includ ...

  4. hdu 2822 Dogs(优先队列)

    题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...

  5. hdu 2822 ~!!!!!!坑死我

    首先 在此哀悼...  为我逝去的时间哀悼...  每一步都确定再去写下一步吧...日狗 不过还是有点收获的..  对优先队列的使用 有了进一步的理解 先上代码 #include<iostrea ...

  6. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  7. HDU——T 2818 Building Block

    http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. 【数学】【背包】【NOIP2018】P5020 货币系统

    传送门 Description 在网友的国度中共有 \(n\) 种不同面额的货币,第 \(i\) 种货币的面额为 \(a[i]\),你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为 \(n ...

  2. 《剑指offer》— JavaScript(12)数值的整数次方

    数值的整数次方 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 思路一 考察指数的正负以及底数是否为零的几种情形: 将指数转换 ...

  3. CSK & KCF(tracking)

    转自:http://blog.csdn.net/ben_ben_niao/article/details/51364323 上次介绍了SRDCF算法,发展历史轨迹为CSK=>>KCF/DC ...

  4. POJ--2752

    原题链接:http://poj.org/problem?id=2752 分析:no! #include<cstdio> #include<cstring> #include&l ...

  5. VLFeat在matlab和vs中安装

    转:http://blog.csdn.net/u011718701/article/details/51452011 博主最近用vlfeat库做课题,网上搜索使用方法,一大片都会告诉你说:run(/v ...

  6. hdu 2608 (数论)

    hdu2608  0 or 1 题意:给你一个数N(N < 2^31), 问从 1--N 所有数的因子和S(N),求 S(N)%2 的值. 链接:http://acm.hdu.edu.cn/sh ...

  7. 问题分析 - 电容的ESR

    ESR,是Equivalent Series Resistance三个单词的缩写,翻译过来就是“等效串连电阻” 理论上,一个完美的电容,自身不会产生任何能量损失,但是实际上,因为制造电容的材料有电阻, ...

  8. socket--接受大数据

    一.简单ssh功能 1.1 实现功能 在前面的一篇博客中,我们已经实现了一个简单的类似Linux服务器ssh功能的小程序,可以输入系统命令来返回命令运行结果,今天我们也以此开始,看看socket如何来 ...

  9. 前端PHP入门-023-重点日期函数之程序执行时间检测

    我们有的时经常需要做程序的执行时间执行效率判断. 实现的思路如下: <?php //记录开始时间 //记录结整时 // 开始时间 减去(-) 结束时间 得到程序的运行时间 ?> 可是大家不 ...

  10. libc、glibc与gcc

    转http://blog.163.com/dragon_sjl@126/blog/static/100473339201107101517380/ 1.gcc(gnu collect compiler ...