Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Last active November 7, 2019 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinglunliao/5abc64e5a2ef695365f2 to your computer and use it in GitHub Desktop.
Save pinglunliao/5abc64e5a2ef695365f2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
const short SIZE = 9;
void prtMsg(bool b)
{
cout << (b?"yes":"no") << endl;
}
bool check(short value[SIZE])
{
bool num[SIZE + 1];
for(int i = 0; i < SIZE + 1; i++)
num[i] = false;
for(int i = 0; i < SIZE; i++)
{
//cout << "value[" << i << "] = " << value[i] << " ";
if( num[value[i]] == false )
{
num[value[i]] = true;
}
else
{
return false;
}
}
return true;
}
int main()
{
const short MATRIX_SIZE = SIZE*SIZE;
short sudoku[MATRIX_SIZE];
short matrix[SIZE];
short index[] = {
1, 2, 3,
10, 11, 12,
19, 20, 21
};
while( cin >> sudoku[0] )
{
for( int i = 1; i < MATRIX_SIZE; i++ )
cin >> sudoku[i];
bool isSudoku = true;
for(int k = 0; k < SIZE; k++)
{
short subScript = 27 * (k/3) + 3 * (k%3);
for( int j = 0; j < SIZE; j++ )
{
short temp = subScript + index[j];
matrix[j] = sudoku[temp-1];
}
isSudoku = check(matrix);
if(!isSudoku)
break;
}
prtMsg(isSudoku);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment