[백준 2DimensionArray] 2563번 문제

Kwon·2023년 11월 29일

백준

목록 보기
16/22
post-thumbnail

백준 2563번 문제

풀이

  • 100 X 100 도화지에 설정 된 도형들의 총 넓이 구하기
  • 겹치는 부분은 교집합 형식으로 넓이를 구한다.
  • 2차 배열을 사용하는 문제
import java.util.Scanner;

public class dimension4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] array = new int[100][100];
        int result = 0;

        int x;
        int y;
        int count = 0;
        int num = sc.nextInt();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = 0;
            }
        }

        while (count < num) {
            x = sc.nextInt();
            y = sc.nextInt();
            int size_x = 0;
            for (int i = 0; i < array.length; i++) {
                int size_y = 0;
                boolean inputCheck = false;
                for (int j = 0; j < array[i].length; j++) {
                    if (i >= y && j >= x && size_x < 10 && size_y < 10) {
                        array[i][j]++;
                        size_y++;
                        inputCheck = true;
                    }
                }
                if (inputCheck) size_x++;
            }
            count++;
        }

        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                if (array[i][j] >= 1) result++;
            }
        }
        System.out.println(result);
    }
}

1. 위치 설정, 도형 배치

while (count < num) {
    x = sc.nextInt();
    y = sc.nextInt();
    int size_x = 0;
    for (int i = 0; i < array.length; i++) {
        int size_y = 0;
        boolean inputCheck = false;
        for (int j = 0; j < array[i].length; j++) {
            if (i >= y && j >= x && size_x < 10 && size_y < 10) {
                array[i][j]++;
                size_y++;
                inputCheck = true;
            }
        }
        if (inputCheck) size_x++;
    }
    count++;
}
  • num은 필드에 설정한 도형 배치 횟수
  • x, y 값을 통해 10 X 10 사각형을 배치한 후 1 싸이클 스택 적립(count)
  • 이중 for문을 이용해 10 X 10 사각형을 그림
  • x 값 10을 맞추기 위해 두 번째 for문 끝난 후 inputCheck로 조절

2. 결과 값 추출

 for (int i = 0; i < 100; i++) {
     for (int j = 0; j < 100; j++) {
         if (array[i][j] >= 1) result++;
     }
 }
 System.out.println(result);
  • 이중 for문을 이용해 출력
  • 100 X 100 맵에 1 이상 숫자 발견 시 넓이 값 +1

**총총

  • 문제를 깔끔히 못 풀었단 점에서 아쉬움이 많이 남음(그래서 이번 건 설명 적을 땐 힘들었음)
  • 굳이 X, Y값 추출하지 않고 1이상인 부분만 1+ 하면 충분히 쉽게 풀 수 있는 문제

profile
📲 @bu_kwon_2 / 💻 dnu05043.log / ⌨ Back-end / 🦁 LikeLion

0개의 댓글