Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4772    Accepted Submission(s): 1624

Problem Description
Pass 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.
 
Input
The 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
 
Output
For 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
 
 
 
 
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define min(a,b)(a>b?b:a)
char map[1010][1010];
int vis1[1010][1010],vis2[1010][1010],ans1[1010][1010],ans2[1010][1010];
int m,n;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
struct node
{
int x,y;
int step;
}p,temp;
int judge(node s,int vis[1010][1010])
{
if(s.x<0||s.x>=m||s.y<0||s.y>=n)
return 1;
if(vis[s.x][s.y])
return 1;
if(map[s.x][s.y]=='#')
return 1;
return 0;
}
void dfs(int x,int y,int ans[1010][1010],int vis[1010][1010])
{
p.x=x;
p.y=y;
p.step=0;
vis[x][y]=1;
ans[x][y]=p.step;
queue<node>q;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
if(judge(temp,vis))
continue;
temp.step=p.step+1;
ans[temp.x][temp.y]=temp.step;
q.push(temp);
vis[temp.x][temp.y]=1;
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
int i,j,x1,y1,x2,y2;
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
if(map[i][j]=='Y')
{
x1=i;y1=j;
}
if(map[i][j]=='M')
{
x2=i;y2=j;
}
}
}
memset(vis1,0,sizeof(vis1));
memset(ans1,0,sizeof(ans1));
dfs(x1,y1,ans1,vis1);
memset(vis2,0,sizeof(vis2));
memset(ans2,0,sizeof(ans2));
dfs(x2,y2,ans2,vis2);
int ans=0xfffffff;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j])
{
ans=min(ans,ans1[i][j]+ans2[i][j]);
}
}
printf("%d\n",ans*11);
}
return 0;
}

最新文章

  1. selenium 关于富文本的处理
  2. GT考试(bzoj 1009)
  3. &lt;我是一只IT小小鸟&gt;读书笔记
  4. 前端优化:RequireJS Optimizer 的使用和配置方法
  5. android post请求
  6. How to Diagnose Audi Vehicles via Tuirel S777
  7. 在fedora 桌面上添加应用程序
  8. C语言头文件的作用
  9. NOI2011 兔农
  10. PHP升级之后$SESSION丢失
  11. C#操作Xml:如何定义Xsd文件
  12. 一个看起来不像中年人的中年人,带着两个初出茅庐的小伙子儿,用git管理项目代码的进击之路
  13. Sql的基础知识(一)
  14. jquery基础总结 -- 转载
  15. GreenDao教程2
  16. javaScript中的redirect
  17. checkbox选中相关问题总结
  18. ES6_入门(3)_顶层对象属性
  19. Hive为什么要启用Metastore?
  20. SSM综合练习

热门文章

  1. java与安卓中的回调callback学习笔记
  2. windows和linux无法访问VMware中linux的tomcat主页问题
  3. HDU_2149_基础博弈sg函数
  4. coredata示意图
  5. 关于vuex
  6. &quot; \t\r\n\f&quot;是什么意思
  7. redis得配置及使用
  8. Java子类对于父类中static方法的继承
  9. BZOJ 2959: 长跑 LCT_并查集_点双
  10. c++ STL - priority_queue优先队列详解