(백준) 마법사 상어와 파이어스톰

2021. 10. 19. 19:50·알고리즘/백준

https://www.acmicpc.net/problem/20058

 

🐢 설명

하나의 보드에서 부분사각형으로 회전을 하고 모든 회전이 끝나고 나면 각 좌표들에 대해서 인접 노드의 얼음 개수가 3미만인 경우를 찾고, 해당 좌표의 얼음을 1 감소시킨다. 이 후 모든 얼음의 개수를 세고, 가장 큰 얼음덩어리의 면적을 구한다.

이때 각 작은 사각형마다 회전을 하기위해 작은사각형의 크기만큼 x좌표와 y좌표를 이동시켜서 회전을 했다.

 

🐢코드
//얼음이 있는 칸 3개 또는 그 이상과 인접해있지 않은 칸은 얼음의 양이 1 줄어든다

public class 마법사상어와파이어스톰_20058 {
    static int N, Q, L;
    static int[] dy = {-1, 1, 0, 0};
    static int[] dx = {0, 0, -1, 1};
    static boolean[][] visited;
    static int[][] board;
    static int[][] tmpBoard;
    static ArrayList<Integer> qList = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        intput();
        solve();
    }

    private static void solve() {
        for (Integer size : qList) {
            for (int i = 0; i < L; i += size) {
                for (int j = 0; j < L; j += size) {
                    tmpBoard = new int[size][size];
                    turn90(i, j, size);
                }
            }
            melting();
        }
        countAll();
        getLargestLand();
    }

    private static void getLargestLand() {
        int MAX = 0;
        visited = new boolean[L][L];

        for (int i = 0; i < L; i++) {
            for (int j = 0; j < L; j++) {
                if (visited[i][j] || board[i][j] == 0) continue;
                MAX = Math.max(MAX, BFS(i, j));
            }
        }
        System.out.println(MAX);
    }

    private static int BFS(int i, int j) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{i, j});
        visited[i][j] = true;
        int count = 0;

        while (!queue.isEmpty()) {
            int[] now = queue.poll();
            int y = now[0];
            int x = now[1];

            if (board[y][x] >= 1) count++;

            for (int d = 0; d < 4; d++) {
                int ny = dy[d] + y;
                int nx = dx[d] + x;

                if (ny < 0 || nx < 0 || ny >= L || nx >= L) continue;
                if (board[ny][nx] == 0) continue;
                if (visited[ny][nx]) continue;
                visited[ny][nx] = true;
                queue.add(new int[]{ny, nx});
            }
        }

        return count;
    }

    private static void countAll() {
        int sum = 0;
        for (int i = 0; i < L; i++) {
            for (int j = 0; j < L; j++) {
                sum += board[i][j];
            }
        }
        System.out.println(sum);
    }

    private static void melting() {
        ArrayList<int[]> meltingDot = new ArrayList<>();

        for (int i = 0; i < L; i++) {
            for (int j = 0; j < L; j++) {
                int count = 0;
                if (board[i][j] > 0) {
                    int ny, nx;
                    for (int d = 0; d < 4; d++) {
                        ny = i + dy[d];
                        nx = j + dx[d];
                        if (ny < 0 || ny >= L || nx < 0 || nx >= L) continue;
                        if (board[ny][nx] == 0) continue;
                        count++;
                    }
                    if (count < 3) meltingDot.add(new int[]{i, j});
                }
            }
        }
        for (int[] dot : meltingDot) {
            board[dot[0]][dot[1]]--;
        }
    }

    private static void turn90(int y, int x, Integer size) {
        int idx = 1;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                tmpBoard[j][size - idx] = board[y + i][x + j];
            }
            idx++;
        }

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i + y][j + x] = tmpBoard[i][j];
            }
        }
    }

    private static void intput() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        Q = Integer.parseInt(st.nextToken());
        L = (int) Math.pow(2, N);
        board = new int[L][L];

        for (int i = 0; i < L; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < L; j++) {
                board[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < Q; i++) {
            int q = Integer.parseInt(st.nextToken());
            qList.add((int) Math.pow(2, q));
        }
    }
}
🐢 마무리
반응형
저작자표시 비영리 변경금지 (새창열림)

'알고리즘 > 백준' 카테고리의 다른 글

(백준) 불 _ 5427  (0) 2021.12.08
(백준) 뱀 _ 3190  (0) 2021.10.22
(백준) 마법사 상어와 토네이도  (0) 2021.10.19
(백준) 독서실 거리두기 _ 20665  (0) 2021.10.10
(백준) 사과와 바나나 _ 3114  (0) 2021.09.29
'알고리즘/백준' 카테고리의 다른 글
  • (백준) 불 _ 5427
  • (백준) 뱀 _ 3190
  • (백준) 마법사 상어와 토네이도
  • (백준) 독서실 거리두기 _ 20665
구름뭉치
구름뭉치
구름의 개발일기장
    반응형
  • 구름뭉치
    구름 개발일기장
    구름뭉치
  • 전체
    오늘
    어제
    • ALL (288) N
      • 프로젝트 (23)
        • 토스페이먼츠 PG 연동 시리즈 (12)
        • JWT 방식 인증&인가 시리즈 (6)
        • 스우미 웹 애플리케이션 프로젝트 (1)
        • 스프링부트 기본 보일러 플레이트 구축 시리즈 (2)
        • 마이크로서비스 아키텍쳐 시리즈 (1)
      • 스프링 (43)
        • 스프링부트 API 설계 정리 (8)
        • 스프링부트 RestAPI 프로젝트 (18)
        • 스프링부트 WebSocket 적용기 (3)
        • 스프링 JPA 정리 시리즈 (5)
        • 스프링 MVC (5)
        • 스프링 배치 (2)
        • 토비의 스프링 정리 (2)
      • 기술 학습 (4) N
        • 아파치 카프카 (9)
        • 클린 코드 (4)
        • 디자인 패턴의 아름다움 (2)
        • 모던 자바 인 액션 (7)
        • JVM 스레드 딥다이브 (7)
      • Web (25)
        • 정리글 (20)
        • GraphQL 정리글 (2)
        • Jenkins 정리글 (3)
      • 취업 (6)
      • CS (77)
        • 네트워크 전공 수업 정리 (11)
        • OSI 7계층 정리 (12)
        • 운영체제 정리 (19)
        • 데이터베이스 정리 (5)
        • MySql 정리 (17)
        • GoF의 Design Pattern 정리 (12)
      • 알고리즘 (70)
        • 백준 (56)
        • 프로그래머스 (12)
        • 알고리즘 정리본 (1)
      • 기초 지식 정리 (2)
      • 일상 (8)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    부다페스트
    마우스 패드
    동유럽
    마우스
    크로아티아
    레이저
    류블라냐
    mx master s3 for mac
    키보드 손목 받침대
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
구름뭉치
(백준) 마법사 상어와 파이어스톰
상단으로

티스토리툴바