[백준] 11559 Puyo Puyo

장철현·2024년 2월 13일
0

백준

목록 보기
76/80

링크

11559 Puyo Puyo

문제

풀이

이 문제 푸는데 문제를 똑바로 안읽어서 헤맸다.
상하좌우로 4개 이상 붙은거 한번에 다 처리하고 밑으로 내려야 한다. 그거 말고는 그냥 시물레이션이다.

풀이로는
1. 2중 포문으로 .이 아니라면 탐색 시작한다.
2. 만약 4개 이상이라면 visited로 방문배열 해놓고 map을 .으로 바꿔준다.
3. .이 있는 빈 공간들을 .위에 있는 블럭들로 내려서 채운다.
이런식으로 계속 풀어나가면 된다.

코드

import java.awt.desktop.PreferencesEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.Buffer;
import java.sql.Array;
import java.util.*;

public class Main {
    public static int[] dx = {-1, 0, 1, 0};
    public static int[] dy = {0, 1, 0, -1};
    public static String[][] map = new String[12][6];
    public static int answer = 0;
    public static boolean[][] visited = new boolean[12][6];
    public static boolean isBomb = false;

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

        for(int i=0;i<12;i++) {
            String[] arr = br.readLine().split("");

            for(int j=0;j<6;j++) {
                map[i][j] = arr[j];
            }

//			System.out.println(Arrays.toString(map[i]));
        }




        while(true) {
            visited = new boolean[12][6];
            boolean isBomb = checkBomb();

            if(!isBomb) break;
            answer++;
        }

//		System.out.println("-----------------------------------");
//		for(int i=0;i<12;i++) System.out.println(Arrays.toString(map[i]));
        System.out.println(answer);


    }

    public static boolean checkBomb() {
        isBomb = false;
        for(int i=0;i<map.length;i++) {
            for(int j=0;j<map[i].length;j++) {

                if(!map[map.length - 1 - i][j].equals(".") && !visited[map.length - 1 - i][j]) {
                    bomb(map.length -1 - i, j, map[map.length - 1 - i][j]);
                }

            }
        }

        down();
//        System.out.println("---------------------- 내림 ------------------");
//        for(int i=0;i<map.length;i++) System.out.println(Arrays.toString(map[i]));

        return isBomb;
    }

    public static void bomb(int x, int y, String color) {
        visited = new boolean[12][6];

        Queue<Integer> queue = new LinkedList<>();
        queue.add(x);
        queue.add(y);
//        visited[x][y] = true;
        int count = 0;

        while(!queue.isEmpty()) {
            x = queue.poll();
            y = queue.poll();

            for(int i=0;i<4;i++) {
                int newX = x + dx[i];
                int newY = y + dy[i];

                if(newX < 0 || newY < 0 || newX >= map.length || newY >= map[0].length) continue;

                if(map[newX][newY].equals(color) && !visited[newX][newY]){
                    visited[newX][newY] = true;
                    queue.add(newX);
                    queue.add(newY);
                    count++;
                }
            }

        }

//		System.out.println("------------------------------");
//		for(int i=0;i<visited.length;i++) System.out.println(Arrays.toString(visited[i]));

        // count의 갯수가 4개 이상이면 터뜨리기
        if(count >= 4) {
            isBomb = true;
            for(int i=0;i<map.length;i++) {
                for(int j=0;j<map[i].length;j++) {
                    if(visited[i][j]) {
                        map[i][j] = ".";
                    }
                }
            }
        }

//		System.out.println("-----------------------------------");
//        System.out.println(answer);
//		for(int i=0;i<12;i++) System.out.println(Arrays.toString(map[i]));

    }

    //빈공간 내리기
    public static void down() {
        for(int i=0;i<map[0].length;i++) {
            int count = 0;

            for(int j=map.length-1;j>=0;j--) {
                if(map[j][i].equals(".")) {
                    count++;
                } else {
                    map[j+count][i] = map[j][i];
                }
            }

            for(int j=0;j<count;j++){
                map[j][i] = ".";
            }


        }
    }
}

0개의 댓글