Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active May 12, 2018 10:46
Show Gist options
  • Save MORTAL2000/da769b76ae8d40ce0924e2274b571bb5 to your computer and use it in GitHub Desktop.
Save MORTAL2000/da769b76ae8d40ce0924e2274b571bb5 to your computer and use it in GitHub Desktop.

Revisions

  1. MORTAL2000 revised this gist May 12, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions sudoku3.cpp
    Original file line number Diff line number Diff line change
    @@ -54,10 +54,10 @@ void printSudoku(int puzzle[9][9])
    for (int j = 0; j < 9; ++j)
    {
    std::cout << puzzle[i][j];
    if (j % 3 == 1 || j % 3 == 0)
    std::cout << " ";
    else
    if (j % 3 == 2)
    std::cout << "|";
    else
    std::cout << " ";
    }
    std::cout << "\n";
    }
    @@ -80,7 +80,7 @@ int main()
    printSudoku(puzzle);

    std::cin.ignore();
    std::cout << "---------------------\n";
    std::cout << "\n";

    solveSudoku(puzzle, 0, 0);
    printSudoku(puzzle);
  2. MORTAL2000 revised this gist May 12, 2018. 1 changed file with 88 additions and 0 deletions.
    88 changes: 88 additions & 0 deletions sudoku3.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    #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 == 1 || j % 3 == 0)
    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();
    }
  3. MORTAL2000 revised this gist May 12, 2018. 1 changed file with 70 additions and 0 deletions.
    70 changes: 70 additions & 0 deletions sudoku2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #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);
    }

  4. MORTAL2000 revised this gist May 11, 2018. 1 changed file with 70 additions and 0 deletions.
    70 changes: 70 additions & 0 deletions sudoku1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #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);
    }
  5. MORTAL2000 created this gist May 11, 2018.
    232 changes: 232 additions & 0 deletions sudoku.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,232 @@
    #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);
    }