/
7682.java
113 lines (103 loc) · 3.09 KB
/
7682.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main {
static Vector<Character> v = new Vector();
static int xc,oc;
static char g_t;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while(true)
{
String s = br.readLine();
if(s.equals("end"))
break;
System.out.println(checkValid(s) ? "valid" : "invalid");
}
}
static boolean checkBingo1(int x,int y,int cnt ,char[][] arr,char c) {
boolean ret = false;
if (cnt == 2) {
return true;
}
if(y+1 <=2 && arr[y+1][x] == c)
{
ret = checkBingo1(x,y+1,cnt+1, arr,c);
}
return ret;
}
static boolean checkBingo2(int x,int y,int cnt ,char[][] arr,char c) {
boolean ret = false;
if (cnt == 2) {
return true;
}
if (x + 1 <= 2 && arr[y][x+1] == c) {
ret = checkBingo2(x + 1, y, cnt + 1, arr, c);
}
return ret;
}
static boolean checkBingo3(int x,int y,int cnt ,char[][] arr,char c) {
boolean ret = false;
if (cnt == 2) {
return true;
}
if(x+1 <=2 && y+1<=2 && arr[y+1][x+1] == c)
{
ret = checkBingo3(x+1,y+1,cnt+1, arr,c);
}
return ret;
}
static boolean checkBingo4(int x,int y,int cnt ,char[][] arr,char c) {
boolean ret = false;
if (cnt == 2) {
return true;
}
if(x+1 <=2 && y-1>=0 && arr[y-1][x+1] == c)
{
ret = checkBingo4(x+1,y-1,cnt+1, arr,c);
}
return ret;
}
static boolean checkValid(String s)
{
int check_x =0,check_o=0;
char[][] arr = new char[3][3];
for(int i = 0 ;i<3;i++)
{
for(int j=0;j<3;j++) {
arr[i][j] = s.charAt(i * 3 + j);
if (arr[i][j] == 'X')
check_x++;
if (arr[i][j] == 'O')
check_o++;
}
}
boolean o_win =false;
boolean x_win =false;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(arr[i][j] !='.' && checkBingo1(j,i,0,arr,arr[i][j]) || checkBingo2(j,i,0,arr,arr[i][j]) || checkBingo3(j,i,0,arr,arr[i][j])
||checkBingo4(j,i,0,arr,arr[i][j])) {
if(arr[i][j] == 'X')
x_win=true;
else if(arr[i][j] == 'O')
o_win = true;
}
}
}
if(o_win && x_win)
return false;
if(x_win && check_x- check_o == 1)
return true;
if(o_win && check_x == check_o)
return true;
if(!o_win && !x_win && check_x == 5 && check_o == 4)
return true;
return false;
}
}