-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktracking.cpp
More file actions
213 lines (203 loc) · 5.25 KB
/
backtracking.cpp
File metadata and controls
213 lines (203 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<bits/stdc++.h>
using namespace std;
//Rat in a Maze
bool maze[][4] = {{1,0,0,0}, {1,1,0,1}, {0,1,0,0}, {1,1,1,1}}, sol[4][4];
bool isSafe(int i, int j, int n){
if(i < n && j < n && maze[i][j] == 1){
return true;
}
return false;
}
bool RatMaze(int i, int j, int n){
if(i == n - 1 && j == n - 1){
sol[i][j] = 1;
return true;
}
if(isSafe(i, j, n) == true){
sol[i][j] = 1;
if(RatMaze(i + 1, j, n) == true){
return true;
}
else if(RatMaze(i, j + 1, n) == true){
return true;
}
sol[i][j] = 0;
}
return false;
}
bool solveMaze(){
if(RatMaze(0, 0, 4) == false){
return false;
}
else{
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cout << sol[i][j] << " ";
}
cout << "\n";
}
return true;
}
}
//N queens
bool board[4][4];
bool isSafe_(int row, int col, int N){
for(int i = 0; i < col; i++){
if(board[row][i]){
return false;
}
}
for(int i = row, j = col; i >= 0 && j >= 0; i--,j--){
if(board[i][j]){
return false;
}
}
for(int i = row, j = col; i < N && j >= 0; i++,j--){
if(board[i][j]){
return false;
}
}
return true;
}
bool solveNqueens(int col, int N){
if(col == N){
return true;
}
for(int i = 0; i < N; i++){
if(isSafe_(i, col, N) == true){
board[i][col] = 1;
if(solveNqueens(col + 1, N)){
return true;
}
board[i][col] = 0;
}
}
return false;
}
bool Nqueens(){
if(solveNqueens(0, 4) == false){
return false;
}
else{
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
board[i][j] = 0;
}
}
}
return false;
}
//Mazepath(4 -directions)
bool isSafe__(int i, int j, int m, int n){
if(i < 0 || j < 0 || i >= m || j >= n){
return false;
}
return true;
}
bool solveMazePath(int i, int j, int matrix[4][4]){
if(i == 3 && j == 3){
return true;
}
if(isSafe__(i, j, 4, 4)){
if(isSafe__(i, j + 1, 4, 4) && matrix[i][j + 1] == 0){
if(solveMazePath(i, j + 1, matrix)){
return true;
}
}
if(isSafe__(i + 1, j, 4, 4) && matrix[i + 1][j] == 0){
if(solveMazePath(i + 1, j, matrix)){
return true;
}
}
if(isSafe__(i, j - 1, 4, 4) && matrix[i][j - 1] == 0){
if(solveMazePath(i, j - 1, matrix)){
return true;
}
}
if(isSafe__(i - 1, j, 4, 4) && matrix[i - 1][j] == 0){
if(solveMazePath(i - 1, j, matrix)){
return true;
}
}
}
return false;
}
bool Mazepath(){
int matrix[4][4] = {{0,0,0,0}, {0,-1,-1,0}, {-1,0,0,0}, {0,0,0,0}};
if(solveMazePath(0, 0, matrix)){
return true;
}
return false;
}
//Mazepath(2- directions)
vector<string> ans;
void getMazePaths(int sr, int sc, int dr, int dc, string temp) {
if(sr == dr && sc == dc){
ans.push_back(temp);
return;
}
if(sc < dc){
getMazePaths(sr, sc + 1, dr, dc, temp + 'h');
}
if(sr < dr){
getMazePaths(sr + 1, sc, dr, dc, temp + 'v');
}
}
//FloofFill
void floodfill(vector<vector<int>> maze, int sr, int sc,int dr, int dc, string ans, vector<vector<bool>> visited){
if(sr == dr && sc == dc){
cout << ans << endl;
return;
}
if(sr < 0 || sr > dr || sc < 0 || sc > dc||maze[sr][sc] == 1 || visited[sr][sc] == true){
return;
}
visited[sr][sc] = true;
floodfill(maze,sr - 1,sc,dr,dc,ans + 't',visited);
floodfill(maze,sr,sc - 1,dr,dc,ans + 'l',visited);
floodfill(maze, sr + 1, sc,dr,dc,ans + 'd',visited);
floodfill(maze,sr,sc + 1,dr,dc,ans + 'r',visited);
visited[sr][sc] = false;
}
//Knights Tour
void knightsTour(vector<vector<int>> &chess, int row, int col, int move){
if(row < 0 || col < 0 || row >= chess.size() || col >= chess[0].size() || chess[row][col] > 0){
return;
}
else if(move == chess.size() * chess[0].size()){
chess[row][col] = move;
// displayBoard(chess);
chess[row][col] = 0;
return;
}
chess[row][col] = move;
knightsTour(chess, row - 2, col + 1, move + 1);
knightsTour(chess, row - 1, col + 2, move + 1);
knightsTour(chess, row + 1, col + 2, move + 1);
knightsTour(chess, row + 2, col + 1, move + 1);
knightsTour(chess, row + 2, col - 1, move + 1);
knightsTour(chess, row + 1, col - 2, move + 1);
knightsTour(chess, row - 1, col - 2, move + 1);
knightsTour(chess, row - 2, col - 1, move + 1);
chess[row][col] = 0;
}
//Word Break
void wordBreak(unordered_set<string> words, string s, string ans){
if(s == ""){
cout << ans << endl;
return;
}
string q = "";
for (int i = 0; i < s.size(); i++) {
q += s[i];
if(words.find(q) != words.end()){
wordBreak(words, s.substr(i + 1), ans + q + ' ');
}
}
}
int main(){
// cout << solveMaze();
// cout << Nqueens();
cout << Mazepath();
return 0;
}