쵼쥬
쵼쥬의 개발공부 TIL
쵼쥬
전체 방문자
오늘
어제
  • 분류 전체보기 (276)
    • 코딩테스트 (192)
      • [알고리즘] 알고리즘 정리 (7)
      • [백준] 코딩테스트 연습 (126)
      • [프로그래머스] 코딩테스트 연습 (59)
    • Spring (71)
      • [인프런] 스프링 핵심 원리- 기본편 (9)
      • [인프런] 스프링 MVC 1 (6)
      • [인프런] 스프링 MVC 2 (4)
      • [인프런] 실전! 스프링 부트와 JPA 활용1 (7)
      • [인프런] 실전! 스프링 부트와 JPA 활용2 (5)
      • [인프런] 실전! 스프링 데이터 JPA (7)
      • [인프런] 실전! Querydsl (7)
      • JWT (5)
      • [인프런] Spring Cloud (17)
      • [인프런] Spring Batch (4)
    • Java (6)
      • [Java8] 모던인자바액션 (4)
      • [부스트코스] 웹 백엔드 (2)
      • [패스트캠퍼스] JAVA STREAM (0)
    • CS (6)
      • 디자인 패턴과 프로그래밍 패터다임 (2)
      • 네트워크 (4)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 알고리즘
  • spring
  • jpa
  • 코딩테스트
  • 타임리프
  • 스프링
  • 구현
  • MVC
  • 부스트코스
  • querydsl
  • 누적합
  • 비트마스킹
  • BFS
  • Spring Data JPA
  • 자바
  • 백준
  • 백분
  • 프로그래머스
  • 인프런
  • 위클리 챌린지

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

불 - 5427번
코딩테스트/[백준] 코딩테스트 연습

불 - 5427번

2022. 1. 24. 21:13


내 코드

package com.company;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static char[][] arr;
    static int w, h, time;
    static Queue<int[]> fireQ = new LinkedList<>();
    static PriorityQueue<Node> pq = new PriorityQueue<>();
    static int[][] way = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    static boolean[][] visited;

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

        int T = Integer.parseInt(br.readLine());

        for (int tc = 0; tc < T; tc++) {
            boolean check = false;

            fireQ.clear();
            pq.clear();

            st = new StringTokenizer(br.readLine());
            w = Integer.parseInt(st.nextToken());
            h = Integer.parseInt(st.nextToken());
            time = 0;

            arr = new char[h + 2][w + 2];
            visited = new boolean[h + 2][w + 2];

            for (int i = 1; i <= h; i++) {
                String s = br.readLine();
                for (int j = 1; j <= w; j++) {
                    arr[i][j] = s.charAt(j - 1);
                    if (arr[i][j] == '*') {
                        fireQ.add(new int[]{i, j});
                    }
                    if (arr[i][j] == '@')
                        pq.add(new Node(i, j, 0));
                }
            }

            while (!pq.isEmpty()) {
                Node node = pq.poll();

                if (visited[node.x][node.y] || arr[node.x][node.y] == '*')
                    continue;

                visited[node.x][node.y] = true;

                if (node.x == 0 || node.y == 0 || node.x == h + 1 || node.y == w + 1) {
                    check = true;
                    time = node.dis;
                    break;
                }

                if (time == node.dis) {
                    fire(fireQ.size());
                    time++;
                }

                for (int i = 0; i < 4; i++) {
                    int x = node.x + way[i][0];
                    int y = node.y + way[i][1];

                    if (!visited[x][y] && arr[x][y] != '#' && arr[x][y] != '*') {
                        pq.add(new Node(x, y, node.dis + 1));
                        arr[x][y] = '@';
                    }
                }

            }

            if (check)
                System.out.println(time);
            else
                System.out.println("IMPOSSIBLE");

        }
    }

    static void fire(int size) {
        for (int i = 0; i < size; i++) {
            int[] index = fireQ.poll();

            for (int j = 0; j < 4; j++) {
                int x = index[0] + way[j][0];
                int y = index[1] + way[j][1];

                if (x >= 1 && x < h + 1 && y >= 1 && y < w + 1 && arr[x][y] != '#' && arr[x][y] != '*') {
                    arr[x][y] = '*';
                    fireQ.add(new int[]{x, y});
                }
            }
        }
    }
}

class Node implements Comparable<Node> {
    int x, y, dis;

    public Node(int x, int y, int dis) {
        this.x = x;
        this.y = y;
        this.dis = dis;
    }

    @Override
    public int compareTo(Node o) {
        return dis - o.dis;
    }
}

 

'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글

소수상수근 - 9421번  (0) 2022.02.07
스카이라인 쉬운거 - 1863번  (0) 2022.02.04
월드컵 - 6987번  (0) 2022.01.24
간선 이어가기2 - 14284번  (0) 2022.01.21
백양로 브레이크 - 11562번  (0) 2022.01.19
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 소수상수근 - 9421번
    • 스카이라인 쉬운거 - 1863번
    • 월드컵 - 6987번
    • 간선 이어가기2 - 14284번
    쵼쥬
    쵼쥬

    티스토리툴바