-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_sudoko.java
More file actions
76 lines (74 loc) · 1.9 KB
/
Copy pathstring_sudoko.java
File metadata and controls
76 lines (74 loc) · 1.9 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
package Recurssion_New;
public class string_sudoko {
public static void main(String args[]) {
char [][] board = {{'5','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}};
display(board);
System.out.println("...........................");
solver(board,0,0);
}
private static boolean solver(char[][] grid, int row, int col) {
if(col==grid[0].length) {
col=0;
row=row+1;
}
if(row==grid.length) {
display(grid);
return true;
}
boolean flag=false;
if(grid[row][col]!='.') {
flag=solver(grid,row,col+1);
}
else {
for(char i='1';i<='9';i++) {
if(isvalid(grid,row,col,i)) {
grid[row][col]=i;
flag=solver(grid,row,col+1);
if(flag==true){
return true;
}
grid[row][col]='.';
}
}
}
return flag;
}
private static boolean isvalid(char[][] grid, int row, int col, char val) {
int r=row;
int c=col;
for(int i=0;i<grid.length;i++) {
if(grid[i][c]==val) {
return false;
}
}
for(int i=0;i<grid[0].length;i++) {
if(grid[r][i]==val) {
return false;
}
}
r=row-(row%3);
c=col-(col%3);
for(int i=r;i<r+3;i++) {
for(int j=c;j<c+3;j++) {
if(grid[i][j]==val) return false;
}
}
return true;
}
static void display(char[][] grid) {
for(char i=0;i<grid.length;i++) {
for(char j=0;j<grid[0].length;j++) {
System.out.print(grid[i][j]+" ");
}
System.out.println();
}
}
}