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();
}
}'백준' 카테고리의 다른 글
| [JAVA] 백준 1445 - 일요일 아침의 데이트 (0) | 2025.10.03 |
|---|---|
| [JAVA] 백준 12659 - Welcome to Code Jam (Small) (0) | 2025.10.02 |
| [JAVA] 백준 11909 - 배열 탈출 (0) | 2025.10.02 |
| [JAVA] 백준 10486 - Trapezoid Walkway (0) | 2025.10.02 |
| [JAVA] 백준 1343 - 폴리오미노 (0) | 2025.09.29 |