곽로그

[백준 16197, Java] 두 동전 -ing 본문

카테고리 없음

[백준 16197, Java] 두 동전 -ing

일도이동 2021. 4. 16. 09:36
반응형
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

class Coin{
    int x,y;
    Coin(int x, int y){
        this.x = x;
        this.y =y;
    }

    @Override
    public String toString() {
        return "Coin{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
public class Main {
    static String[][] map;
    static int N;
    static int M;
    static int min;
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,1,0,-1};
    static final String COIN = "o";
    static final String BLANK = ".";
    static final String WALL = "#";

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] line = br.readLine().split(" ");
        N = Integer.parseInt(line[0]);
        M = Integer.parseInt(line[1]);
        map = new String[N][M];
        min = 10;
        ArrayList<Coin> coins;coins = new ArrayList<>();

        //코인 입력
        for(int r=0; r<N; r++){
            line = br.readLine().split("");
            for(int c=0; c<M; c++){
                String value = line[c];
                map[r][c] = value;
                if("o".equals(value)){
                    coins.add(new Coin(r,c));
                }
            }
        }

        int result = getMinCount(0, coins);
        if(result == Integer.MAX_VALUE){
            bw.write("-1");
        }
        else {
            bw.write(String.valueOf(result));
        }
        bw.flush();
    }

    public static int getMinCount(int pushCount, ArrayList<Coin> coinList){
        if(pushCount>10){
            return Integer.MAX_VALUE;
        }
        else if(coinList.size()==0){
            return Integer.MAX_VALUE;
        }
        else if(coinList.size()==1){
            return pushCount;
        }
        else{
            int min = Integer.MAX_VALUE;
            for(int d = 0; d<4; d++){
                // 동전던지기 전에 리스트 복사
                ArrayList<Coin> temp = new ArrayList<>();
                for(int index = 0; index<coinList.size(); index++){
                    temp.add(new Coin(coinList.get(index).x, coinList.get(index).y));
                }

                // 동전던지기
                for(int index = temp.size()-1; index>=0 ; --index){
                    Coin coin = temp.get(index);
                    int nx = coin.x + dx[d];
                    int ny = coin.y + dy[d];

                    if(nx<0 || ny<0 || nx>=N || ny>=M){
                        temp.remove(index);
                    }
                    else if(!map[nx][ny].equals("#")){
                        coin.x = nx;
                        coin.y = ny;
                    }
                }

                int result = getMinCount(pushCount+1, temp);
                min = Math.min(result, min);
            }
            return min;
        }

    }
}
반응형
Comments