Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created February 7, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanfal/9e3a00da9667bb8b84f1f21d226e4e6c to your computer and use it in GitHub Desktop.
Save juanfal/9e3a00da9667bb8b84f1f21d226e4e6c to your computer and use it in GitHub Desktop.
Check validity of sudoku board
// 03.sudoku.cpp
// juanfc 2018-02-06
//
#include <iostream>
#include <array>
using namespace std;
const int N = 9;
typedef array<int, N> TRow;
typedef array<TRow, N> TSudoku;
void checkSudoku(int n, TSudoku tab);
int countCell(TSudoku m, int cell, int n);
int countCol(TSudoku m, int col, int n);
int countRow(TSudoku m, int row, int n);
int main()
{
TSudoku board1 = {{
{{5, 3, 0, 0, 7, 0, 0, 0, 0}},
{{6, 0, 0, 1, 9, 5, 0, 0, 0}},
{{0, 9, 8, 0, 0, 0, 0, 6, 0}},
{{8, 0, 0, 0, 6, 0, 0, 0, 3}},
{{4, 0, 0, 8, 0, 3, 0, 0, 1}},
{{7, 0, 0, 0, 2, 0, 0, 0, 6}},
{{0, 6, 0, 0, 0, 0, 2, 8, 0}},
{{0, 0, 0, 4, 1, 9, 0, 0, 5}},
{{0, 0, 0, 0, 8, 0, 0, 7, 9}}
}
};
TSudoku board2 = {{
{{5, 3, 0, 0, 7, 0, 0, 0, 0}},
{{6, 0, 0, 1, 9, 5, 0, 0, 0}},
{{0, 9, 8, 0, 0, 0, 0, 6, 0}},
{{8, 0, 3, 0, 6, 0, 0, 0, 3}},
{{4, 0, 0, 8, 0, 3, 0, 0, 1}},
{{7, 0, 0, 0, 2, 0, 0, 0, 6}},
{{0, 6, 0, 0, 0, 0, 2, 8, 0}},
{{0, 0, 0, 4, 1, 9, 0, 0, 5}},
{{0, 0, 0, 0, 8, 0, 0, 7, 9}}
}
};
TSudoku board3 = {{
{{5, 3, 0, 0, 7, 0, 0, 0, 0}},
{{6, 0, 0, 1, 9, 5, 0, 0, 0}},
{{0, 9, 8, 0, 2, 0, 0, 6, 0}},
{{8, 0, 0, 0, 6, 0, 0, 0, 3}},
{{4, 0, 0, 8, 0, 3, 0, 0, 1}},
{{7, 0, 0, 0, 2, 0, 0, 0, 6}},
{{0, 6, 0, 0, 0, 0, 2, 8, 0}},
{{0, 0, 0, 4, 1, 9, 0, 0, 5}},
{{0, 0, 0, 0, 8, 0, 0, 7, 9}}
}
};
TSudoku board4 = {{
{{5, 3, 0, 0, 7, 0, 6, 0, 0}},
{{6, 0, 0, 1, 9, 5, 0, 0, 0}},
{{0, 9, 8, 0, 0, 0, 0, 6, 0}},
{{8, 0, 0, 0, 6, 0, 0, 0, 3}},
{{4, 0, 0, 8, 0, 3, 0, 0, 1}},
{{7, 0, 0, 0, 2, 0, 0, 0, 6}},
{{0, 6, 0, 0, 0, 0, 2, 8, 0}},
{{0, 0, 0, 4, 1, 9, 0, 0, 5}},
{{0, 0, 0, 0, 8, 0, 0, 7, 9}}
}
};
checkSudoku(1, board1);
checkSudoku(2, board2);
checkSudoku(3, board3);
checkSudoku(4, board4);
return 0;
}
bool validSudoku(TSudoku m);
void printBoard(TSudoku m);
void checkSudoku(int n, TSudoku tab)
{
// printBoard(tab);
cout << "The board number " << n << " is a";
if (validSudoku(tab)) // to solve!
cout << " CORRECT Sudoku" << endl;
else
cout << "n INCORRECT Sudoku" << endl;
}
int countCol(TSudoku m, int col, int n);
int countRow(TSudoku m, int row, int n);
int countCell(TSudoku m, int cell, int n);
bool validSudoku(TSudoku m)
{
bool isOk = true;
int one2nine = 1;
while (isOk and one2nine <= 9) {
int col = 0;
while (isOk and col < N) {
isOk = (countCol(m, col, one2nine) <= 1);
++col;
}
++one2nine;
}
one2nine = 1;
while (isOk and one2nine <= 9) {
int row = 0;
while (isOk and row < N) {
isOk = (countRow(m, row, one2nine) <= 1);
++row;
}
++one2nine;
}
one2nine = 1;
while (isOk and one2nine <= 9) {
int cell = 0;
while (isOk and cell < 9 ) {
isOk = countCell(m, cell, one2nine) <= 1;
++cell;
}
++one2nine;
}
return isOk;
}
int countCell(TSudoku m, int cell, int n)
{
int cnt = 0;
// There could be many ways to refer to cells
// I've numbered cells from 0-8, going from the
// top-left-> top-right to the bottom-right
// The map is:
// 0 -> 0,0 1 -> 0,3 2 -> 0,6
// 3 -> 3,0 4 -> 3,3 5 -> 3,6
// 6 -> 6,0 7 -> 6,3 8 -> 6,6
int iorig = (cell / 3) * 3;
int jorig = (cell % 3) * 3;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (m[iorig + i][jorig + j] == n)
++cnt;
return cnt;
}
int countCol(TSudoku m, int col, int n)
{
int cnt = 0;
for (int row = 0; row < N; ++row)
if (m[row][col] == n)
++cnt;
return cnt;
}
int countRow(TSudoku m, int row, int n)
{
int cnt = 0;
for (int col = 0; col < N; ++col)
if (m[row][col] == n)
++cnt;
return cnt;
}
void printBoard(TSudoku m)
{
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j)
if (m[i][j] > 0)
cout << m[i][j] << " ";
else
cout << " ";
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment