Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f460e32

Browse files
florist-notessoubh1k
authored andcommitted
Backtrack Problems (#87)
* BackTracking Implementation * _knights tour_ will PR this later * delete_knightsTour * Delete Sudoku.cpp * Delete nQueen.cpp * Backtrack
1 parent f775d44 commit f460e32

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// A Backtracking program in C++ to solve Sudoku problem
2+
#include <stdio.h>
3+
4+
// UNASSIGNED is used for empty cells in sudoku grid
5+
#define UNASSIGNED 0
6+
7+
// N is used for size of Sudoku grid. Size will be NxN
8+
#define N 9
9+
10+
// This function finds an entry in grid that is still unassigned
11+
bool FindUnassignedLocation(int grid[N][N], int &row, int &col);
12+
13+
// Checks whether it will be legal to assign num to the given row,col
14+
bool isSafe(int grid[N][N], int row, int col, int num);
15+
16+
/* Takes a partially filled-in grid and attempts to assign values to
17+
all unassigned locations in such a way to meet the requirements
18+
for Sudoku solution (non-duplication across rows, columns, and boxes) */
19+
bool SolveSudoku(int grid[N][N])
20+
{
21+
int row, col;
22+
23+
// If there is no unassigned location, we are done
24+
if (!FindUnassignedLocation(grid, row, col))
25+
return true; // success!
26+
27+
// consider digits 1 to 9
28+
for (int num = 1; num <= 9; num++)
29+
{
30+
// if looks promising
31+
if (isSafe(grid, row, col, num))
32+
{
33+
// make tentative assignment
34+
grid[row][col] = num;
35+
36+
// return, if success, yay!
37+
if (SolveSudoku(grid))
38+
return true;
39+
40+
// failure, unmake & try again
41+
grid[row][col] = UNASSIGNED;
42+
}
43+
}
44+
return false; // this triggers backtracking
45+
}
46+
47+
/* Searches the grid to find an entry that is still unassigned. If
48+
found, the reference parameters row, col will be set the location
49+
that is unassigned, and true is returned. If no unassigned entries
50+
remain, false is returned. */
51+
bool FindUnassignedLocation(int grid[N][N], int &row, int &col)
52+
{
53+
for (row = 0; row < N; row++)
54+
for (col = 0; col < N; col++)
55+
if (grid[row][col] == UNASSIGNED)
56+
return true;
57+
return false;
58+
}
59+
60+
/* Returns a boolean which indicates whether any assigned entry
61+
in the specified row matches the given number. */
62+
bool UsedInRow(int grid[N][N], int row, int num)
63+
{
64+
for (int col = 0; col < N; col++)
65+
if (grid[row][col] == num)
66+
return true;
67+
return false;
68+
}
69+
70+
/* Returns a boolean which indicates whether any assigned entry
71+
in the specified column matches the given number. */
72+
bool UsedInCol(int grid[N][N], int col, int num)
73+
{
74+
for (int row = 0; row < N; row++)
75+
if (grid[row][col] == num)
76+
return true;
77+
return false;
78+
}
79+
80+
/* Returns a boolean which indicates whether any assigned entry
81+
within the specified 3x3 box matches the given number. */
82+
bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num)
83+
{
84+
for (int row = 0; row < 3; row++)
85+
for (int col = 0; col < 3; col++)
86+
if (grid[row+boxStartRow][col+boxStartCol] == num)
87+
return true;
88+
return false;
89+
}
90+
91+
/* Returns a boolean which indicates whether it will be legal to assign
92+
num to the given row,col location. */
93+
bool isSafe(int grid[N][N], int row, int col, int num)
94+
{
95+
/* Check if 'num' is not already placed in current row,
96+
current column and current 3x3 box */
97+
return !UsedInRow(grid, row, num) &&
98+
!UsedInCol(grid, col, num) &&
99+
!UsedInBox(grid, row - row%3 , col - col%3, num);
100+
}
101+
102+
/* A utility function to print grid */
103+
void printGrid(int grid[N][N])
104+
{
105+
for (int row = 0; row < N; row++)
106+
{
107+
for (int col = 0; col < N; col++)
108+
printf("%2d", grid[row][col]);
109+
printf("\n");
110+
}
111+
}
112+
113+
/* Driver Program to test above functions */
114+
int main()
115+
{
116+
// 0 means unassigned cells
117+
int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
118+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
119+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
120+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
121+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
122+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
123+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
124+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
125+
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
126+
if (SolveSudoku(grid) == true)
127+
printGrid(grid);
128+
else
129+
printf("No solution exists");
130+
131+
return 0;
132+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* C/C++ program to solve N Queen Problem using backtracking */
2+
#define N 4
3+
#include<stdio.h>
4+
5+
/* A utility function to print solution */
6+
void printSolution(int board[N][N])
7+
{
8+
for (int i = 0; i < N; i++)
9+
{
10+
for (int j = 0; j < N; j++)
11+
printf(" %d ", board[i][j]);
12+
printf("\n");
13+
}
14+
}
15+
16+
/* A utility function to check if a queen can
17+
be placed on board[row][col]. Note that this
18+
function is called when "col" queens are
19+
already placed in columns from 0 to col -1.
20+
So we need to check only left side for
21+
attacking queens */
22+
bool isSafe(int board[N][N], int row, int col)
23+
{
24+
int i, j;
25+
26+
/* Check this row on left side */
27+
for (i = 0; i < col; i++)
28+
if (board[row][i])
29+
return false;
30+
31+
/* Check upper diagonal on left side */
32+
for (i=row, j=col; i>=0 && j>=0; i--, j--)
33+
if (board[i][j])
34+
return false;
35+
36+
/* Check lower diagonal on left side */
37+
for (i=row, j=col; j>=0 && i<N; i++, j--)
38+
if (board[i][j])
39+
return false;
40+
41+
return true;
42+
}
43+
44+
/* A recursive utility function to solve N Queen problem */
45+
bool solveNQUtil(int board[N][N], int col)
46+
{
47+
/* base case: If all queens are placed then return true */
48+
if (col >= N)
49+
return true;
50+
51+
/* Consider this column and try placing this queen in all rows one by one */
52+
for (int i = 0; i < N; i++)
53+
{
54+
/* Check if queen can be placed on board[i][col] */
55+
if ( isSafe(board, i, col) )
56+
{
57+
/* Place this queen in board[i][col] */
58+
board[i][col] = 1;
59+
60+
/* recur to place rest of the queens */
61+
if ( solveNQUtil(board, col + 1) )
62+
return true;
63+
64+
/* If placing queen in board[i][col] doesn't lead to a solution, then remove queen from board[i][col] */
65+
board[i][col] = 0; // BACKTRACK
66+
}
67+
}
68+
69+
/* If queen can not be place in any row in this colum col then return false */
70+
return false;
71+
}
72+
73+
/* This function solves the N Queen problem using
74+
Backtracking. It mainly uses solveNQUtil() to
75+
solve the problem. It returns false if queens
76+
cannot be placed, otherwise return true and
77+
prints placement of queens in the form of 1s.
78+
Please note that there may be more than one
79+
solutions, this function prints one of the
80+
feasible solutions.*/
81+
bool solveNQ()
82+
{
83+
int board[N][N] = { {0, 0, 0, 0},
84+
{0, 0, 0, 0},
85+
{0, 0, 0, 0},
86+
{0, 0, 0, 0}
87+
};
88+
89+
if ( solveNQUtil(board, 0) == false )
90+
{
91+
printf("Solution does not exist");
92+
return false;
93+
}
94+
95+
printSolution(board);
96+
return true;
97+
}
98+
99+
// driver program to test above function
100+
int main()
101+
{
102+
solveNQ();
103+
return 0;
104+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /