-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuGraph.java
166 lines (137 loc) · 4.93 KB
/
SudokuGraph.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
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
import java.util.*;
public class SudokuGraph {
private final int NUM_col = 9;
private final int NUM_row = 9;
private int totalNodes = NUM_row * NUM_col;
private Graph graph = new Graph();
SudokuGraph(){
this.generateGraph();
this.connectEdges();
}
private void generateGraph(){
for (int i = 1; i < 82; i++){
graph.addById(i);
}
}
public int[][] getGridMatrix(){
int[][] matrix = new int[9][9];
int count = 1;
for(int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
matrix[i][j] = count;
count++;
}
}
return matrix;
}
private List<Integer> getConnections(int row, int col){
int[][] matrix = this.getGridMatrix();
ArrayList<Integer> connections = new ArrayList<Integer>();
// connect row
for(int i = col + 1; i < 9; i++){
connections.add(matrix[row][i]);
}
// connect columns
for(int i = row + 1; i < 9; i++){
connections.add(matrix[i][col]);
}
// connect all in the 3x3 block, not same row and col
if (row % 3 == 0){
if(col % 3 == 0){
connections.add(matrix[row+1][col+1]);
connections.add(matrix[row+1][col+2]);
connections.add(matrix[row+2][col+1]);
connections.add(matrix[row+2][col+2]);
}
else if(col % 3 == 1){
connections.add(matrix[row+1][col-1]);
connections.add(matrix[row+1][col+1]);
connections.add(matrix[row+2][col-1]);
connections.add(matrix[row+2][col+1]);
}
else if (col % 3 == 2){
connections.add(matrix[row+1][col-2]);
connections.add(matrix[row+1][col-1]);
connections.add(matrix[row+2][col-2]);
connections.add(matrix[row+2][col-1]);
}
}
else if(row % 3 == 1){
if (col % 3 == 0){
connections.add(matrix[row-1][col+1]);
connections.add(matrix[row-1][col+2]);
connections.add(matrix[row+1][col+1]);
connections.add(matrix[row+1][col+2]);
}
else if (col % 3 == 1){
connections.add(matrix[row-1][col-1]);
connections.add(matrix[row-1][col+1]);
connections.add(matrix[row+1][col-1]);
connections.add(matrix[row+1][col+1]);
}
else if (col % 3 == 2){
connections.add(matrix[row-1][col-2]);
connections.add(matrix[row-1][col-1]);
connections.add(matrix[row+1][col-2]);
connections.add(matrix[row+1][col-1]);
}
}
else if (row % 3 == 2){
if (col % 3 == 0){
connections.add(matrix[row-2][col+1]);
connections.add(matrix[row-2][col+2]);
connections.add(matrix[row-1][col+1]);
connections.add(matrix[row-1][col+2]);
}
else if (col % 3 == 1){
connections.add(matrix[row-2][col-1]);
connections.add(matrix[row-2][col+1]);
connections.add(matrix[row-1][col-1]);
connections.add(matrix[row-1][col+1]);
}
else if (col % 3 == 2){
connections.add(matrix[row-2][col-2]);
connections.add(matrix[row-2][col-1]);
connections.add(matrix[row-1][col-2]);
connections.add(matrix[row-1][col-1]);
}
}
return connections;
}
private void connectNodes(HashMap<Integer, List<Integer>> headConnections){
for (Integer head: headConnections.keySet()){
List<Integer> connections = headConnections.get(head);
for(Integer v: connections){
this.graph.addEdge(head, v);
}
}
}
private void connectEdges(){
int[][] matrix = this.getGridMatrix();
HashMap<Integer, List<Integer>> headConnections = new HashMap<Integer, List<Integer>>();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
int head = matrix[i][j];
List<Integer> connections = this.getConnections(i, j);
headConnections.put(head, connections);
}
}
this.connectNodes(headConnections);
}
public int getColor(int id){
return graph.getColor(id);
}
public void setColor(int id, int color){
graph.setColor(id, color);
}
public List<Integer> getNeighbors(int id){
return graph.getNeighbors(id);
}
public int getTotalNodes(){
return this.totalNodes;
}
public static void main(String[] args){
SudokuGraph sg = new SudokuGraph();
sg.graph.printVertices();
}
}