Skip to content

Commit 0ce2569

Browse files
authored
Merge pull request #45 from AlgorithmWithGod/03do-new30
[20250205] BOJ / 실버1 / 오목 / 신동윤
2 parents c0072ef + f6a5c90 commit 0ce2569

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
10+
int N = 20;
11+
int[][] arr = new int[N][N];
12+
13+
// 검사 방향: 아래, 우상향, 좌하향, 우측
14+
int[] dr = {1, -1, 1, 0};
15+
int[] dc = {0, 1, 1, 1};
16+
17+
for (int i = 1; i < N; i++) {
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
for (int j = 1; j < N; j++) {
20+
arr[i][j] = Integer.parseInt(st.nextToken());
21+
}
22+
}
23+
24+
int winner = 0;
25+
int winnerR = 0;
26+
int winnerC = 0;
27+
28+
for (int r = 1; r < N; r++) {
29+
for (int c = 1; c < N; c++) {
30+
31+
if (winner > 0) break; // 승부 결정 완료
32+
if (arr[r][c] == 0) continue;
33+
34+
// 방향 체크
35+
for (int i = 0; i < 4; i++) {
36+
37+
int cnt = 1; // (r, c)에서 시작, 연속된 바둑돌 개수 카운트
38+
int nr = r + dr[i];
39+
int nc = c + dc[i];
40+
41+
while (1 <= nr && nr < N && 1 <= nc && nc < N) {
42+
if (arr[nr][nc] != arr[r][c]) {
43+
break;
44+
}
45+
nr += dr[i];
46+
nc += dc[i];
47+
cnt++;
48+
}
49+
50+
// i방향으로 연속된 바둑돌이 5개인 경우
51+
if (cnt == 5) {
52+
// (r, c) 이전의 바둑돌 색상을 확인하여 6개 이상인지 체크
53+
int prevR = r - dr[i];
54+
int prevC = c - dc[i];
55+
56+
boolean isValid = false;
57+
if (1 <= prevR && prevR < N && 1 <= prevC && prevC < N) {
58+
if (arr[r][c] != arr[prevR][prevC]) {
59+
isValid = true;
60+
}
61+
}
62+
else { // 이전의 바둑돌이 없다면 5개임이 보장
63+
isValid = true;
64+
}
65+
66+
if (isValid) {
67+
winner = arr[r][c];
68+
winnerR = r;
69+
winnerC = c;
70+
break; // 승부 결정 완료
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
bw.write(winner + "\n");
78+
if (winner > 0) {
79+
bw.write(winnerR + " " + winnerC);
80+
}
81+
82+
83+
br.close();
84+
bw.flush();
85+
bw.close();
86+
}
87+
}
88+
89+
```
90+
91+
- (r, c)에서 ⬇↗↘➡ 방향으로 진행해서 같은 색 바둑돌 5개를 찾은 경우
92+
93+
- 육목일 가능성이 없는지 진행 방향의 반대 방향으로 가서 확인
94+
95+
- `틀렸습니다` 받은 이유
96+
- 육목 가능성 체크하기 위해 반대 방향의 좌표 (prevR, prevC)룰 구할 때
97+
- (prevR, prevC)가 범위를 벗어난다면 해당 케이스는 오목임이 보장됨
98+
- 범위 벗어나는 경우 생각 못하고 `범위 내에 있을때, (r, c)와 같은 색 바둑돌인지`만 체크했던 실수💥
99+
100+
- 가능한 케이스들을 꼼꼼히 생각해 보는 것이 중요

0 commit comments

Comments
 (0)