Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions 권혁준_12주차/[BOJ-7682] 틱택토.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 분기를 나눠서 판단하였지만, 대각 승리, 가로세로 승리와 같이 경우의 수를 확실하게 나눠서 구현하는 방법이 더 직관적이고 확실한 방법인 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 case work 문제들 풀 때에는 풀기 전에 명확하게 case 정리를 해놓고 구현하는 게 좋은 것 같더라구요 ,,
읽어주셔서 감사합니다 수고 많으셨어요 !

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
```java

import java.util.*;
import java.io.*;

class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static char[][] T;
static String temp;

public static void main(String[] args) throws Exception {

for(temp = br.readLine();!temp.equals("end");temp = br.readLine()) {

ready();
solve();
}

bwEnd();

}

static void ready() throws Exception{

T = new char[3][3];
for(int i=0;i<9;i++) T[i/3][i%3] = temp.charAt(i);

}

static void solve() throws Exception{

// X의 개수 - O의 개수 확인
int xCount = 0, oCount = 0;
for(int i=0;i<3;i++) for(int j=0;j<3;j++) {
xCount += T[i][j] == 'X' ? 1 : 0;
oCount += T[i][j] == 'O' ? 1 : 0;
}
if(xCount - oCount < 0 || xCount - oCount > 1) {
bw.write("invalid\n");
return;
}

// 대각선 승리
int xDiagWin = 0, oDiagWin = 0;
if(T[0][0] == 'X' && T[1][1] == 'X' && T[2][2] == 'X') xDiagWin++;
if(T[0][0] == 'O' && T[1][1] == 'O' && T[2][2] == 'O') oDiagWin++;
if(T[0][2] == 'X' && T[1][1] == 'X' && T[2][0] == 'X') xDiagWin++;
if(T[0][2] == 'O' && T[1][1] == 'O' && T[2][0] == 'O') oDiagWin++;
if(xDiagWin > 0) {
if(xCount == oCount) {
bw.write("invalid\n");
return;
}
bw.write("valid\n");
return;
}
else if(oDiagWin > 0) {
if(xCount > oCount) {
bw.write("invalid\n");
return;
}
bw.write("valid\n");
return;
}

// 성공 패턴의 개수
int xWin = 0, oWin = 0;
for(int i=0;i<3;i++) {
int res = 0;
for(int j=0;j<3;j++) res += T[i][j] == 'X' ? 1 : 0;
if(res == 3) xWin++;
res = 0;
for(int j=0;j<3;j++) res += T[i][j] == 'O' ? 1 : 0;
if(res == 3) oWin++;
}
for(int j=0;j<3;j++) {
int res = 0;
for(int i=0;i<3;i++) res += T[i][j] == 'X' ? 1 : 0;
if(res == 3) xWin++;
res = 0;
for(int i=0;i<3;i++) res += T[i][j] == 'O' ? 1 : 0;
if(res == 3) oWin++;
}
if(xWin + oWin == 0) {
if(xCount + oCount != 9) {
bw.write("invalid\n");
return;
}
bw.write("valid\n");
return;
}
if(xWin>0 && oWin>0) {
bw.write("invalid\n");
return;
}

if(xWin > 0) {
if(xCount == oCount) {
bw.write("invalid\n");
return;
}
bw.write("valid\n");
return;
}
else {
if(xCount > oCount) {
bw.write("invalid\n");
return;
}
bw.write("valid\n");
return;
}

}

}

```