HDU 2612 Find a way(Java)

題目:http://acm.hdu.edu.cn/showproblem.php?pid=2612

題目大意說一下,有一個地圖上面有兩個人,要去KFC見面,然後需要求出二者到KFC的最短距離之和,這原本是一個簡簡單單的bfs搜索,但是兩個人的出現,讓它加了點難度

思路:第一次搜索從Y開始,然後求出他到達每個KFC的距離,然後打個表出來,存着備用,然後從M開始搜索,在求出他到達每個KFC的距離,將這兩次的結果加起來找出其中最短的

坑:這裏有個問題,讓我wa了一上午,連中午的泡麪都不香了,原本按照上面的思路,是頂呱呱的,但是有一組實例會有問題

5 5
Y..#@
...M#
.....
.....
@....

如果按照上面的思路,這裏就會是0,因爲存在一家KFC不能到達!
所以我們需要在篩選的時候判斷一下,去除最後結果中爲0的部分,然後再去尋找最短的那個即可

import java.util.LinkedList;
import java.util.Scanner;

public class Main {

    private static String[] map;
    private static int[][] visited;
    private static int m;
    private static int n;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            m = sc.nextInt();
            n = sc.nextInt();
            map = new String[m];
            for(int i = 0; i < m; i++) {
                map[i] = sc.next();
            }
            visited = new int[m][n];
            int num = 0;
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    visited[i][j] = 0;
                    if(map[i].charAt(j)=='@') {
                        num++;
                    }
                }
            }
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(map[i].charAt(j)=='Y') {
                        bfs(i, j, 0);
                    }
                }
            }
            int[] kfc = new int[num];
            int index = 0;
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(map[i].charAt(j)=='@') {
                        kfc[index] = visited[i][j];
                        index++;
                    }
                    visited[i][j] = 0;
                }
            }
            index = 0;
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(map[i].charAt(j)=='M') {
                        bfs(i, j, 0);
                    }
                }
            }
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(map[i].charAt(j)=='@') {
                        kfc[index] = visited[i][j] + kfc[index];
                        index++;
                    }
                    visited[i][j] = 0;
                }
            }
            int min = 10000005;
            for(int i = 0; i < kfc.length; i++) {
            	//去除爲零的部分,尋找最短的
                if(kfc[i]!=0&&kfc[i]<min) {
                    min = kfc[i];
                }
            }
            long result = min * 11;
            System.out.println(result);
        }
    }
    
    public static void bfs(int x, int y, int step) {
        
        //定義移動需要用到的數組
        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};
        //用集合冒充隊列應該沒人發現吧
        LinkedList<node> queue = new LinkedList<node>();
        node no = new node(x, y, 0);
        visited[x][y] = 1;
        queue.addLast(no);
        while(queue.size()!=0) {
            node now = queue.removeFirst();
            if(map[now.getX()].charAt(now.getY())=='@') {
                visited[now.getX()][now.getY()] = now.getStep();
            }
            for(int i = 0; i < 4; i++) {
                int xx = now.getX() + dx[i];
                int yy = now.getY() + dy[i];
                //下一個節點在合法範圍內且沒有訪問過
                if((xx>=0)&&(xx<m)&&(yy>=0)&&(yy<n)&&(visited[xx][yy]==0)) {
                    if(map[xx].charAt(yy)=='.'||map[xx].charAt(yy)!='#') {
                        queue.addLast(new node(xx, yy, now.getStep()+1));
                        visited[xx][yy]=1;
                    }
                }
            }
        }
    }
}
class node{
    private int x;
    private int y;
    private int step;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getStep() {
        return step;
    }
    public void setStep(int step) {
        this.step = step;
    }
    public node(int x, int y, int step) {
        super();
        this.x = x;
        this.y = y;
        this.step = step;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章