hdu2612 Find a way

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
BFS
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int m,n;
int vis[][];
char mapp[][];
int dis[][][];
int dir[][] = {,,,,-,,,-},flag;
struct node{
int x;
int y;
int step;
};
bool judge(int x,int y)
{
if(x>=&&x<m&&y>=&&y<n&&mapp[x][y]!='#'&&vis[x][y]==)
return ;
return ;
}
int BFS(int x,int y)
{
queue<node>q;
node now,next;
now.x=x;
now.y=y;
now.step=;
vis[x][y]=;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
next.step=now.step+;
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(judge(next.x,next.y))
{
vis[next.x][next.y]=;
if(mapp[next.x][next.y]=='@')
dis[next.x][next.y][flag]=next.step;
q.push(next);
}
}
}
}
int main()
{
while(cin>>m>>n)
{
int min=;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
dis[i][j][]=dis[i][j][]=min;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
cin>>mapp[i][j];
}
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(mapp[i][j]=='Y')
{
flag=;
memset(vis,,sizeof(vis));
BFS(i,j);
}
else if(mapp[i][j]=='M')
{
flag=;
memset(vis,,sizeof(vis));
BFS(i,j);
}
}
for(int i=;i<m;i++)
for(int j=;j<n;j++)
if(mapp[i][j]=='@' && min>dis[i][j][]+dis[i][j][])
min=dis[i][j][]+dis[i][j][];
printf("%d\n",min*);
}
}
上一篇:JS之链式运动,及任意值运动框架,包括透明度的改变


下一篇:如何缩减Try{}Catch{}Finally{}代码----定义一个公用的Try{}Catch{}Finally{}