-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_validation.cpp
151 lines (115 loc) · 2.24 KB
/
maze_validation.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
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
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
struct cell {
char ch;
bool visit;
int r;
int c;
cell() {
visit = false;
}
};
bool check_maze_validity( cell maze[][21], cell start, cell end, int m, int n );
bool operator== ( cell a, cell b ) {
if ( a.r == b.r && a.c == b.c ) {
return true;
} else {
return false;
}
}
int main()
{
int t;
cin >> t;
vector<bool> sol;
for ( int k = 0; k < t; k++ ) {
int m, n;
cell start, end;
cin >> m;
cin >> n;
cell maze[m][21];
int ctr = 0;
for ( int i = 0; i < m; i++ ) {
for ( int j = 0; j <n; j++ ) {
cin >> maze[i][j].ch;
maze[i][j].r = i;
maze[i][j].c = j;
if ( ( i == 0 ) || ( i == m - 1 ) || ( j == 0 ) || ( j == n - 1) ) {
if ( maze[i][j].ch == '.' ) {
if ( ctr == 0 ) {
//maze[i][j].visit = true;
start = maze[i][j];
} else {
end = maze[i][j];
}
ctr++;
}
}
}
}
if ( ctr == 2 ) {
if ( check_maze_validity( maze, start, end, m, n ) ) {
sol.push_back(true);
} else {
sol.push_back(false);
}
} else {
sol.push_back(false);
}
}
for ( int i = 0; i < sol.size(); i++ ) {
if ( sol[i] ) {
cout << "valid"<< endl;
}else {
cout << "invalid"<< endl;
}
}
return 0;
}
bool check_maze_validity( cell maze[][21], cell start, cell end, int m, int n ) {
if ( start.ch == '#' ) {
return false;
}
if ( start.visit == true ) {
return false;
}
if ( start == end ) {
return true;
}
bool decn;
int row = start.r;
int col = start.c;
/* getchar();
cout << endl << "row " << row;
cout << "col " << col;
cout << endl;
*/
maze[row][col].visit = true;
if ( start.r != 0 ) {
decn = check_maze_validity( maze, maze[row - 1][col], end, m, n );
if ( decn ) {
return decn;
}
}
if ( start.c != n - 1 ) {
decn = check_maze_validity( maze, maze[row][col + 1], end, m, n );
if ( decn ) {
return decn;
}
}
if ( start.r != m - 1 ) {
decn = check_maze_validity( maze, maze[row + 1][col], end, m, n );
if ( decn ) {
return decn;
}
}
if ( start.c != 0 ) {
decn = check_maze_validity( maze, maze[row][col - 1], end, m, n );
if ( decn ) {
return decn;
}
}
return false;
}