Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int grid[100][100];
- struct POINT
- {
- int x, y;
- };
- void SolveSudoku(int n, int k)
- {
- if (k == n * n)
- printGrid();
- else
- {
- POINT g;
- g = FindUnassignedLocation(n);
- for (int num = 1; num <= n; num++)
- {
- if (isSafe(n, row, col, num))
- {
- grid[row][col] = num;
- SolveSudoku(n, k + 1);
- grid[row][col] = 0;
- }
- }
- }
- }
- POINT FindUnassignedLocation(int n)
- {
- int row, col;
- POINT g;
- for (row = 0; row < n; row++)
- for (col = 0; col < n; col++)
- if (grid[row][col] == 0)
- {
- g.x = row;
- g.y = col;
- return g;
- }
- return g;
- }
- bool UsedInRow(int n, int row, int num)
- {
- for (int col = 0; col < n; col++)
- if (grid[row][col] == num)
- return true;
- return false;
- }
- bool UsedInCol(int n, int col, int num)
- {
- for (int row = 0; row < n; row++)
- if (grid[row][col] == num)
- return true;
- return false;
- }
- bool isSafe(int n, int row,
- int col, int num)
- {
- return !UsedInCol(n, col, num) && !UsedInRow(n, row, num)&&
- grid[row][col] == 0;
- }
- void printGrid()
- {
- int row, col;
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- cout << grid[row][col] << " ";
- cout << endl;
- }
- }
- int main()
- {
- SolveSudoku(n, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement