๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/21236)
< Comfortable Cows >
๋ฌธ์ ํ์ด
์ขํ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋๋ง๋ค ์ํ์ข์ฐ๋ฅผ ์ดํด๋ณด๋ฉฐ ์๊ฐ ์๋ค๋ฉด ๋ ๋ค ๊ฐ์ 1 ๋ํด์ค๋ค. ์ด๋, ํ์ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ธ์ ํ ๋ค ๋ฐฉํฅ ์ค 3๋ง๋ฆฌ์ ๋๋ฌ์ธ์ธ ์์ ์๋ฅผ ์ ๋ฐ์ดํธํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class _21236_ { // Comfortable Cows
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int n = Integer.parseInt(bf.readLine());
int arr[][] = new int[1001][1001];
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
arr[i][j] = -1;
}
}
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int result = 0;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(bf.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
arr[x][y] = 0;
for (int j = 0; j < 4; j++) {
int x1 = x + dx[j];
int y1 = y + dy[j];
if (x1 >= 0 && x1 < 1001 && y1 >= 0 && y1 < 1001) {
if (arr[x1][y1] > -1) {
arr[x][y] += 1;
if (arr[x1][y1] == 3) {
result -= 1;
}
arr[x1][y1] += 1;
if (arr[x1][y1] == 3) {
result += 1;
}
}
}
}
if (arr[x][y] == 3) {
result += 1;
}
bw.write(result + "\n");
}
bw.flush();
}
}
๋ณ์)
n : ์ขํ ์
arr : ๊ฐ ์์น์ ์๋ ์๋ง๋ค ์ธ์ ํ ๋ฐฉํฅ์ ์๋ ์์ ์
dx, dy : ์, ํ, ์ข, ์ฐ
x, y : ์ขํ
x1, y1 : ์, ํ, ์ข, ์ฐ ์์น
result : ์ธ์ ํ ๋ค ๋ฐฉํฅ ์ค 3๋ง๋ฆฌ์ ๋๋ฌ์ธ์ธ ์์ ์
์ขํ ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์ขํ์ ๋ฒ์๊ฐ 0 ์ด์ 1000 ์ดํ ์ด๋ฏ๋ก ๋ฐฐ์ด์ ์ ์ธํ์ฌ -1๋ก ์ด๊ธฐํํ๋ค. ์ขํ ์๋งํผ ์ขํ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ค์ ๊ณผ์ ์ ๊ฑฐ์น๋ค.
1) ํ์ฌ ์์น ๊ฐ์ 0์ผ๋ก ์ ์ฅ
2) ์ํ์ข์ฐ๋ฅผ ํ์ํ๋ฉฐ ๋ฒ์ ์์ด๊ณ ์๊ฐ ์๋ค๋ฉด ํ์ฌ ์์น +1, ํ์ ์์น์ ์๋ ์๋ ์ธ์ ํ ์๊ฐ ๋์ด๋๋ ๊ฒ์ด๋ฏ๋ก ๊ฐ์ +1 ํ๋ค. +1 ํ๊ธฐ ์ ๊ฐ์ด 3์ด๋ผ๋ฉด result-1, +1 ํ ํ์ ๊ฐ์ด 3์ด๋ผ๋ฉด result +1์ ํ๋ค.
3) ํ์ฌ ์์น ๊ฐ์ด 3์ด๋ผ๋ฉด result+1
4) result ์ถ๋ ฅ

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Baekjoon] 6119_Cow Line (0) | 2026.02.27 |
|---|---|
| [Baekjoon] 11969_Breed Counting (0) | 2026.02.24 |
| [Baekjoon] 3022_PRASE (0) | 2026.02.23 |
| [Baekjoon] 19622_ํ์์ค ๋ฐฐ์ 3 (0) | 2026.02.12 |
| [Baekjoon] 19621_ํ์์ค ๋ฐฐ์ 2 (0) | 2026.02.11 |