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);
}
}
随机推荐
- Tirgger SimpleTrigger CronTrigger(四)
触发器通用属性: jobKey:表示job实例的标识,触发器被触发时,指定的job实例会被执行. startTime:表示触发器的时间表 首次被触发的时间,他的值类型为java.uti.Date. e ...
- Everything(一款用于检索硬盘文件的工具)
有时候文件夹一多,找不到文件,忘记放哪个盘符怎么办? Everything就能帮你解决,比电脑自带的快多啦,官网在此:http://www.voidtools.com/ (也不大,就几M,没有特别的安 ...
- MySQL多表查询练习题
一.准备数据 #创建表及插入记录 CREATE TABLE class ( cid ) NOT NULL AUTO_INCREMENT, caption ) NOT NULL, PRIMARY KEY ...
- .Net敏捷开发框架6.1.6.2版本,联系QQ:6539471
演示地址:www.fishcmonkey.com .NET敏捷开发框架 6.1.6.2 版本发布 新增手机流程-我的流程(可查看流程进度和表单内容) 新增手机流程-待办任务(可查看流程进度和表单内容, ...
- windows下mysql-5.7.18安装
在官网下载了mysql压缩包,按照官网:https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html的教程安装,但是出错,后面根据 ...
- Owin 自寄宿 asp.net web api
http://owin.org/ Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖, 就是说以后可 ...
- T-SQL:基础练习(杂)
1.返回 每月最后一天订单 SELECT orderid, orderdate, custid, empid FROM Sales.Orders WHERE orderdate = EOMONTH(o ...
- 【java工具】java常用工具
java反编译工具 可以将看不懂的.class文件转换成看得懂的.java文件,这样一来就能很方便的读懂别人编写的java代码. findbugs FindBugs-IDEA插件的使用 阿里巴巴Jav ...
- 了解java虚拟机—G1回收器(9)
G1(Garbage-First)回收器是在JDK1.7中正式使用的全新垃圾回收器,G1拥有独特的垃圾回收策略,从分代上看,G1依然属于分代垃圾回收器,它会区分年代和老年代,依然有eden和survi ...
- UserControl VS TemplatedControl
一:首先来看一下UserControl 熟悉XAML的朋友们都知道,当我们创建一个用户控件的时候,VS会自动为我们生成一个XXX.xaml文件和XXX..xaml.cs文件,XAML文件用于进行控件的 ...