Xinqi Bao's Git
2 * A new method to solve the puzzles,
3 * more nice orgnized, and more efficent I suppose.
4 * (at least compared to my original one)
6 * this one take puzzles and std input,
7 * an example puzzle could be found below:
18 * after calculation, the correct output should be:
19 * +------+------+-----
20 * |4 3 5 |2 6 9 |7 8 1
21 * |6 8 2 |5 7 1 |4 9 3
22 * |1 9 7 |8 3 4 |5 6 2
23 * +------+------+-----
24 * |8 2 6 |1 9 5 |3 4 7
25 * |3 7 4 |6 8 2 |9 1 5
26 * |9 5 1 |7 4 3 |6 2 8
27 * +------+------+-----
28 * |5 1 9 |3 2 6 |8 7 4
29 * |2 4 8 |9 5 7 |1 3 6
30 * |7 6 3 |4 1 8 |2 5 9
32 * Yet, the core computing part is still a kind of brute force,
33 * which is more intutive to implement.
34 * It might be implemented to my CheaterHub project in the future,
35 * but I'm getting really frustrated by 81 boxes in UI design.
44 vector
<vector
<int>> ori(9, vector
<int>(9));
45 vector
<pair
<int, int>> todo
;
48 // Another idea, is pre-generate checking list
49 // for each blank box in the puzzle,
50 // it should save some runtime for less checking.
51 for(int i
= 0; i
< 9; i
++)
53 for(int j
= 0; j
< 9; j
++)
57 todo
.push_back({i
, j
});
61 cout
<< "********************" << endl
;
63 while(cur
< todo
.size())
65 int x
= todo
[cur
].first
;
66 int y
= todo
[cur
].second
;
67 vector
<bool> check(10, false);
69 // TODO: there are 6 overlapping checks,
70 // including (x, y) itself twice.
72 for(int i
= 0; i
< 9; i
++)
73 check
[ori
[i
][y
]] = true;
75 for(int j
= 0; j
< 9; j
++)
76 check
[ori
[x
][j
]] = true;
78 int ibase
= x
/ 3 * 3;
79 int jbase
= y
/ 3 * 3;
80 for(int i
= 0; i
< 3; i
++)
81 for(int j
= 0; j
< 3; j
++)
82 check
[ori
[i
+ibase
][j
+jbase
]] = true;
84 int k
= ori
[x
][y
] + 1;
101 for(int i
= 0; i
< 9; i
++)
103 if(i
%3 == 0) cout
<< "+------+------+-----" << endl
;
104 for(int j
= 0; j
< 9; j
++)
106 if(j
%3 == 0) cout
<< "|";
107 cout
<< ori
[i
][j
] << " ";