๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/9400)
< Calculate the Fence Needed >
๋ฌธ์ ํ์ด
ํ ๊ฒฉ์ ์นธ์ ํ์ํ ์ธํ๋ฆฌ์ ๊ธธ์ด = 4 - (๋์ฅ์ ํ ๊ฒฉ์ ์นธ๊ณผ ์ฐ๊ฒฐ๋ ๋์ฅ์ ๊ฒฉ์ ์นธ์ ์)
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.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class _9400_ { // Calculate the Fence Needed
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 = 0;
Queue<int[]> queue = new LinkedList<>();
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
while ((N = Integer.parseInt(bf.readLine())) != 0) {
boolean arr[][] = new boolean[101][101];
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[101 - x][y] = true;
queue.add(new int[] { 101 - x, y });
}
int result = 0;
while (!queue.isEmpty()) {
int temp[] = queue.poll();
int cnt = 0;
for (int i = 0; i < 4; i++) {
int x = temp[0] + dx[i];
int y = temp[1] + dy[i];
if (x >= 0 && x < 101 && y >= 0 && y < 101 && arr[x][y]) {
cnt += 1;
}
}
result += (4 - cnt);
}
bw.write(result + "\n");
}
bw.flush();
}
}
๋ณ์)
N : ๋์ฅ ๋ฉด์
queue : Queue <int []>
dx, dy : ์, ํ, ์ข, ์ฐ
arr : ๋์ฅ
result : ํ์ํ ์ธํ๋ฆฌ ๊ธธ์ด
cnt : ๋์ฅ์ ํ ๊ฒฉ์ ์นธ๊ณผ ์ฐ๊ฒฐ๋ ๋์ฅ์ ๊ฒฉ์ ์นธ์ ์
๋์ฅ ๋ฉด์ ์ ์ ๋ ฅ๋ฐ๋๋ค. ๋์ฅ ๋ฉด์ ๋งํผ ์ขํ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด arr์ ํ์ํ๊ณ queue์ ์ ์ฅํ๋ค. queue๊ฐ ๋น ๋๊น์ง ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1) queue poll
2) ์, ํ, ์ข, ์ฐ๋ฅผ ํ์ํ๋ฉฐ ๋์ฅ์ธ์ง ํ์ธ
3) ํ์ํ ์ธํ๋ฆฌ ๊ธธ์ด์ (4- ๋์ฅ์ ํ ๊ฒฉ์ ์นธ๊ณผ ์ฐ๊ฒฐ๋ ๋์ฅ์ ๊ฒฉ์ ์นธ์ ์) ๋ํ๊ธฐ
์ต์ข result๋ฅผ ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 15426_GlitchBot (3) | 2025.07.23 |
---|---|
[Baekjoon] 30949_Equal Schedules (2) | 2025.07.22 |
[Baekjoon] 4466_A Smart Brain is a Tasty Brain (3) | 2025.07.18 |
[Baekjoon] 17585_Circuit Math (2) | 2025.07.17 |
[Baekjoon] 8594_Program (0) | 2025.07.16 |