반응형
📖 문제
7682번: 틱택토
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의 문자는 게임판에서 제일 윗 줄 왼쪽부터의 순서이다. 입
www.acmicpc.net
💡 풀이 방식
• 구현
- X의 갯수가 더 크다면, X가 이겨야한다.(X가 먼저 시작하기 때문)
- X의 갯수와 O의 갯수가 같다면, O가 이기는 것이다.(X가 먼저 시작하므로)
🔺 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import java.util.*;
import java.io.*;
public class Main {
static char[][] board;
static StringBuilder ans = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true) {
String str = br.readLine();
if(str.equals("end")) break;
int[] cntArr = new int[2]; // X, O
int idx = 0;
board = new char[3][3];
for(int i = 0 ; i < 3 ; i++) {
for(int j = 0 ; j < 3 ; j++) {
board[i][j] = str.charAt(idx++);
if(board[i][j] == 'X') cntArr[0]++;
else if(board[i][j] == 'O') cntArr[1]++;
}
}
if(cntArr[0] == cntArr[1] + 1) { // X가 이김
if(cntArr[0] + cntArr[1] == 9 && !bingo('O')) ans.append("valid");
else if(!bingo('O') && bingo('X')) ans.append("valid");
else ans.append("invalid");
}
else if(cntArr[0] == cntArr[1]) { // O가 이김
if(!bingo('X') && bingo('O')) ans.append("valid");
else ans.append("invalid");
}
else {
ans.append("invalid");
}
ans.append("\n");
}
System.out.println(ans.toString());
}
private static boolean bingo(char c) {
// 행 확인
for(int i = 0 ; i < 3 ; i++) {
if(board[i][0] == c && board[i][1] == c && board[i][2] == c)
return true;
}
// 열 확인
for(int i = 0 ; i < 3 ; i++) {
if(board[0][i] == c && board[1][i] == c && board[2][i] == c)
return true;
}
// \ 확인
if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
return true;
// / 확인
if(board[0][2] == c && board[1][1] == c && board[2][0] ==c)
return true;
return false;
}
}
|
cs |
➕ 다른 풀이 방식
- 조금 방식이 다르다.
[ BOJ ][JAVA][7682] 틱택토
https://www.acmicpc.net/problem/7682 7682번: 틱택토 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의 문자는 게임
coder-in-war.tistory.com
💦 어려웠던 점
- 문제 이해 자체를 요상하게 함... << 왜 때문에??!
- 조건을 따지는 게 까다로웠다..
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
(참고)
백준 7682 틱택토(Java)
문제 출처 : https://www.acmicpc.net/problem/7682 7682번: 틱택토 입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의
jyunslog.tistory.com
반응형
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 2933번: 미네랄 (0) | 2024.03.12 |
---|---|
[백준/JAVA] 2638번: 치즈 (0) | 2024.03.12 |
[백준/JAVA] 28215번: 대피소 (0) | 2024.03.07 |
[백준/JAVA] 9935번: 문자열 폭발 (1) | 2024.03.07 |
[백준/JAVA] 16935번: 배열 돌리기 3 (0) | 2024.03.02 |