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);
}
}
随机推荐
- 实验吧 burpsuie拦截修改请求
Forbidden 解题链接:http://ctf5.shiyanbar.com/basic/header/ 把language改成zh-hk,go,flag到手 Forms 题目地址:http:// ...
- IntelliJ IDEA 2018.3 for Mac 注册码激活
一.前往 jetbrains 官网下载 IDEA Ultimate版本,地址: https://www.jetbrains.com/idea/download/#section=mac 二.安装 ID ...
- Receiver 和 Direct方式的区别
Kafka direct 跟receiver 方式接收数据的区别? Receiver是使用Kafka的高层次Consumer API来实现的.Receiver从Kafka中获取的数据都是存储在Spar ...
- [android] 手机卫士项目
按照模块组织代码的包结构:各个模块之间的业务是独立的 风行网 ----播放器 com.funshion.android.player ----下载模块 com.funshion.android.dow ...
- 搜藏一个php文件上传类
<?php /** * 上传文件类 * @param _path : 服务器文件存放路径 * @param _allowType : 允许上传的文件类型和所对应的MIME * @param _f ...
- (1)Microsoft office Word 2013版本操作入门_常用功能区
word2013界面做了比较大的优化,刚开始用的时候不太习惯,研究了一下win10下word的新版本,记录以下几个功能小技巧: 1.常用功能区: 新打开一个word文档 文件.开始 .插入 等菜单称 ...
- GDB使用技巧
最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...
- minitab 输入一串数字
有时候,我们要向minitab的worksheet输入一串串的数字,很是麻烦. 相如一串数字我们在一个pdf文件存着 那么效率最低的输入方法就是一个一个的输入,"Enter"进入下 ...
- 使用 json-server 模拟数据
1. 先安装 npm install json-server -g 2.查看是否安装成功 json-server -h 3.准备数据,新建一个文件夹 mock,cd mock,在mock下 新建tes ...
- python学习之解决中英文混合输出的排版问题
在python的格式输出时常使用format()方法,其中的多余空间默认使用英文空格作为占位符,不方便中英混合时的格式输出,因此我们要为其中的中文字符串使用占字符较大的中文空格符作为多余空间的占位符. ...