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. 【BZOJ4828】【HNOI2017】大佬(动态规划)

    [BZOJ4828][HNOI2017]大佬(动态规划) 题面 BZOJ 洛谷 LOJ 人们总是难免会碰到大佬.他们趾高气昂地谈论凡人不能理解的算法和数据结构,走到任何一个地方,大佬的气场 就能让周围 ...

  2. BZOJ2733 永无乡 【splay启发式合并】

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4190  Solved: 2226 [Submit][Sta ...

  3. javaweb之request获取referer请求头实现防盗链

    package test.request; import java.io.IOException; import javax.servlet.ServletException; import java ...

  4. Git config配置

    git获取帮助git help config git config --help man git-config   git config --global user.name "fuleyi ...

  5. 【cdq分治】【CF1093E】 Intersection of Permutations

    传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...

  6. Tensorflow BatchNormalization详解:3_使用tf.layers高级函数来构建带有BatchNormalization的神经网络

    Batch Normalization: 使用tf.layers高级函数来构建带有Batch Normalization的神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴 ...

  7. centos7下安装配置jenkins+git+maven+jdk

    环境 centos7 jdk1.8 maven3 git 在安装jenkins之前,先安装jdk1.8.maven.git 一. 安装jdk1.8 第一步:下载 jdk-8u131-linux-x64 ...

  8. CodeBlocks调试器设置错误问题

    错误如下: Building to ensure sources are up-to-date Selecting target:  Debug ERROR: You need to specify ...

  9. 倍增求lca模板

    倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...

  10. CF540 C BFS 水

    '.'->'X' 前者走后变成后者,后者除了是终点不能再走.初始位置是X很傻的以为这样从初始点走出去后初始位置就变成不能走了,实际上是还能走一次的. 其他就是BFS,路上记得把路变成X就好了 太 ...