zoukankan      html  css  js  c++  java
  • HDU2612 Find a way (双广搜)

    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);
                     }
            }
     }
  • 相关阅读:
    UML系列图--用例图
    扩展方法
    POJ3254 Corn Fields(状压DP)
    POJ2836 Rectangular Covering(状压DP)
    POJ2817 WordStack(状压DP)
    POJ2441 Arrange the Bulls(状压DP)
    HDU2167 Pebbles(状压DP)
    HDU1561 The more, The Better(树形DP)
    POJ3659 Cell Phone Network(树上最小支配集:树型DP)
    UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/qdu-lkc/p/12247841.html
Copyright © 2011-2022 走看看