Skip to content

Commit

Permalink
backtracking completed
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-sureja committed Jul 16, 2021
1 parent c2ae7aa commit 79205c1
Show file tree
Hide file tree
Showing 20 changed files with 2,472 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 09_backtracking/0_intro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
what is backtracking ?
video: https://youtu.be/SJ_pXT-L5IE
how it is implemented (logically)
video: https://youtu.be/DKCbsiDBN6c
Backtracking Algorithm:
Backtracking is an algorithmic-technique for solving problems recursively by trying
to build a solution incrementally. Solving one piece at a time, and removing those solutions
that fail to satisfy the constraints of the problem at any point of time (by time, here,
is referred to the time elapsed till reaching any level of the search tree) is the process of backtracking.
*/
54 changes: 54 additions & 0 deletions 09_backtracking/10_tug_of_war.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
link (little confusing) : https://www.geeksforgeeks.org/tug-of-war/
video: https://youtu.be/Q1fLW_zQr3M
*/



// ----------------------------------------------------------------------------------------------------------------------- //
static string allComb = "";
static int minDiff = INT_MAX;


void tugOfWar(int arr[], int n, int idx, vector<int> set1, vector<int> set2, int soset1, int soset2) {
if (idx == n) {
int diff = abs(soset1 - soset2);

if (diff < minDiff) {
minDiff = diff;

string ans = "";

for (int x : set1) ans += " " + to_string(x);
ans += " " + "$";
for (int x : set2) ans += " " + to_string(x);

allComb = ans.substr(1);
}
return;
}

if (set1.size() < (n + 1) / 2) {
set1.push_back(arr[idx]);
solve(arr, n, idx + 1, set1, set2, soset1 + arr[idx], soset2);
set1.pop_back();
}

if (set2.size() < (n + 1) / 2) {
set2.push_back(arr[idx]);
solve(arr, n, idx + 1, set1, set2, soset1, soset2 + arr[idx]);
set2.pop_back();
}
}


int main() {
int arr[] = { 23, 45, -34, 12, 0, 98, -99, 4, 189, -1, 4 };
int n = sizeof(arr) / sizeof(arr[0]);

vector<int> set1, set2;

tugOfWar(arr, n, 0, set1, set2, 0, 0);
return 0;
}
194 changes: 194 additions & 0 deletions 09_backtracking/11_find_shortest_safe_route_in_path_with_landmines.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
link: https://www.geeksforgeeks.org/find-shortest-safe-route-in-a-path-with-landmines/
note:
calculate length of the shortest safe route possible from any cell
in the first column to any cell in the last column of the matrix.
*/


// ----------------------------------------------------------------------------------------------------------------------- //
// C++ program to find shortest safe Route in
// the matrix with landmines
#include <bits/stdc++.h>
using namespace std;
#define R 12
#define C 10

// These arrays are used to get row and column
// numbers of 4 neighbours of a given cell
int rowNum[] = { -1, 0, 0, 1 };
int colNum[] = { 0, -1, 1, 0 };

// A function to check if a given cell (x, y)
// can be visited or not
bool isSafe(int mat[R][C], int visited[R][C],
int x, int y)
{
if (mat[x][y] == 0 || visited[x][y])
return false;

return true;
}

// A function to check if a given cell (x, y) is
// a valid cell or not
bool isValid(int x, int y)
{
if (x < R && y < C && x >= 0 && y >= 0)
return true;

return false;
}

// A function to mark all adjacent cells of
// landmines as unsafe. Landmines are shown with
// number 0
void markUnsafeCells(int mat[R][C])
{
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
// if a landmines is found
if (mat[i][j] == 0)
{
// mark all adjacent cells
for (int k = 0; k < 4; k++)
if (isValid(i + rowNum[k], j + colNum[k]))
mat[i + rowNum[k]][j + colNum[k]] = -1;
}
}
}

// mark all found adjacent cells as unsafe
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
if (mat[i][j] == -1)
mat[i][j] = 0;
}
}

// Uncomment below lines to print the path
/*for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cout << std::setw(3) << mat[i][j];
}
cout << endl;
}*/
}

// Function to find shortest safe Route in the
// matrix with landmines
// mat[][] - binary input matrix with safe cells marked as 1
// visited[][] - store info about cells already visited in
// current route
// (i, j) are coordinates of the current cell
// min_dist --> stores minimum cost of shortest path so far
// dist --> stores current path cost
void findShortestPathUtil(int mat[R][C], int visited[R][C],
int i, int j, int& min_dist, int dist)
{
// if destination is reached
if (j == C - 1)
{
// update shortest path found so far
min_dist = min(dist, min_dist);
return;
}

// if current path cost exceeds minimum so far
if (dist > min_dist)
return;

// include (i, j) in current path
visited[i][j] = 1;

// Recurse for all safe adjacent neighbours
for (int k = 0; k < 4; k++)
{
if (isValid(i + rowNum[k], j + colNum[k]) &&
isSafe(mat, visited, i + rowNum[k], j + colNum[k]))
{
findShortestPathUtil(mat, visited, i + rowNum[k],
j + colNum[k], min_dist, dist + 1);
}
}

// Backtrack
visited[i][j] = 0;
}

// A wrapper function over findshortestPathUtil()
void findShortestPath(int mat[R][C])
{
// stores minimum cost of shortest path so far
int min_dist = INT_MAX;

// create a boolean matrix to store info about
// cells already visited in current route
int visited[R][C];

// mark adjacent cells of landmines as unsafe
markUnsafeCells(mat);

// start from first column and take minimum
for (int i = 0; i < R; i++)
{
// if path is safe from current cell
if (mat[i][0] == 1)
{
// initialize visited to false
memset(visited, 0, sizeof visited);

// find shortest route from (i, 0) to any
// cell of last column (x, C - 1) where
// 0 <= x < R
findShortestPathUtil(mat, visited, i, 0,
min_dist, 0);

// if min distance is already found
if (min_dist == C - 1)
break;
}
}

// if destination can be reached
if (min_dist != INT_MAX)
cout << "Length of shortest safe route is "
<< min_dist;

else // if the destination is not reachable
cout << "Destination not reachable from "
<< "given source";
}

// Driver code
int main()
{
// input matrix with landmines shown with number 0
int mat[R][C] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }
};

// find shortest path
findShortestPath(mat);

return 0;
}
Loading

0 comments on commit 79205c1

Please sign in to comment.