베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • Spring
    • 코드업
    • C++
    • 함수형인터페이스
    • leetcode
    • 스프링시큐리티
    • dfs
    • BFS
    • jpa
    • 스레드
    • 알고리즘
    • 데이터베이스
    • 스프링
    • 운영체제
    • 이펙티브자바
    • 토비스프링
    • 자바
    • 자바8
    • java
    • 백준

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    알고리즘/Baekjoon

    [백준] 7576 토마토 (JAVA)

    2022. 4. 4. 23:35

    ✉️문제

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

     

    7576번: 토마토

    첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토

    www.acmicpc.net

     

    📝 접근

    Queue를 이용한 BFS를 이용한다.

     

    🗝 문제풀이

    1. 입력을 받을 때 input == 1이면 큐에 집어넣는다.

      // 입력을 받을 때 input == 1이면 큐에 집어넣는다.
      for(int i = 0; i < n; i++) {
          for(int j = 0; j < m; j++) {
              int input = sc.nextInt();
              box[i][j] = input;
              if(input == 1) q.add(new Point(i, j));
          }
      }

       

      2. box안의 좌표가 현재 토마토(tmp)에 영향을 받는 위치이고, 값이 0이면 1로 바꾼뒤 큐에 넣는다.

      3. 체크 배열에 현재 토마토(tmp)가 익은 날짜 + 1을 수행한다. 

      public static void BFS() {
          while(!q.isEmpty()) {
              Point tmp = q.poll();
              for(int i = 0; i < dx.length; i++) {
                  int nx = tmp.x + dx[i];
                  int ny = tmp.y + dy[i];
                  if(nx >= 0 && nx < n && ny >= 0 && ny < m && box[nx][ny] == 0) {
                      box[nx][ny] = 1;
                      q.add(new Point(nx, ny));
                      ch[nx][ny] = ch[tmp.x][tmp.y] + 1; // 현재 토마토가 익은 날짜 + 1
                  }
              }
          }
      }

       

      최종 코드 

      package baekjoon;
      
      import java.util.LinkedList;
      import java.util.Queue;
      import java.util.Scanner;
      
      public class B7576 {
      
          static int[][] box,ch;
          static int m,n;
      
          static int[] dx = {-1, 0, 0, 1};
          static int[] dy = {0, -1, 1, 0};
      
          static Queue<Point> q = new LinkedList<>();
      
          static class Point {
              int x, y;
      
              public Point(int x, int y) {
                  this.x = x;
                  this.y = y;
              }
          }
      
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
      
              m = sc.nextInt(); // 가로 칸의 수
              n = sc.nextInt(); // 세로 칸의 수
      
              box = new int[n][m];
              ch = new int[n][m];
      
              for(int i = 0; i < n; i++) {
                  for(int j = 0; j < m; j++) {
                      int input = sc.nextInt();
                      box[i][j] = input;
                      if(input == 1) q.add(new Point(i, j));
                  }
              }
      
              BFS();
      
              int answer = -1;
              if(checkTomato()) {
                  for(int i = 0; i < n; i++) {
                      for(int j = 0; j < m; j++) {
                          answer = Math.max(answer, ch[i][j]);
                      }
                  }
                  System.out.println(answer);
              }
              else System.out.println(answer);
          }
      
          private static boolean checkTomato() {
              for(int i = 0; i < n; i++) {
                  for(int j = 0; j < m; j++) {
                      if(box[i][j] == 0) return false;
                  }
              }
              return true;
          }
      
          public static void BFS() {
              while(!q.isEmpty()) {
                  Point tmp = q.poll();
                  for(int i = 0; i < dx.length; i++) {
                      int nx = tmp.x + dx[i];
                      int ny = tmp.y + dy[i];
                      if(nx >= 0 && nx < n && ny >= 0 && ny < m && box[nx][ny] == 0) {
                          box[nx][ny] = 1;
                          q.add(new Point(nx, ny));
                          ch[nx][ny] = ch[tmp.x][tmp.y] + 1;
                      }
                  }
              }
          }
      }
        '알고리즘/Baekjoon' 카테고리의 다른 글
        • [백준] 1697 숨바꼭질 (JAVA)
        • [백준] 7569 토마토 (JAVA)
        • [백준] 2606 바이러스(JAVA)
        • [백준] 10830 행렬 제곱 (JAVA)
        베어_
        베어_
        Today I learned | 문제를 해결하는 개발자

        티스토리툴바