First, rough solution -- works up to a point
This commit is contained in:
parent
edc8a4922a
commit
b1bcc49e3f
11
Makefile
Normal file
11
Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
CXX = g++
|
||||
CXXFLAGS = -Wall
|
||||
|
||||
q: main.o
|
||||
$(CXX) $(CXXFLAGS) -o q main.o
|
||||
|
||||
main.o: main.cpp
|
||||
$(CXX) $(CXXFLAGS) -c main.cpp
|
||||
|
||||
clean :
|
||||
-rm *.o q
|
104
main.cpp
Normal file
104
main.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
|
||||
#define BOARD_SIZE 81
|
||||
#define BOARD_DIMENSION 9
|
||||
|
||||
class Sudoku {
|
||||
private:
|
||||
int board[BOARD_SIZE];
|
||||
|
||||
public:
|
||||
|
||||
Sudoku(std::string s) {
|
||||
for (unsigned int i = 0; i < s.length(); i++) {
|
||||
board[i] = (int) (s[i] - '0');
|
||||
}
|
||||
}
|
||||
|
||||
void solve() {
|
||||
// Very naive first attempt: Fails because it sets stuff wrongly and doesn't backtrack
|
||||
for (int y = 0; y < BOARD_DIMENSION; y++) {
|
||||
for (int x = 0; x < BOARD_DIMENSION; x++) {
|
||||
if (get(x, y) == 0) {
|
||||
auto possibilities = get_possibilities(x, y);
|
||||
|
||||
set(x, y, possibilities.front());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Real search:
|
||||
// 1. Sort possibilities by how many new constraints they create
|
||||
// 2. Start with the one creating the smallest amount of new constraints
|
||||
// 3. Continue until dead end
|
||||
// 4. If dead end: Try the next one
|
||||
}
|
||||
|
||||
int coordinates_to_id(int x, int y) {
|
||||
return y * BOARD_DIMENSION + x;
|
||||
}
|
||||
|
||||
int get(int x, int y) {
|
||||
return board[coordinates_to_id(x, y)];
|
||||
}
|
||||
|
||||
void set(int x, int y, int value) {
|
||||
board[coordinates_to_id(x, y)] = value;
|
||||
}
|
||||
|
||||
std::list<int> get_possibilities(int x, int y) {
|
||||
std::list<int> possibilities{1, 2, 3, 4, 5, 6, 7, 8 ,9};
|
||||
|
||||
// Remove all from this row
|
||||
for (int dx = 0; dx < BOARD_DIMENSION; dx++) {
|
||||
possibilities.remove(get(dx, y));
|
||||
}
|
||||
|
||||
// Remove all from this column
|
||||
for (int dy = 0; dy < BOARD_DIMENSION; dy++) {
|
||||
possibilities.remove(get(x, dy));
|
||||
}
|
||||
|
||||
// Remove all from this quadrant
|
||||
for (int dy = 0; dy < 3; dy++) {
|
||||
for (int dx = 0; dx < 3; dx++) {
|
||||
possibilities.remove(get((x - x % 3) + dx, (y - y % 3) + dy));
|
||||
}
|
||||
}
|
||||
|
||||
return possibilities;
|
||||
}
|
||||
|
||||
void print_board() {
|
||||
std::cout << board[0] << " ";
|
||||
for (unsigned int i = 1; i < BOARD_SIZE; i++) {
|
||||
if (i % 3 == 0 && i % 9 != 0) {
|
||||
std::cout << "| ";
|
||||
}
|
||||
if (i % 9 == 0) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (i % 27 == 0) {
|
||||
std::cout << "------+-------+------" << std::endl;
|
||||
}
|
||||
std::cout << board[i] << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
//char str[82];
|
||||
//cin >> str;
|
||||
//Sudoku solver(str);
|
||||
Sudoku solver("006001849030000000000020006000400320400003000600008000010060003000005004029074005");
|
||||
//Sudoku solver("850002400720000009004000000000107002305000900040000000000080070017000000000036040");
|
||||
solver.solve();
|
||||
solver.print_board();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user