본문 바로가기
백준

[JAVA] 백준 2567 - 색종이 - 2

by 맴썰 2025. 10. 2.

https://www.acmicpc.net/problem/2567

100 * 100 크기의 배열을 만들고, 색종이 위치를 1로 입력한 후 배열 전체를 순회하며 1을 찾으면 4방향의 0의 개수 + 배열 범위 밖으로 나간 횟수를 더해 반환했다.

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,1,-1};
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tc = par(br.readLine());
        int[][] map = new int[100][100];
        for (int i = 0; i < tc; i++) {
            int[] temp = getArray(br.readLine());
            for (int j = temp[0]; j < temp[0]+10; j++) {
                for (int k = temp[1]; k < temp[1]+10; k++) {
                    map[j][k] = 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                if(map[i][j]==1){
                    for (int k = 0; k < 4; k++) {
                        if(check(i+dx[k],j+dy[k])){
                            if(map[i+dx[k]][j+dy[k]]==0) count++;
                        }else count++;
                    }
                }
            }
        }
        System.out.println(count);
    }
    static boolean check(int i, int j){
        if(i<0||i>=100) return false;
        if(j<0||j>=100) return false;
        return true;
    }
    static int par(String s) {
        return Integer.parseInt(s);
    }

    static int[] getArray(String s) {
        return Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
    }
}