[백준] java 1926 그림

험프티덤프티·2022년 9월 15일
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
  public static void main(String[] args) throws IOException {
    System.setIn(new FileInputStream("src/input.txt"));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

    for (int i = 0; i < width; i++) {
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j < height; j++) {
        arr[i][j] = Integer.parseInt(st.nextToken());
      }
    }


    Queue<Pair> queue = new LinkedList<>();
    boolean[][] visited = new boolean[width][height];
//    visited[0][0] = true;
//    queue.add(new Pair(0, 0));

    int[] temp1 = {1, 0, -1, 0};
    int[] temp2 = {0, 1, 0, -1};

    int count = 0;
    int maxSize = 0;

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        if (visited[i][j] || arr[i][j] == 0) continue;
        visited[i][j] = true;
        queue.add(new Pair(i, j));
        count++;
        int tempSize = 0;

        while (!queue.isEmpty()) {
          tempSize++;
          Pair cur = queue.remove();
          for (int k = 0; k < 4; k++) {
            int x = cur.x + temp1[k];
            int y = cur.y + temp2[k];

            if (x < 0 || y < 0 || x > width - 1 || y > height - 1) continue;
            if (visited[x][y] || arr[x][y] == 0) continue;

            queue.add(new Pair(x, y));
            visited[x][y] = true;
          }
        }
        maxSize = Math.max(maxSize, tempSize);
      }
    }

      System.out.println(count);
      System.out.println(maxSize);
  }
}

class Pair {
  int x;
  int y;

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

처음엔 이중 for문 들어가기 전에 아무생각없이 (0,0)을 큐에 넣고, visited에 (0,0)을 추가하고나서 이중 for문제 들어가는 구조로 짰는데 (위 코드의 주석부분), 이렇게 짜면
인풋이
1 1
1
이렇게 들어오거나
2 2
1 0
0 1
이렇게 들어올때 오류가 난다.

bfs 많이 풀어보자..

profile
기록기록기록

0개의 댓글