배움과 기록의 장
[백준] 2563번: 색종이 (Java) 본문
✅ 문제
https://www.acmicpc.net/problem/2563
✅ 풀이
- 전체 100x100 보드를 나타내는 2차원 배열을 만들어준다. 각 요소의 초기값은 0.
- 입력값으로 left와 bottom 값을 받고, left ~ left+10, bottom ~ bottom+10에 해당하는 영역에 1을 할당해준다. (이 과정을 색종이 수 만큼 반복)
- 100x100 보드를 나타내는 2차원 배열 전체를 순회하며, 요소의 값이 1인 것을 찾아 count 해주면 색종이가 붙은 검은 영역의 넓이를 구할 수 있다.
✅ 코드
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[][] board = new int[100][100];
int num = sc.nextInt();
for(int i = 0; i < num; i++){
int left = sc.nextInt();
int bottom = sc.nextInt();
for(int j= left; j < left+10; j++){
for(int k = bottom; k < bottom+10; k++){
board[j][k] = 1;
}
}
}
int count = 0;
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
if(board[i][j] == 1) count++;
}
}
System.out.println(count);
}
}
반응형
'problem solving' 카테고리의 다른 글
[백준] 1085번: 직사각형에서 탈출 (Java) (0) | 2023.03.25 |
---|---|
[백준] 11653번: 소인수분해 (Java) (0) | 2023.03.22 |
[백준] 1316번: 그룹 단어 체커 (Java) (0) | 2023.03.09 |
[백준] 2941번: 크로아티아 알파벳 (Java) (0) | 2023.03.09 |
[백준] 1157번: 단어 공부 (Java) (0) | 2023.03.09 |