Find a way
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 Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66 主要思想:不能直接用枚举,这样容易超时(不信可以试试)。用bfs把每一个位置遍历,发现@就记录一下。其中也有一些问题,比如怎么对每一个@进行区别,和如果有一个@两个人都不能到达或者有一个人不能到达怎么办(这个需要读题仔细)。
解决方法:用二维数组,对@位置进行记录,再用一个一维数组判断是不是两个人都到了。
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 10000
using namespace std;
typedef pair<int,int> ee;
bool vis[209][209];
int t[4][2]={0,1,1,0,-1,0,0,-1};
int d[209][209];
char a[209][209];
int b[1009][1009];
int g[1009][1009];
queue<ee> que;
int n,m,k;
int tx,ty,sx,sy,mx,my;
{
while(!que.empty()) que.pop();
return;
}
{
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
d[x][y]=0;
que.push(ee(x,y));
vis[x][y]=true;
{
ee exa=que.front();
que.pop();
if(a[exa.first][exa.second]=='@')
{
b[exa.first][exa.second]=b[exa.first][exa.second]+d[exa.first][exa.second];
g[exa.first][exa.second]++;
}
for(int i=0;i<4;i++)
{
tx=exa.first+t[i][0];
ty=exa.second+t[i][1];
if(a[tx][ty]=='#') continue;
if(vis[tx][ty]) continue;
vis[tx][ty]=true;
d[tx][ty]=d[exa.first][exa.second]+1;
// printf("%d\n",d[tx][ty]);
}
}
clean();
return;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
k=0;
memset(b,INF,sizeof(b));
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
for(int j=1;j<=m;j++)
{
if(a[i][j]=='Y'){sx=i;sy=j;}
if(a[i][j]=='M'){mx=i;my=j;}
if(a[i][j]=='@'){b[i][j]=0;}
}
}
bfs(sx,sy);
bfs(mx,my);
int x1=1,x2=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(b[i][j]<b[x1][x2]&&g[i][j]==2)
{
b[x1][x2]=b[i][j];
}
}
}
printf("%d\n",b[x1][x2]*11);
}
}
随机推荐
- 详细的最新版fastdfs单机版搭建
前言 目前项目是tomcat单机部署的,图片.视频也是上传到tomcat目录下,关键是此项目的主要内容还就是针对图片.视频的,这让我非常担忧:文件服务器的应用是必然的,而且时间还不会太久.之前一直有听 ...
- 根据运算符优先级解析SQL规则表达式
1.需求 测试数据库使用Greenplum,生产库使用GBase 普通表:存储客户数据,千万级别,结构如下 stat_date代表日期:user_id代表用户id:serial_number代表手机号 ...
- PHP算法之快速排序、冒泡排序
快速排序 <?php Class Sort { //快速排序 public function quickly($array) { //判断排序的数组是否大于1 if (count($array) ...
- openssl genrsa
openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html genrsa用于生成RSA私钥,不会生成公钥,因为公钥提取自私钥,如果需要查 ...
- springboot使用遇到问题:Class “model.Address” is listed in the persistence.xml file but not mapped
报错如下: 解决如下: 这是一个Eclipse的怪癖.我最近在创建一个禁用了JPA库配置的新JPA项目时遇到了这个问题,但是在我通过Eclipse New JPA Entity向导创建实体之前没有手动 ...
- 用Visual Studio Code写Node.j
介绍 vsc的宣传语是: 一个运行于 Mac OS X.Windows和 Linux 之上的,针对于编写现代 Web 和云应用的跨平台源代码编辑器. 按它说的,vsc特别适合来作为前端开发编辑器. 内 ...
- 【手记】.net正则行尾匹配符$的问题
本来想用正则Split一下sql语句中简单场景的的GO,于是用^GO$(配合忽略大小写和多行模式),可居然连这种情况都搞不掂: go 如果删掉$就能匹配了,但这显然不是办法,遂又在VS的C#交互窗口. ...
- Oracle11g em启动报此网站的安全证书有问题的解决方案
http://blog.sina.com.cn/s/blog_a32eff280101cgje.html C:\>emctl status dbconsoleOracle Enterprise ...
- Visual Studio 代码风格约束
团队内部若能统一代码风格对于日后的项目维护大有裨益,但面对厚达十几甚至几十页的代码风格规范,开发人员难免产生抵触心理.Python和Go等在语言层面就对代码风格作了一定的约束,但C#并没有,为解决这个 ...
- UML速记
依赖:虚线箭头 关联:实线箭头 接口:虚线三角 父类:实线三角 聚合:空心菱形 组合:实心菱形 顺着箭头方向: 依赖于和什么关联是什么的子类是什么的接口的实现是什么的聚合是什么的组合