HDU2612 Find a way (双广搜)

本文讲述了Yifengfei在杭州的学习旅程中,与好友Merceki相约在市区的一家肯德基(KFC)见面,他们通过最少的时间路径寻找最近的肯德基店。地图上显示了移动成本,两人分别从乡村和城市出发,进行了一次寻找共同目的地的路线优化挑战。
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. 

InputThe 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 
OutputFor 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

题意:求2个点到同一个点的最短路程

注意:某些点可能两个点不能同时到达,所以要用两个数组分别记录两次广搜的路径到达情况

还是强调多个输入,每次都要数组初始化、队列清空

代码:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
class Node{
        int x;
        int y;
        int step;
        public Node(int x,int y,int step){
                this.x=x; this.y=y; this.step=step;
        }
}
public class Main{
        static final int N=205;
        static int n,m;
        static int dx[]={0,0,1,-1};
        static int dy[]={1,-1,0,0};
        static char map[][]=new char[N][N];
        static int s1[][]=new int[N][N];
        static int s2[][]=new int[N][N];
        static boolean vis[][]=new boolean[N][N];
        static ArrayDeque<Node> q=new ArrayDeque<Node>();
        static void init(){
                  for(int i=0;i<N;i++)  Arrays.fill(s1[i], 0);
                  for(int i=0;i<N;i++)  Arrays.fill(s2[i], 0);
        }
        static void bfs(int sx,int sy,int s[][]){
                while(!q.isEmpty()) q.poll();
                vis[sx][sy]=true;
                q.offer(new Node(sx,sy,0));
                while(!q.isEmpty()){
                        Node t=q.poll();
                        for(int i=0;i<4;i++){
                                int xx=t.x+dx[i];
                                int yy=t.y+dy[i];
                                if(xx<0||yy<0||xx>=n||yy>=m ||vis[xx][yy]||map[xx][yy]=='#') continue;
                                vis[xx][yy]=true;
                                s[xx][yy]=t.step+1;
                                q.offer(new Node(xx,yy,t.step+1));
                        }
                        
                }
        }
         public static void main(String[] args) {
                 Scanner scan=new Scanner(System.in);
                 while(scan.hasNext()){
                         n=scan.nextInt();
                         m=scan.nextInt();
                         for(int i=0;i<n;i++) map[i]=scan.next().toCharArray();

                         int yx=0,yy=0,mx=0,my=0;
                         for(int i=0;i<n;i++)
                             for(int j=0;j<m;j++)
                                 if(map[i][j]=='Y'){
                                         yx=i; yy=j;
                                 }
                                 else if(map[i][j]=='M'){
                                         mx=i; my=j;
                                 }
                         
                         init();
                         for(int i=0;i<N;i++)  Arrays.fill(vis[i], false);
                         bfs(yx,yy,s1);
                         for(int i=0;i<N;i++)   Arrays.fill(vis[i], false);
                         bfs(mx,my,s2);
                         
                         int min=2147483647;
                         for(int i=0;i<n;i++)
                             for(int j=0;j<m;j++)
                                 if(map[i][j]=='@' && s1[i][j]!=0 &&s2[i][j]!=0)
                                       min=Math.min(min, s1[i][j]+s2[i][j]);
                         System.out.println(min*11);
                 }
        }
 }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值