sudoku-solver/solve.c

58 lines
1.4 KiB
C

/*
* Copyright (C) 2022 Camden Dixie O'Brien
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "solve.h"
#include <stdio.h>
int solve(struct sudoku *sud)
{
unsigned r, c, n, val, np, pr, pc;
bool match;
for (n = 0;; ++n) {
match = false;
/* Check if only one place a value can go on each row. */
for (r = 0; r < NDIGITS; ++r) {
for (val = 0; val < NDIGITS; ++val) {
np = 0;
for (c = 0; c < NDIGITS; ++c) {
if (!sud->cells[r][c].det && sud->cells[r][c].pvals[val]) {
pr = r;
pc = c;
++np;
if (np > 1)
break;
}
}
if (np == 1) {
if (update(sud, pr, pc, val) != OK)
return -1;
match = true;
}
}
}
/* Exit if no matches. */
if (!match)
return n;
}
}