hdu 2612 Find a way(BFS)
题目链接:hdu2612
思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 205
using namespace std;
char map[N][N];
int v[N][N],ans1[N][N],ans2[N][N],d[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
int x1,x2,y1,y2,n,m,num;
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step > b.step;
}
};
void bfs1(int x,int y,int total)//total表示KFC的总数目
{
memset(ans1,1,sizeof(ans1));
memset(v,0,sizeof(v));
priority_queue <node> q;
node s,temp;
s.x = x;
s.y = y;
s.step = 0;
v[x][y] = 1;
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(map[temp.x][temp.y] == '@')
{
ans1[temp.x][temp.y] = temp.step;
total -- ;
}
if(total == 0) return;//所有的KFC都已经计算过
for(int i= 0 ; i < 4 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || v[s.x][s.y] || map[s.x][s.y] == '#')
continue;
v[s.x][s.y] = 1;
s.step ++;
q.push(s);
}
}
}
void bfs2(int x,int y,int total)
{
memset(ans2,1,sizeof(ans2));
memset(v,0,sizeof(v));
priority_queue <node> q;
node s,temp;
s.x = x;
s.y = y;
s.step = 0;
v[x][y] = 1;
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(map[temp.x][temp.y] == '@')
{
ans2[temp.x][temp.y] = temp.step;
total -- ;
}
if(total == 0) return;
for(int i= 0 ; i < 4 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || v[s.x][s.y] || map[s.x][s.y] == '#')
continue;
v[s.x][s.y] = 1;
s.step ++;
q.push(s);
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
num = 0;
for(i = 0 ; i < n ; i ++)
{
scanf("%s",map[i]);
for(j = 0 ; j < m ; j ++)
{
if(map[i][j] == 'Y')
{
x1 = i;
y1 = j;
}
else if(map[i][j] == 'M')
{
x2 = i;
y2 = j;
}
else if(map[i][j] == '@')
num++;
}
}
bfs1(x1,y1,num);
bfs2(x2,y2,num);
int sum = 10000000;
for(i = 0 ; i < n ; i ++)
for(j = 0 ; j < m ; j ++)
if(map[i][j] == '@')
{
sum = min(sum,ans1[i][j] + ans2[i][j]);
}
printf("%d\n",sum * 11);
}
return 0;
}
hdu 2612 Find a way(BFS)的更多相关文章
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU - 2612 Find a way(BFS搜索)
题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
随机推荐
- HelloCharts pie饼图绘制
先引用hellocharts-library-<version>.jar 然后在layout中设置 <lecho.lib.hellocharts.view.PieChartView ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- C++中cin输入类型不匹配解决方法
#include <iostream> #include <set> using namespace std; int main() { int a; cin>>a ...
- php 序列化储存和转化 json_encode() json_decode($q,true)
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 例如:当需要数据库只有一个 ...
- WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...
- Turbo Sort Add problem to Todo list Problem code: TSORT
def heap_sort(ary): n = len(ary) first = int(n / 2 - 1) for start in range(first, -1, -1): # 3~0 rev ...
- python yield 理解
在别人的代码中看到yield这个关键字,看了几篇资料,说一下个人理解. 包含yield 关键字的函数成为一个迭代器,yield跟return的位置一样,只不过每次返回结果后,并没有退出,而是等待下一次 ...
- Oracle 10.2数据库管理员指南-27章
27使用调度程序 Oracle Database provides database job capabilities through Oracle Scheduler (the Scheduler) ...
- TypeScript开发Vue
用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 目录 背景 解决方案 关于Vue中的计算属性类型 TypeScript的强制类型声明语法 强制类型声明的局限 ...
- CSS3----border-sizing
#wrapper input[type="text"], #wrapper input[type="password"] { /* display: flex; ...