HDUOJ----(2612)Find a way

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3177    Accepted Submission(s): 1031

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,以Y和M为中心搜索
代码:
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
using namespace std;
const int maxn=;
typedef struct Map
{
char ss;
int x,y;
int value;
}gmap;
unsigned int dir[][]={{,},{-,},{,},{,-}}; /*·½Ïò*/
gmap bb[maxn][maxn];
int ans[maxn][maxn];
int n,m;
void bfs1(gmap a[][],int x,int y)
{
deque<gmap>q;
gmap temp;
q.push_back(a[x][y]);
while(!q.empty())
{
temp=q.front();
q.pop_front();
for(int i=;i</*&&temp.ss!='@'*/;i++)
{ if((temp.x+dir[i][])<||(temp.x+dir[i][])>=n||(temp.y+dir[i][])<||(temp.y+dir[i][])>=m) continue;
if(a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='.'||a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='@')
{
a[temp.x+dir[i][]][temp.y+dir[i][]].value+=temp.value+;
q.push_back(a[temp.x+dir[i][]][temp.y+dir[i][]]);
if(a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='@')
{
a[temp.x+dir[i][]][temp.y+dir[i][]].ss='d';
ans[temp.x+dir[i][]][temp.y+dir[i][]]+=a[temp.x+dir[i][]][temp.y+dir[i][]].value;
}
else
a[temp.x+dir[i][]][temp.y+dir[i][]].ss='r';
}
}
}
} int main()
{
int i,j,sx1,sy1,sx2,sy2;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(ans,,sizeof(ans));
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%c",&bb[i][j].ss);
bb[i][j].value=;
bb[i][j].x=i;
bb[i][j].y=j;
if(bb[i][j].ss=='Y')
{
sx1=i;
sy1=j;
}
else if(bb[i][j].ss=='M')
{
sx2=i;
sy2=j;
}
}
getchar();
}
bfs1(bb,sx1,sy1);
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
bb[i][j].value=;
if(bb[i][j].ss=='d')
bb[i][j].ss='@';
else
if(bb[i][j].ss=='r')
bb[i][j].ss='.';
}
}
bfs1(bb,sx2,sy2);
int minm=INT_MAX;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(bb[i][j].ss=='d'&&minm>ans[i][j])
minm=ans[i][j];
}
}
printf("%d\n",minm*);
}
return ;
}
上一篇:在Java 线程中返回值的用法


下一篇:【Docker】Docker容器中安装vim命令