본문 바로가기
알고리즘 문제풀이/백준

[백준 2563] 색종이

by 선서니 2023. 2. 15.

[문제 바로가기]👇

 

2563번: 색종이

가로, 세로의 크기가 각각 100인 정사각형 모양의 흰색 도화지가 있다. 이 도화지 위에 가로, 세로의 크기가 각각 10인 정사각형 모양의 검은색 색종이를 색종이의 변과 도화지의 변이 평행하도록

www.acmicpc.net

💡풀이💡

  1. 전체 흰 도화지 영역에서 색종이 영역을 true로 처리
  2. 전체 영역(100*100)에서 색종이 영역(true)을 빼기

 

1. 색종이 영역 표시하기

검은색 색종이 크기가 가로, 세로가 10이기 때문에

입력 받은 색종이 좌표로부터 가로, 세로 10안에 있는 영역을 true로 바꾸기

boolean[][] paper = new boolean[100][100];
		
for(int i=0; i<N; i++) {
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int x = Integer.parseInt(st.nextToken());
		int y = Integer.parseInt(st.nextToken());
			
        	// 검은색 색종이 영역 표시하기
		for(int r=y; r<y+10; r++) {
			for(int c=x; c<x+10; c++) {
				paper[r][c] = true;
			}
		}
}

 

2. 색종이 넓이 구하기

전체에서 색종이 영역(true 체크)된 곳이면 cnt 증가시켜서 구하기

int cnt = 0;
for(int i=0; i<100; i++) {
	for(int j=0; j<100; j++) {
		if(paper[i][j]) cnt++;
	}
}

 

 

전체 코드

import java.io.*;
import java.util.*;

public class B2563 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		boolean[][] paper = new boolean[100][100];
		
		for(int i=0; i<N; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			
			for(int r=y; r<y+10; r++) {
				for(int c=x; c<x+10; c++) {
					paper[r][c] = true;
				}
			}
		}
		
		int cnt = 0;
		for(int i=0; i<100; i++) {
			for(int j=0; j<100; j++) {
				if(paper[i][j]) cnt++;
			}
		}
		System.out.println(cnt);
	}
}

댓글