Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active May 12, 2018 10:46
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 MORTAL2000/da769b76ae8d40ce0924e2274b571bb5 to your computer and use it in GitHub Desktop.
Save MORTAL2000/da769b76ae8d40ce0924e2274b571bb5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void gotoXY(int x, int y) {
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void getCursorXY(int &x, int&y) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
x = csbi.dwCursorPosition.X;
y = csbi.dwCursorPosition.Y;
}
}
int readKey()
{
int key = 0;
if (kbhit())
{
key = getch(); /* a key was pressed; read it */
if (key == 0) /* if zero, this is a two-char Fn key */
key = -getch(); /* read 2nd char & return as negative */
} /* to signal that this is an F-key */
return key;
}
int isAvailable(int puzzle[][9], int row, int col, int num)
{
int rowStart = (row / 3) * 3;
int colStart = (col / 3) * 3;
int i, j;
for (i = 0; i<9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[rowStart + (i % 3)][colStart + (i / 3)] == num) return 0;
}
return 1;
}
int fillSudoku(int puzzle[][9], int row, int col)
{
int i;
if (row<9 && col<9)
{
if (puzzle[row][col] != 0)
{
if ((col + 1)<9) return fillSudoku(puzzle, row, col + 1);
else if ((row + 1)<9) return fillSudoku(puzzle, row + 1, 0);
else return 1;
}
else
{
for (i = 0; i<9; ++i)
{
if (isAvailable(puzzle, row, col, i + 1))
{
puzzle[row][col] = i + 1;
if ((col + 1)<9)
{
if (fillSudoku(puzzle, row, col + 1)) return 1;
else puzzle[row][col] = 0;
}
else if ((row + 1)<9)
{
if (fillSudoku(puzzle, row + 1, 0)) return 1;
else puzzle[row][col] = 0;
}
else return 1;
}
}
}
return 0;
}
else return 1;
}
void PrintSudoku(int puzzle[9][9]) {
int j, i;
gotoXY(0, 0);
for (i = 0; i<9; ++i)
{
printf("{");
for (j = 0; j<9; ++j) {
printf("%1d", puzzle[i][j]);
if (j<8) printf(",");
}
printf("}\n");
}
printf("(r)eset (s)solve (q)uit\n");
}
void ResetSudoku(int puzzle[9][9]) {
int j, i;
gotoXY(0, 0);
for (i = 0; i<9; ++i)
{
printf("{");
for (j = 0; j<9; ++j) {
puzzle[i][j] = 0;
printf("%1d", puzzle[i][j]);
if (j<8) printf(",");
}
printf("}\n");
}
}
int main()
{
int i = 0, j = 0;
int puzzle[9][9];
/* ={{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0}};
*/
char c;
int xx;
int yy;
ResetSudoku(puzzle);
system("cls");
PrintSudoku(puzzle);
gotoXY(1, 0);
do {
c = readKey();
// if (c!=0) printf("%d",c);
if (c == -32) {
c = readKey();
// printf(",%d",c);
switch (c)
{
case 72:
if (j>0) j--;
break;
case 80:
if (j<8) j++;
break;
case 75:
if (i>0) i--;
break;
case 77:
if (i<8) { i++; }
break;
}
xx = i * 2 + 1;
yy = j;
// gotoXY(0,10);
// printf(" %d ",c);
}
else {
switch (c)
{
case 13:
i = 0;
if (j<8) j++;
xx = i * 2 + 1;
yy = j;
break;
case '0':
puzzle[j][i] = c - '0';
printf("%1d", puzzle[j][i]);
if (i<8) {
i++;
xx = i * 2 + 1;
}
yy = j;
gotoXY(xx, yy);
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (isAvailable(puzzle, j, i, c - '0')) {
puzzle[j][i] = c - '0';
printf("%1d", puzzle[j][i]);
if (i<8) {
i++;
xx = i * 2 + 1;
}
yy = j;
gotoXY(xx, yy);
}
break;
case 'r':
ResetSudoku(puzzle);
break;
case 's':
fillSudoku(puzzle, 0, 0);
PrintSudoku(puzzle);
break;
}
}
gotoXY(xx, yy);
// if (c!=0) printf("%1d",puzzle[j][i]);
} while (c != 'q');
gotoXY(0, 10);
return(0);
}
#include <iostream>
int isAvailable(int puzzle[][9], int row, int col, int num)
{
int sqCol = col / 3 * 3;
int sqRow = row / 3 * 3;
for (int i = 0; i<9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[sqRow + (i % 3)][sqCol + (i / 3)] == num) return 0;
}
return 1;
}
int solveSudoku(int puzzle[][9], int row, int col)
{
if (col == 9) {
col = 0;
row++;
}
if (row == 9) {
return 1;
}
if (puzzle[row][col] != 0) {
return solveSudoku(puzzle, row, col + 1);
}
for (int i = 1; i <= 9; ++i)
{
if (isAvailable(puzzle, row, col, i))
{
puzzle[row][col] = i;
if (solveSudoku(puzzle, row, col + 1))
return 1;
}
}
puzzle[row][col] = 0;
return 0;
}
void printSudoku(int puzzle[9][9])
{
for (int i = 0; i<9; ++i)
{
for (int j = 0; j<9; ++j) {
std::cout << puzzle[i][j] << " ";
}
std::cout << "\n";
}
}
int main()
{
int puzzle[9][9] = {
{ 3, 0, 6, 5, 0, 8, 4, 0, 0 },
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
solveSudoku(puzzle, 0, 0);
printSudoku(puzzle);
}
#include <iostream>
int isAvailable(int puzzle[][9], int row, int col, int num)
{
int sqCol = col - col % 3;
int sqRow = row - row % 3;
for (int i = 0; i < 9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[sqRow + (i % 3)][sqCol + (i / 3)] == num) return 0;
}
return 1;
}
int solveSudoku(int puzzle[][9], int row, int col)
{
if (col == 9) {
col = 0;
row++;
}
if (row == 9) {
return 1;
}
if (puzzle[row][col] != 0) {
return solveSudoku(puzzle, row, col + 1);
}
for (int i = 1; i <= 9; ++i)
{
if (isAvailable(puzzle, row, col, i))
{
puzzle[row][col] = i;
if (solveSudoku(puzzle, row, col + 1))
return 1;
}
}
puzzle[row][col] = 0;
return 0;
}
void printSudoku(int puzzle[9][9])
{
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
std::cout << puzzle[i][j] << " ";
}
std::cout << "\n";
}
}
int main()
{
int puzzle[9][9] = {
{ 3, 0, 6, 5, 0, 8, 4, 0, 0 },
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
solveSudoku(puzzle, 0, 0);
printSudoku(puzzle);
}
#include <iostream>
int isAvailable(int puzzle[][9], int row, int col, int num)
{
int sqCol = col - col % 3;
int sqRow = row - row % 3;
for (int i = 0; i < 9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[sqRow + (i % 3)][sqCol + (i / 3)] == num) return 0;
}
return 1;
}
int solveSudoku(int puzzle[][9], int row, int col)
{
if (col == 9) {
col = 0;
row++;
}
if (row == 9) {
return 1;
}
if (puzzle[row][col] != 0) {
return solveSudoku(puzzle, row, col + 1);
}
for (int i = 1; i <= 9; ++i)
{
if (isAvailable(puzzle, row, col, i))
{
puzzle[row][col] = i;
if (solveSudoku(puzzle, row, col + 1))
return 1;
}
}
puzzle[row][col] = 0;
return 0;
}
void printSudoku(int puzzle[9][9])
{
for (int i = 0; i < 9; ++i)
{
if(i % 3 == 0)
std::cout << "+-----+-----+-----+\n|";
else
std::cout << "|";
for (int j = 0; j < 9; ++j)
{
std::cout << puzzle[i][j];
if (j % 3 == 2)
std::cout << "|";
else
std::cout << " ";
}
std::cout << "\n";
}
std::cout << "+-----+-----+-----+\n";
}
int main()
{
int puzzle[9][9] = {
{ 3, 0, 6, 5, 0, 8, 4, 0, 0 },
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
printSudoku(puzzle);
std::cin.ignore();
std::cout << "\n";
solveSudoku(puzzle, 0, 0);
printSudoku(puzzle);
std::cin.ignore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment