-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.txt
97 lines (95 loc) · 2.85 KB
/
sudoku.txt
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
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static int singleIteration(int[][] puzzle){
int change=0; //variable to check if there is any change in puzzle after singleIteration function has run completely
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(puzzle[i][j]==0){ //condition to check if the box is empty
int[] constant=new int[9]; //array created to mark those digits which are already there in the //row or column or block
for(int k=0; k<9; k++){
if(puzzle[i][k]!=0){ // checking for row
constant[puzzle[i][k]-1]=1;
}
if(puzzle[k][j]!=0){ //checking for column
constant[puzzle[k][j]-1]=1;
}
}
/* In next section i am assuming the puzzle array to be made up of 9 3*3 sized blocks or a multidimensional array made up of 9 multidimensional arrays of size 3*3. Thus only after checking which block the box is in, i can check for digits present in the box. In simple words next section of code checks which digits are there in the block. */
int block_i=i/3;
int block_j=j/3;
for(int small_i=block_i*3; small_i<(block_i*3+3); small_i++){
for(int small_j=block_j*3; small_j<(block_j*3+3); small_j++){
if(puzzle[small_i][small_j]!=0){
constant[puzzle[small_i][small_j]-1]=1;
}
}
}
int sum=0; //summing all elements of constant array
for(int k=0; k<9; k++){
if(constant[k]==1){
sum++;
}
}
if(sum==8){ // if sum is 8 it implies that there is only one possibility for that box
for(int k=0; k<9; k++){
if(constant[k]==0){
puzzle[i][j]=k;
change++; // Updating the change variable
}
}
}
}
}
}
return change;
}
public static void main (String[] args) throws java.lang.Exception
{
int[][] puzzleArray= new int[9][9];
puzzleArray[0][2]=3;
puzzleArray[0][4]=2;
puzzleArray[0][6]=6;
puzzleArray[1][0]=9;
puzzleArray[1][3]=3;
puzzleArray[1][5]=5;
puzzleArray[1][8]=1;
puzzleArray[2][2]=1;
puzzleArray[2][3]=8;
puzzleArray[2][5]=6;
puzzleArray[2][6]=4;
puzzleArray[3][2]=8;
puzzleArray[3][3]=1;
puzzleArray[3][5]=2;
puzzleArray[3][6]=9;
puzzleArray[4][0]=7;
puzzleArray[4][8]=8;
puzzleArray[5][2]=6;
puzzleArray[5][3]=7;
puzzleArray[5][5]=8;
puzzleArray[5][6]=2;
puzzleArray[6][2]=2;
puzzleArray[6][3]=6;
puzzleArray[6][5]=9;
puzzleArray[6][6]=5;
puzzleArray[7][0]=8;
puzzleArray[7][3]=2;
puzzleArray[7][5]=3;
puzzleArray[7][8]=9;
puzzleArray[8][2]=5;
puzzleArray[8][4]=1;
puzzleArray[8][6]=3;
int i;
i=singleIteration(puzzleArray);
while(i!=0){
i=singleIteration(puzzleArray);
}
for(int j=0; j<9; j++){
for(int k=0; k<9; k++){
System.out.print(puzzleArray[j][k]);
}
}
}
}