nsoj L. Find a way

                                         Time Limit: 1000msMemory Limit: 32768KB 64-bit integer IO format: Java class name:
                                                                                    Submit Status PID: 1624


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<iostream>    
#include<queue>    
#include<cstring> 
#define inf 0x7f7f7f7
using namespace std;    
              
typedef struct    
{    
    int x ,y; //坐标    
    int step; //步数   
 }node;
char map[210][210];  //地图       
int vis_Y[210][210],vis_M[210][210];  //  可以储存Y的一个队列    
int vis[210][210];   
int end_step[210][210];    
int dx[4] = {0,1,0,-1}; //记录方向   
int dy[4] = {1,0,-1,0};    
int n,m;    
             
int bfs(node &t , int i , int flag)    
{    
     t.x += dx[i];    
     t.y += dy[i];    
     if(t.x<0||t.x>=n||t.y<0||t.y>=m) return 0; //判断是否越界    
     if(flag)    
         if(vis_Y[t.x][t.y]) return 0;  //已经访问    
     if(!flag)    
         if(vis_M[t.x][t.y]) return 0;  //已经访问    
     if(map[t.x][t.y] == '#')  return 0;  //前路不通无法访问 
     else if(map[t.x][t.y] == '.')  ///可以访问  
     {    
         if(flag) vis_Y[t.x][t.y] = 1;      
         else vis_M[t.x][t.y] = 1;    
     }    
     else if(map[t.x][t.y] == '@')  //KFC  
     {    
         if(flag) vis_Y[t.x][t.y] = 1;    
         else vis_M[t.x][t.y] = 1;    
         vis[t.x][t.y]++;    
         end_step[t.x][t.y] += t.step+1;    
     }    
     t.step++;    
     return 1;    
 }    
 int main()    
 {    
     int i , j , min;    
     while(cin>>n>>m)    
     {    
         memset(vis_Y , 0 , sizeof(vis_Y)); //数组初始化   
         memset(vis_M , 0 , sizeof(vis_M));    
         memset(vis , 0 , sizeof(vis));    
         memset(end_step , 0 , sizeof(end_step));    
         min = inf;    
         queue<node>q_Y; //定义队列  
         queue<node>q_M;    
         node start_Y , start_M; //相当于head  
         for(i = 0; i < n; i++)    
         {    
             getchar();    
             for(j = 0; j < m; j++)    
             {    
                 scanf("%c" , &map[i][j]);  //读取地图  
                 if(map[i][j] == 'Y')    
                 {start_Y.x = i; start_Y.y = j; start_Y.step = 0; vis_Y[i][j] = 1;} 
				 //记录Y的初始位置,   
                 else if(map[i][j] == 'M')    
                 {start_M.x = i; start_M.y = j; start_M.step = 0; vis_M[i][j] = 1;}    
             }    
             map[i][j] = '\0';    
         }    
         q_Y.push(start_Y); //入队   
         q_M.push(start_M);    
         while(!q_Y.empty() && !q_M.empty()) //判断是否为空   
         {    
             int flag = 0;    
             node  t1 = q_Y.front(); //返回第一个被放置的元素    
             node  t2 = q_M.front();    
             q_Y.pop();  q_M.pop();   //出队(移除一个元素) 
             for(i = 0; i < 4; i++)    
             {    
                 node t3 = t1;  //记录第一个元素  
                 node t4 = t2;    
                 if(bfs(t3 , i , 1))  //搜索  
                     q_Y.push(t3);    
                 if(bfs(t4 , i , 0))    
                     q_M.push(t4);    
             }    
         }    
         for(i = 0; i < n; i++)    
             for(j = 0; j < m; j++)    
                 if(vis[i][j] == 2)     
                 {    
                     if(min > end_step[i][j])  //判断每组的最短路径权值和最小的  
                         min = end_step[i][j];    
                 }    
         cout<<min*11<<endl;    
     }    
     return 0;    
 }




你可能感兴趣的:(算法,搜素)