首页 技术 正文
技术 2022年11月14日
0 收藏 842 点赞 3,567 浏览 2886 个字

题目代号:HDU 2612

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15919    Accepted Submission(s): 5110

Problem DescriptionPass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. InputThe input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet. Sample Input4 4
Y.#@
….
.#..
@..M
4 4
Y.#@
….
.#..
@#.M
5 5
Y..@.
.#…
.#…
@..M.
#…# Sample Output66
88
66

题目大意:有两个人,位置分别由Y与M代替,@是餐厅的位置,每个人移动到周围的点需要11分钟,现在求两个人到达某个餐厅的时间花费最少。

题目思路:双向bfs遍历所有的位置,分别用两个数组作为标记,如果碰到@就保存当前花费的步数,然后两个数组对应位置相加,然后找到总步数最小的那个的值乘以11,输出就行了

AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;const int MAXM=;
char a[MAXM][MAXM];
int n,m;
int cx[]= {-,,,};
int cy[]= {,,-,};struct node
{
int x,y;
int flag;
int step;
};queue<node>Q;
int vis1[MAXM][MAXM];
int vis2[MAXM][MAXM];void bfs()
{
while(!Q.empty())
{
int x=Q.front().x;
int y=Q.front().y;
int flag=Q.front().flag;
int cnt=Q.front().step;
Q.pop();
for(int i=; i<; i++)
{
int tx=x+cx[i];
int ty=y+cy[i];
if(flag==)
{
if(vis1[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis1[tx][ty]=cnt+;
}
else
{
vis1[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
else
{
if(vis2[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis2[tx][ty]=cnt+;
}
else
{
vis2[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
}
}
}int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d",&n,&m))
{
mem(vis1,);
mem(vis2,);
for(int i=; i<=n; i++)
{
scanf("%s",a[i]+);
for(int j=; j<=m; j++)
{
if(a[i][j]=='Y')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='M')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='.'||a[i][j]=='@')
{
vis1[i][j]=vis2[i][j]=-;
}
}
}
bfs();
/*
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis1[i][j]);
}
puts("");
}
puts("");
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis2[i][j]);
}
puts("");
}
*/
int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
vis1[i][j]+=vis2[i][j];
if(vis1[i][j]>)ans=min(ans,vis1[i][j]);
}
}
cout<<ans*<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289