[JAVA-자바] 2638번: 치즈

2025. 10. 6. 13:53·알고리즘/백준

 

소스코드:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

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

    StringTokenizer st = new StringTokenizer(br.readLine());
    int height = Integer.parseInt(st.nextToken());
    int width = Integer.parseInt(st.nextToken());

    int[][] board = new int[height][width];
    int total = 0;
    for (int y = 0; y < height; y++) {
      st = new StringTokenizer(br.readLine());
      for (int x = 0; x < width; x++) {
        board[y][x] = Integer.parseInt(st.nextToken());
        if (board[y][x] == 1) {
          total++;
        }
      }
    }

    int time = 0;
    int[] dx = {0, 0, -1, 1};
    int[] dy = {-1, 1, 0, 0};

    while (total > 0) {
      boolean[][] visited = new boolean[height][width];
      int[][] cheese = new int[height][width];

      Queue<int[]> queue = new LinkedList<>();
      queue.add(new int[]{0, 0});

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

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

          if (nextX < 0 || nextX >= width || nextY < 0 || nextY >= height) {
            continue;
          }

          if (visited[nextY][nextX]) {
            continue;
          }

          if (board[nextY][nextX] == 0) {
            // air
            visited[nextY][nextX] = true;
            queue.add(new int[]{nextX, nextY});
          } else if (board[nextY][nextX] == 1) {
            // cheese
            cheese[nextY][nextX]++;
          }
        }
      }

      // del
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          if (cheese[y][x] >= 2) {
            board[y][x] = 0;
            total--;
          }
        }
      }
      time++;
    }
    bw.write(String.valueOf(time));
    bw.close();
    br.close();
  }
}

 

글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.

여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.

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

[JAVA-자바] 3015번: 오아시스 재결합  (0) 2025.10.08
[JAVA-자바] 2206번: 벽 부수고 이동하기  (0) 2025.05.22
[JAVA-자바] 1865번: 웜홀  (0) 2025.05.21
[JAVA-자바] 1238번: 파티  (0) 2025.05.19
[JAVA-자바] 17144번: 미세먼지 안녕!  (0) 2025.05.18
'알고리즘/백준' 카테고리의 다른 글
  • [JAVA-자바] 3015번: 오아시스 재결합
  • [JAVA-자바] 2206번: 벽 부수고 이동하기
  • [JAVA-자바] 1865번: 웜홀
  • [JAVA-자바] 1238번: 파티
raven
raven
Github : https://github.com/RabeMaster | Email : ra___be@naver.com
  • raven
    배움을 원하는 사람
    raven
  • 전체
    오늘
    어제
  • 공지사항

    • 안녕하세요
    • 분류 전체보기 (169)
      • 네이버 부스트캠프 10기 (7)
        • 멤버십 (5)
        • 챌린지 (1)
        • 베이직 (1)
      • 공부 (2)
        • JAVA (1)
        • CS (0)
        • 정보처리기사 (1)
      • 알고리즘 (159)
        • 백준 (159)
      • 개발 (1)
        • 백준 확장 프로그램 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 인기 글

  • 태그

    부트캠프
    네부캠
    개발
    구현
    코테
    네이버부스트캠프
    너비 우선 탐색
    백트래킹
    네이버
    자바
    다이나믹 프로그래밍
    그리디 알고리즘
    최단 경로
    알고리즘
    회고
    IT
    그래프 탐색
    수학
    문자열
    브루트포스 알고리즘
    정렬
    부스트캠프
    백준
    코딩테스트
    그래프 이론
    스택
    멤버십
    자료 구조
    코딩
    java
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
raven
[JAVA-자바] 2638번: 치즈
상단으로

티스토리툴바