Find a way<hdoj2612>

来源:互联网 发布:湖北农村金融数据 编辑:程序博客网 时间:2024/05/20 00:35
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
6688

66

普通访法会超时 先打表列出各个点到@的距离

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#define  INF  0x3f3f3f3fusing namespace std;char map[1001][1001];int vis[1001][1001];int n,m;int dx[4]={0,1,-1,0};int dy[4]={1,0,0,-1};int dis[2][1001][1001];struct node{int hang ,lie;int step;};void bfs(int h,int x,int y){memset(vis,0,sizeof(vis));struct   node start;vis[x][y]=1;start.hang =x;start.lie =y;start.step =0;queue<node>q;q.push(start);while(!q.empty() ){struct node tmp=q.front() ;q.pop() ;if(map[tmp.hang ][tmp.lie ]=='@'){dis[h][tmp.hang ][tmp.lie ]=tmp.step ;}struct node tmp2;  for(int i=0;i<4;i++)  {  tmp2.hang =tmp.hang +dx[i];  tmp2.lie =tmp.lie +dy[i];  if(tmp2.hang >=0&&tmp2.lie >=0&&tmp2.lie <m&&tmp2.hang <n&&vis[tmp2.hang ][tmp2.lie ]!=1&&map[tmp2.hang ][tmp2.lie ]!='#')  {  vis[tmp2.hang ][tmp2.lie ]=1;  tmp2.step =tmp.step +1;  q.push(tmp2);   }  } } }int main(){         int x1,x2,y1,y2;while(scanf("%d%d",&n,&m)!=EOF){    memset(dis,INF,sizeof(dis));   memset(map,0,sizeof(map));for(int i=0;i<n;i++)    {   scanf("%s",map[i]);   for(int j=0;j<m;j++)   {   if(map[i][j]=='M')   {   x1=i,y1=j;   }   if(map[i][j]=='Y')   {   x2=i,y2=j;   }   }     }     int sum=INF;     bfs(0,x1,y1);     bfs(1,x2,y2);      for(int i=0;i<n;i++)      {      for(int j=0;j<m;j++)      {   if(map[i][j]=='@')   {   sum=min(sum,dis[0][i][j]+dis[1][i][j]);   }  }  }  printf("%d\n",sum*11);}return 0;}


0 0
原创粉丝点击