-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver - recursive dfs.cpp
120 lines (94 loc) · 2.85 KB
/
solver - recursive dfs.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n,m;
vector<vector<int>> puzzle;
map<char, pair<int,int>> colorIndexMap;
bool solved = false;
int colorsPresence[26];
void colorManager(int color, vector<vector<int>> vis, vector<vector<int>> grid);
// dfs
void solveThisColor(int row, int col, vector<vector<int>> vis, vector<vector<int>> grid,int color, int recursionLimit) {
if (solved) {
return;
}
vis[row][col] = 1;
if (grid[row][col] == color && ((colorIndexMap[color].first) != row || (colorIndexMap[color].second != col))) {
colorManager(color+1, vis, grid);
return;
}
grid[row][col] = color;
int dr[4] = {-1,0,1,0};
int dc[4] = {0,-1,0,1};
for (int i = 0; i < 4; i++) {
int newRow = row + dr[i];
int newCol = col + dc[i];
if (newRow < n && newCol < m && newRow >= 0 && newCol >= 0) {
if (vis[newRow][newCol] == 0) {
solveThisColor(newRow, newCol, vis, grid, color, 0);
}
if (vis[newRow][newCol] == 2 && grid[newRow][newCol] == color) {
solveThisColor(newRow, newCol, vis, grid, color, 0);
}
}
}
}
void colorManager(int color, vector<vector<int>> vis, vector<vector<int>> grid) {
if (color >= 26) {
bool ok = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 0)
ok = 0;
}
}
if (ok) {
solved = 1;
puzzle = grid;
}
return;
}
if (solved == 1) {
return;
}
if (colorsPresence[color] == 0) {
colorManager(color+1, vis, grid);
return;
}
int row = colorIndexMap[color].first;
int col = colorIndexMap[color].second;
solveThisColor(row, col, vis, grid,color,0);
}
int main() {
cin >> n >> m;
puzzle.resize(n, vector<int>(m, -1));
vector<vector<int>> vis(n, vector<int>(m, 0));
memset(colorsPresence, 0, sizeof(colorsPresence));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char color;
cin >> color;
if (color == '.') {
continue;
}
int colorCode = color - 'A';
colorsPresence[colorCode] = 1;
puzzle[i][j] = colorCode;
colorIndexMap[colorCode] = {i, j};
vis[i][j] = 2;
}
}
colorManager(0, vis, puzzle);
if (!solved) {
cout << "Not possible to solve the given puzzle :(\n";
} else {
cout << "\nYes it is possible to solve the puzzle as follows :)\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char color = puzzle[i][j] + 'A';
cout << color << " ";
}
cout << "\n";
}
}
return 0;
}