[알고리즘] 백준 - 로봇 청소기

June·2021년 4월 28일
0

알고리즘

목록 보기
191/260

로봇 청소기

다른 사람 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int h;
    static int w;
    static char[][] map;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static Pair[] dirty;
    static int size;
    static int res;
    static Pair start;
    static int[][] graph;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        loop:while(true) {
            String[] str = br.readLine().split(" ");
            h = Integer.parseInt(str[1]);
            w = Integer.parseInt(str[0]);
            dirty= new Pair[11];
            res = Integer.MAX_VALUE;
            size = 1;

            if(h==0 && w==0)
                return ;

            map = new char[h][w];
            start = new Pair(0, 0, 0);

            for(int i=0; i<h; i++) {
                String input = br.readLine();

                for(int j=0; j<w; j++) {
                    map[i][j] = input.charAt(j);

                    if(map[i][j]=='o')
                        start = new Pair(i, j, 0);

                    if(map[i][j]=='*') {
                        dirty[size] = new Pair(i, j, 0);
                        size++;
                    }
                }
            }
            dirty[0] = start;
            graph = new int[size][size];

            for(int i=0; i<size-1; i++) {
                for(int j=i+1; j<size; j++){
                    int len = bfs(dirty[i], dirty[j]);
                    if(len==-1) {
                        System.out.println(-1);
                        continue loop;
                    }

                    graph[i][j] = len;
                    graph[j][i] = len;
                }
            }

            boolean[] selected = new boolean[size];
            int[] list = new int[size];
            selected[0] = true;
            list[0] = 0;
            permu(selected, list,  1);
            System.out.println(res);
        }
    }

    static void permu(boolean[] selected, int[] list,  int idx) {
        if(idx==size) {
            int sum = 0;

            for(int i=0; i<size-1; i++) {
                sum += graph[list[i]][list[i+1]];
            }

            res = Math.min(res, sum);
            return;
        }

        for(int i=0; i<size; i++) {
            if(!selected[i]) {
                selected[i] = true;
                list[idx] = i;
                permu(selected, list, idx+1);
                selected[i] = false;
            }
        }
    }

    static int bfs(Pair start, Pair end) {
        Queue<Pair> queue = new LinkedList<>();
        boolean[][] visited = new boolean[h][w];
        visited[start.x][start.y] = true;
        queue.add(new Pair(start.x, start.y, 0));

        while(!queue.isEmpty()) {
            Pair p = queue.poll();

            for(int i=0; i<4; i++) {
                int X = p.x+dx[i];
                int Y = p.y+dy[i];

                if(X<0 || Y<0 || X>=h || Y>=w || map[X][Y]=='x' || visited[X][Y]) continue;

                if(X==end.x && Y==end.y) {
                    return p.d+1;
                }

                visited[X][Y] = true;
                queue.add(new Pair(X, Y, p.d+1));
            }
        }

        return -1;
    }

    static class Pair {
        int x;
        int y;
        int d;

        public Pair(int x, int y, int d) {
            this.x=x;
            this.y=y;
            this.d=d;
        }
    }
}

0개의 댓글