forked from 1989chenguo/CloudComputingLabs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsudoku_basic.cc
More file actions
68 lines (57 loc) · 1.18 KB
/
sudoku_basic.cc
File metadata and controls
68 lines (57 loc) · 1.18 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
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include "sudoku.h"
int board[N];
int spaces[N];
int nspaces;
int (*chess)[COL] = (int (*)[COL])board;
static void find_spaces()
{
nspaces = 0;
for (int cell = 0; cell < N; ++cell) {
if (board[cell] == 0)
spaces[nspaces++] = cell;
}
}
void input(const char in[N])
{
for (int cell = 0; cell < N; ++cell) {
board[cell] = in[cell] - '0';
assert(0 <= board[cell] && board[cell] <= NUM);
}
find_spaces();
}
bool available(int guess, int cell)
{
for (int i = 0; i < NEIGHBOR; ++i) {
int neighbor = neighbors[cell][i];
if (board[neighbor] == guess) {
return false;
}
}
return true;
}
bool solve_sudoku_basic(int which_space)
{
if (which_space >= nspaces) {
return true;
}
// find_min_arity(which_space);
int cell = spaces[which_space];
for (int guess = 1; guess <= NUM; ++guess) {
if (available(guess, cell)) {
// hold
assert(board[cell] == 0);
board[cell] = guess;
// try
if (solve_sudoku_basic(which_space+1)) {
return true;
}
// unhold
assert(board[cell] == guess);
board[cell] = 0;
}
}
return false;
}