Skip to content

Commit efd537f

Browse files
authored
Initial commit
1 parent 9611386 commit efd537f

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

Backtracking/No.OfIsland.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//author @Nishant
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
int totalBridges(int **mat, int n, int m);
7+
void removeIsland(int **mat, int n, int m, int i, int j);
8+
bool isSafe(int n, int m, int i, int j);
9+
10+
int main(){
11+
int m, n;
12+
cin >> n >> m;
13+
int **mat = (int **)malloc(n * sizeof(int *));
14+
for(int i=0; i<n; i++){
15+
mat[i] = (int *)malloc(m * sizeof(int));
16+
}
17+
for(int i=0; i<n; i++){
18+
for(int j=0; j<m; j++){
19+
cin >> mat[i][j];
20+
}
21+
}
22+
// for(int i=0; i<m; i++){
23+
// cout << endl;
24+
// for(int j=0; j<n; j++){
25+
// cout << mat[i][j] << "\t";
26+
// }
27+
// }
28+
cout << totalBridges(mat, n, m);
29+
return 0;
30+
}
31+
32+
int totalBridges(int **mat, int n, int m){
33+
int count = 0;
34+
//cout << mat[0][0] << endl;
35+
for(int i=0; i<n; i++){
36+
for(int j=0; j<m; j++){
37+
if(mat[i][j] == 1){
38+
count++;
39+
removeIsland(mat, n, m, i, j);
40+
}
41+
}
42+
}
43+
return count - 1;
44+
}
45+
46+
void removeIsland(int **mat, int n, int m, int i, int j){
47+
if(isSafe(n, m, i, j) && mat[i][j]==1){
48+
mat[i][j] = 0;
49+
removeIsland(mat, n, m, i - 1, j - 1);
50+
removeIsland(mat, n, m, i - 1, j);
51+
removeIsland(mat, n, m, i - 1, j + 1);
52+
removeIsland(mat, n, m, i, j - 1);
53+
removeIsland(mat, n, m, i, j + 1);
54+
removeIsland(mat, n, m, i + 1, j - 1);
55+
removeIsland(mat, n, m, i + 1, j);
56+
removeIsland(mat, n, m, i + 1, j + 1);
57+
}
58+
}
59+
60+
bool isSafe(int n, int m, int i, int j){
61+
if(i<0 || i>=n){
62+
return false;
63+
}
64+
if(j<0 || j>=m){
65+
return false;
66+
}
67+
return true;
68+
}

Backtracking/RatNMaze.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//author @Nishant
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
void showPath(int **sol, int N) {
7+
for (int i = 0; i < N; i++) {
8+
for (int j = 0; j < N; j++)
9+
cout << " " << sol[i][j] << " ";
10+
cout << endl;
11+
}
12+
}
13+
14+
bool isValidPlace(int **mat, int N, int x, int y) { //function to check place is inside the maze and have value 1
15+
if(x >= 0 && x < N && y >= 0 && y < N && mat[x][y] == 1)
16+
return true;
17+
return false;
18+
}
19+
20+
bool solveRatMaze(int **sol, int **mat, int N, int x, int y) {
21+
if(x == N-1 && y == N-1) { //when (x,y) is the bottom right room
22+
sol[x][y] = 1;
23+
return true;
24+
}
25+
26+
if(isValidPlace(mat, N, x, y) == true) { //check whether (x,y) is valid or not
27+
sol[x][y] = 1; //set 1, when it is valid place
28+
if (solveRatMaze(sol, mat, N, x+1, y) == true) //find path by moving right direction
29+
return true;
30+
if (solveRatMaze(sol, mat, N, x, y+1) == true) //when x direction is blocked, go for bottom direction
31+
return true;
32+
sol[x][y] = 0; //if both are closed, there is no path
33+
return false;
34+
}
35+
return false;
36+
}
37+
38+
bool findSolution(int **sol, int **mat, int N) {
39+
if(solveRatMaze(sol, mat, N, 0, 0) == false) {
40+
cout << "There is no path";
41+
return false;
42+
}
43+
cout << "\nPath to get out of maze" << endl;
44+
showPath(sol, N);
45+
return true;
46+
}
47+
48+
int main() {
49+
int N;
50+
cout << "Enter n value:";
51+
cin >> N;
52+
int **mat = (int **)malloc(N * sizeof(int *));
53+
for(int i=0; i<N; i++){
54+
mat[i] = (int *)malloc(N * sizeof(int));
55+
}
56+
int **sol = (int **)malloc(N * sizeof(int *));
57+
for(int i=0; i<N; i++){
58+
sol[i] = (int *)malloc(N * sizeof(int));
59+
}
60+
for(int i=0; i<N; i++){
61+
for(int j=0; j<N; j++){
62+
cin >> mat[i][j];
63+
}
64+
}
65+
findSolution(sol, mat, N);
66+
}

0 commit comments

Comments
 (0)