[Algorithm/Java] 백준 2567번 - 색종이-2
https://www.acmicpc.net/problem/2567
🔍 문제 풀이
풀이 방법
- 100x100 도화지에 색종이 붙인 부분을 1로 표시
- 색종이 부분(
paper[i][j] == 1
)을 기준으로 상하좌우 중 인접한 칸이 0이면 둘레
문제 도식화
💻 전체 코드
import java.io.*;
import java.util.*;
public class Main {
static int[][] paper;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int cnt=0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
paper = new int[100][100];
int n = Integer.parseInt(br.readLine());
while(n-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int sj = Integer.parseInt(st.nextToken());
int si = Integer.parseInt(st.nextToken());
for(int i=si; i<si+10; i++){
for(int j=sj; j<sj+10; j++){
paper[i][j] = 1;
}
}
}
for(int i=0; i<100; i++){
for(int j=0; j<100; j++){
if(paper[i][j] == 1){
cntZero(i, j);
}
}
}
System.out.println(cnt);
}
static void cntZero(int x, int y){
for(int d=0; d<4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
// 범위 밖이거나 0인 경우 둘레
if (nx < 0 || nx >= 100 || ny < 0 || ny >= 100 || paper[nx][ny] == 0) {
cnt++;
}
}
}
}
댓글남기기