-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.java
More file actions
158 lines (152 loc) · 4.74 KB
/
sudoku.java
File metadata and controls
158 lines (152 loc) · 4.74 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
class Pair {
int row;
int col;
Pair(int r,int c) {row=r;col=c;}
}
ArrayList<Pair> pos;
public void solveSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
pos=new ArrayList<Pair>();
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
if(board[i][j]=='.') {
pos.add(new Pair(i,j));
}
}
}
solve(board,0);
}
public boolean solve(char[][]board,int ind) {
if(ind==pos.size()) return true;
int row=pos.get(ind).row;
int col=pos.get(ind).col;
for(char i='1';i<='9';i++) {
if(valid(board,row,col,i)) {
board[row][col]=i;
if(solve(board,ind+1)) return true;
board[row][col]='.';
}
}
return false;
}
public class Solution {
ArrayList<ArrayList<Integer>> pos;
public void solveSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
pos=new ArrayList<ArrayList<Integer>>();
int empty=0;
for(int i=0;i<board.length;i++) {
ArrayList<Integer> a=new ArrayList<Integer>();
for(int j=0;j<board[0].length;j++) {
if(board[i][j]=='.') {
empty++;
a.add(j);
}
}
pos.add(a);
}
solve(board,0,pos.get(0).get(0),empty,1);
}
public boolean valid(char[][]board,int row,int col,char c) {
for(int i=0;i<board[row].length;i++)
if(board[row][i]==c)
return false;
for(int i=0;i<board.length;i++)
if(board[i][col]==c)
return false;
int i=0,j=0;
if(row<3) i=0;
else if(row<6) i=3;
else if(row<9) i=6;
if(col<3) j=0;
else if(col<6) j=3;
else if(col<9) j=6;
for(int m=i;m<i+3;m++) {
for(int n=j;n<j+3;n++) {
if(board[m][n]==c)
return false;
}
}
return true;
}
public boolean solve(char[][]board,int row,int col,int empty,int in) {
if(empty==0) return true;
for(char i='1';i<='9';i++) {
if(valid(board,row,col,i)) {
board[row][col]=i;
int r=row,ind=in; // gotta use new variable to avoid change
// func params, it wont be the same after backtracking!
if(pos.get(r).size()==ind) {
r++;ind=0;
}
if(r==pos.size())
return true; // last empty cell is filled so return true. empty is useless
int c=pos.get(r).get(ind);
if(solve(board,r,c,empty-1,ind+1))
return true;
board[row][col]='.';
}
}
return false;
}
}
public class Solution {
public boolean isValidSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
if(!valid(board,i,j,board[i][j])) return false;
}
}
return true;
}
public boolean valid(char[][]board,int row,int col,char c) {
if(c=='.') return true;
for(int i=col+1;i<board[row].length;i++)
if(board[row][i]==c)
return false;
for(int i=row+1;i<board.length;i++)
if(board[i][col]==c)
return false;
int i=0,j=0;
if(row<3) i=0;
else if(row<6) i=3;
else if(row<9) i=6;
if(col<3) j=0;
else if(col<6) j=3;
else if(col<9) j=6;
for(int m=row;m<i+3;m++) {
for(int n=(m==row?col+1:j);n<j+3;n++) {
if(board[m][n]==c)
return false;
}
}
return true;
}
public boolean valid(char[][]board,int row,int col,char c) {
if(c=='.') return true;
for(int i=0;i<board[row].length;i++)
if(i!=col&&board[row][i]==c)
return false;
for(int i=0;i<board.length;i++)
if(i!=row&&board[i][col]==c)
return false;
int i=0,j=0;
if(row<3) i=0;
else if(row<6) i=3;
else if(row<9) i=6;
if(col<3) j=0;
else if(col<6) j=3;
else if(col<9) j=6;
for(int m=i;m<i+3;m++) {
for(int n=j;n<j+3;n++) {
if(!(m==row&&n==col)&&board[m][n]==c)
return false;
}
}
return true;
}
}