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 ...
随机推荐
- 【BZOJ1497】【NOI2006】最大获利(网络流)
[BZOJ1497][NOI2006]最大获利(网络流) 题面 BZOJ Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS& ...
- Function.caller, arguments.caller, arguments.callee, arguments.callee.calller
Function.caller指向当前函数的调用者,是arguments.caller的替代者 arguments.caller也是指向当前函数的调用者,已被废弃 arguments.callee是对 ...
- iframe在ios上不能滚动问题解决
同这篇文章一样,用到了-webkit-overflow-scrolling: touch;属性.主要解决方案是在iframe外层添加一个div,然后设置-webkit-overflow-scrolli ...
- Linux常用网络工具:批量主机服务扫描之netcat
netcat又叫做瑞士军刀,是黑客和系统管理员常用的网络工具,最初开发的目的是文件传输,后来发展出很多强大的功能,比如也可以完成批量主机服务扫描. 之前介绍了另一个更常用的批量主机服务扫描工具:nma ...
- echart图跟随屏幕自适应变化
var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...
- html跑马灯效果
实现跑马灯的方法很多,其中最简单的是采用一句Html代码来实现,我们在需要出现跑马灯效果的地方插入“<marquee>滚动的文字</marquee>”语句,它的效果如下所示: ...
- ② 设计模式的艺术-02.简单工厂(Simple Factory)模式
工厂模式 实现了创建者和调用者的分离. 详细分类:简单工厂模式.工厂方法模式.抽象工厂模式 面向对象设计的基本原则 OCP(开闭原则,Open-Closed Principle):一个软件的实体应当对 ...
- BestCoder Round #41 记。
大概整个过程都是很绝望的吧. 发现自己在七点之前是肯定搞不定网了..有冲动跑到机房去打 但是又不喜欢那样的气氛 这可是shi的场呢...好难过啊... 后来..好像是在和lyd讨论怎么把网络复原的过程 ...
- .NET中的异常和异常处理
.NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception. 可以通过编写一个继承自Exception的类的方式,自定义异常类! 异常 ...
- 深入理解Spring系列之五:BeanDefinition装载
转载 https://mp.weixin.qq.com/s/1_grvpJYe8mMIAnebMdz9Q 接上篇<深入理解Spring系列之四:BeanDefinition装载前奏曲>,进 ...