forked from GFGSC-RTU/GFG-SDE-Sheet-Solution-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GFGSC-RTU#60 from mohakmaheshwari1205/main
Backtracking Problem
- Loading branch information
Showing
2 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* C++ program to solve N Queen Problem using | ||
backtracking */ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define N 4 | ||
|
||
int ld[30] = { 0 }; | ||
|
||
int rd[30] = { 0 }; | ||
|
||
int cl[30] = { 0 }; | ||
|
||
void printSolution(int board[N][N]) | ||
{ | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) | ||
cout<<" "<< board[i][j]<<" "; | ||
cout<<endl; | ||
} | ||
} | ||
|
||
|
||
bool solveNQUtil(int board[N][N], int col) | ||
{ | ||
/* base case: If all queens are placed | ||
then return true */ | ||
if (col >= N) | ||
return true; | ||
|
||
/* Consider this column and try placing | ||
this queen in all rows one by one */ | ||
for (int i = 0; i < N; i++) { | ||
/* Check if the queen can be placed on | ||
board[i][col] */ | ||
/* A check if a queen can be placed on | ||
board[row][col].We just need to check | ||
ld[row-col+n-1] and rd[row+coln] where | ||
ld and rd are for left and right | ||
diagonal respectively*/ | ||
if ((ld[i - col + N - 1] != 1 && rd[i + col] != 1) && cl[i] != 1) { | ||
/* Place this queen in board[i][col] */ | ||
board[i][col] = 1; | ||
ld[i - col + N - 1] = rd[i + col] = cl[i] = 1; | ||
|
||
/* recur to place rest of the queens */ | ||
if (solveNQUtil(board, col + 1)) | ||
return true; | ||
|
||
/* If placing queen in board[i][col] | ||
doesn't lead to a solution, then | ||
remove queen from board[i][col] */ | ||
board[i][col] = 0; // BACKTRACK | ||
ld[i - col + N - 1] = rd[i + col] = cl[i] = 0; | ||
} | ||
} | ||
|
||
/* If the queen cannot be placed in any row in | ||
this column col then return false */ | ||
return false; | ||
} | ||
|
||
bool solveNQ() | ||
{ | ||
int board[N][N] = { { 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 } }; | ||
|
||
if (solveNQUtil(board, 0) == false) { | ||
cout<<"Solution does not exist"; | ||
return false; | ||
} | ||
|
||
printSolution(board); | ||
return true; | ||
} | ||
|
||
int main() | ||
{ | ||
solveNQ(); | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// C++ program to solve Rat in a Maze problem using | ||
// backtracking | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
// Maze size | ||
#define N 4 | ||
|
||
bool solveMazeUtil(int maze[N][N], int x, int y,int sol[N][N]); | ||
|
||
// A utility function to print solution matrix sol[N][N] | ||
void printSolution(int sol[N][N]) | ||
{ | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) | ||
cout<<" "<<sol[i][j]<<" "; | ||
cout<<endl; | ||
} | ||
} | ||
|
||
// A utility function to check if x, y is valid index for | ||
// N*N maze | ||
bool isSafe(int maze[N][N], int x, int y) | ||
{ | ||
// if (x, y outside maze) return false | ||
if (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1) | ||
return true; | ||
return false; | ||
} | ||
|
||
|
||
bool solveMaze(int maze[N][N]) | ||
{ | ||
int sol[N][N] = { { 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 }, | ||
{ 0, 0, 0, 0 } }; | ||
if (solveMazeUtil(maze, 0, 0, sol) == false) { | ||
cout<<"Solution doesn't exist"; | ||
return false; | ||
} | ||
printSolution(sol); | ||
return true; | ||
} | ||
|
||
// A recursive utility function to solve Maze problem | ||
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]) | ||
{ | ||
// if (x, y is goal) return true | ||
if (x == N - 1 && y == N - 1 && maze[x][y] == 1) { | ||
sol[x][y] = 1; | ||
return true; | ||
} | ||
// Check if maze[x][y] is valid | ||
if (isSafe(maze, x, y) == true) { | ||
// Check if the current block is already part of | ||
// solution path. | ||
if (sol[x][y] == 1) | ||
return false; | ||
// mark x, y as part of solution path | ||
sol[x][y] = 1; | ||
/* Move forward in x direction */ | ||
if (solveMazeUtil(maze, x + 1, y, sol) == true) | ||
return true; | ||
// If moving in x direction doesn't give solution | ||
// then Move down in y direction | ||
if (solveMazeUtil(maze, x, y + 1, sol) == true) | ||
return true; | ||
// If none of the above movements work then | ||
// BACKTRACK: unmark x, y as part of solution path | ||
sol[x][y] = 0; | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
// driver program to test above function | ||
int main() | ||
{ | ||
int maze[N][N] = { { 1, 0, 0, 0 }, | ||
{ 1, 1, 0, 1 }, | ||
{ 0, 1, 0, 0 }, | ||
{ 1, 1, 1, 1 } }; | ||
solveMaze(maze); | ||
return 0; | ||
} | ||
|
||
|