728x90
https://www.acmicpc.net/problem/2563
문제
가로, 세로의 크기가 각각 100인 정사각형 모양의 흰색 도화지가 있다. 이 도화지 위에 가로, 세로의 크기가 각각 10인 정사각형 모양의 검은색 색종이를 색종이의 변과 도화지의 변이 평행하도록 붙인다. 이러한 방식으로 색종이를 한 장 또는 여러 장 붙인 후 색종이가 붙은 검은 영역의 넓이를 구하는 프로그램을 작성하시오.
예를 들어 흰색 도화지 위에 세 장의 검은색 색종이를 그림과 같은 모양으로 붙였다면 검은색 영역의 넓이는 260이 된다.
입력
첫째 줄에 색종이의 수가 주어진다. 이어 둘째 줄부터 한 줄에 하나씩 색종이를 붙인 위치가 주어진다. 색종이를 붙인 위치는 두 개의 자연수로 주어지는데 첫 번째 자연수는 색종이의 왼쪽 변과 도화지의 왼쪽 변 사이의 거리이고, 두 번째 자연수는 색종이의 아래쪽 변과 도화지의 아래쪽 변 사이의 거리이다. 색종이의 수는 100 이하이며, 색종이가 도화지 밖으로 나가는 경우는 없다
출력
첫째 줄에 색종이가 붙은 검은 영역의 넓이를 출력한다.
색종이가 있는 자리를 1, 없는 자리를 0으로 채운다.
색종이의 가로와 세로가 10이므로 x, y좌표를 입력받아 가로 세로 10만큼의 사각형을 1로 채운다. 이 과정에서 중복을 제외할 수 있다.
import java.util.Scanner;
public class BOJ_2563_색종이 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] arr = new int[100][100];
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
for (int dx = x; dx < x + 10; dx++) {
for (int dy = y; dy < y + 10; dy++) {
arr[dx][dy] = 1;
}
}
}
int sum = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
sum += arr[i][j];
}
}
System.out.println(sum);
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 1427 - 소트인사이드 (자바/Java) : 계수 정렬(Counting Sort) (0) | 2022.07.26 |
---|---|
[백준] 2751 - 수 정렬하기2 (자바/JAVA): 합병 정렬(Merge Sort) (0) | 2022.07.26 |
[백준] 2605 - 줄 세우기 (자바/Java) (0) | 2022.07.23 |
[백준] 2578 - 빙고 (자바/Java) (0) | 2022.07.23 |
[백준] 2309 - 일곱 난쟁이 (자바/Java) (0) | 2022.07.23 |