-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoemaze.java
More file actions
187 lines (168 loc) · 3.96 KB
/
tictactoemaze.java
File metadata and controls
187 lines (168 loc) · 3.96 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
ID: grifync1
LANG: JAVA
PROG: tictactoemaze
*/
//:O :/ :) :D :(
//lil lil peezy
import java.util.*;
import java.io.*;
public class tictactoemaze {
public static int conv(String s) {
return Integer.parseInt(s);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static void print(int num) {
System.out.println(num);
}
public static void prints(String s) {
System.out.println(s);
}
public static void printa(int[] a) {
for (int i = 0; i < a.length; i++) {
if (i == 0) {
System.out.print(a[i]);
} else {
System.out.print(" " + a[i]);
}
}
}
public static int[] sort(int[] nums) {
Arrays.sort(nums);
return nums;
}
public static void main(String[] args) throws IOException {
//BufferedReader in = new BufferedReader(new FileReader("tictactoemaze.in"));
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("tictactoemaze.out")));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
//StringTokenizer st = new StringTokenizer(in.readLine());
int len = conv(in.readLine());
String[][] s = new String[len][len];
int f = 0;
int s1 = 0;
for(int i=0; i<len; i++) {
String l = in.readLine();
for(int j=0; j<len; j++) {
s[i][j]=l.substring(j*3, (j+1)*3);
if(s[i][j].equals("BBB")) {
f=i;
s1=j;
}
}
}
char[][] gr = new char[3][3];
for(int i =0; i<3; i++) {for(int j=0; j<3; j++) {gr[i][j]='.';}};
boolean[][][] vis = new boolean[len][len][19683];
out.println(dfs(f, s1, gr, s, vis));
in.close();
out.close();
}
public static int dfs(int r, int c, char[][] g, String[][] a, boolean[][][] vis) {
for(char[] t: g) {
System.out.println(Arrays.toString(t));
}
System.out.println(r+" "+c);
System.out.println("-------------------");
if(iw(g)) {
return 1;
}
int cnt = 0;
for(int i=0; i<4; i++) {
int rd = 0;
int cd = 0;
if(i==0) {
rd = -1;
cd = 0;
}
else if(i==1) {
rd = 0;
cd = 1;
}
else if(i==2) {
rd=1;
cd = 0;
}
else if(i==3) {
rd = 0;
cd = -1;
}
//System.out.println("rd: "+rd +" cd:"+cd);
int nr = r+rd;
int nc = c+cd;
if(nr<0 || nr>=a.length || nc<0 || nc>=a.length || (a[nr][nc].equals("###"))) {
continue;
}
if(a[nr][nc].charAt(0)=='O' || a[nr][nc].charAt(0)=='M') {
g[a[nr][nc].charAt(1)-'1'][a[nr][nc].charAt(2)-'1']=a[nr][nc].charAt(0);
}
char[][] k = new char[3][3];
for(int h =0; h<3; h++) {for(int j=0; j<3; j++) {k[h][j]=g[h][j];}};
if(a[nr][nc].charAt(0)=='O' || a[nr][nc].charAt(0)=='M') {
k[a[nr][nc].charAt(1)-'1'][a[nr][nc].charAt(2)-'1']=a[nr][nc].charAt(0);
}
if(!vis[nr][nc][ccti(k)]) {
vis[nr][nc][ccti(k)]=true;
cnt+=dfs(nr, nc, k, a, vis);
}
}
vis[r][c][ccti(g)]=false;
return cnt;
}
public static int ccti(char[][] grid) {
int ci = 0;
int f = 0;
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
if(grid[i][j]=='.') {
f++;
continue;
}
else if(grid[i][j]=='O') {
ci+=Math.pow(3, f);
}
else {
ci+=2*Math.pow(3, f);
}
f++;
}
}
return ci;
}
public static boolean iw(char[][] g) {
for(int i=0; i<3; i++) {
if(g[i][0]=='O' && g[i][1]=='O' && g[i][2]=='M') {
return true;
}
else if(g[i][0]=='M' && g[i][1]=='O' && g[i][2]=='O') {
return true;
}
}
if(g[0][0]=='M' && g[1][1]=='O' && g[2][2]=='O') {
return true;
}
else if(g[0][0]=='O' && g[1][1]=='O' && g[2][2]=='M') {
return true;
}
else if(g[0][2]=='M' && g[1][1]=='O' && g[2][0]=='O') {
return true;
}
else if(g[0][2]=='O' && g[1][1]=='O' && g[2][0]=='M') {
return true;
}
for(int i=0; i<3; i++) {
if(g[0][i]=='O' && g[1][i]=='O' && g[2][i]=='M') {
return true;
}
else if(g[0][i]=='M' && g[1][i]=='O' && g[2][i]=='O') {
return true;
}
}
return false;
}
}