-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMAKEMAZE.cpp
99 lines (99 loc) · 1.78 KB
/
MAKEMAZE.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
#include "bits/stdc++.h"
using namespace std;
int r, c;
char arr[22][22];
bool f[22][22];
bool visit[22][22];
const int dx[] = { 0,0,1,-1 };
const int dy[] = { 1,-1,0,0 };
vector<pair<int, int>>s;
bool isSafe(pair<int, int>s)
{
return (s.first >= 0) && (s.first < r) && (s.second >= 0) && (s.second < c);
}
bool bfs(vector<pair<int, int>>s)
{
visit[s[0].first][s[0].second] = 1;
queue<pair<int, int> >q;
pair<int, int>temp;
q.push(s[0]);
while (!q.empty())
{
temp = q.front();
if (s[1].first == temp.first&&s[1].second == temp.second)return true;
q.pop();
for (int k = 0; k < 4; k++)
{
pair<int, int>t1;
t1.first = dx[k] + temp.first;
t1.second = dy[k] + temp.second;
if (isSafe(t1) && arr[t1.first][t1.second] == '.'&&!visit[t1.first][t1.second])
{
visit[t1.first][t1.second] = 1;
q.push(t1);
}
}
}
return false;
}
int main()
{
//READ;
int t;
scanf("%d", &t);
while (t--)
{
s.clear();
memset(visit, 0, sizeof visit);
memset(f, 0, sizeof f);
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++)
{
scanf("%s", arr[i]);
}
int ck = 0;
for (int i = 0; i < c; i++)
{
if (arr[0][i] == '.'&&!f[0][i])
{
ck++;
f[0][i] = 1;
s.push_back(make_pair(0, i));
}
}
for (int i = 0; i < c; i++)
{
if (arr[r-1][i] == '.'&&!f[r-1][i])
{
ck++;
f[r-1][i] = 1;
s.push_back(make_pair(r - 1, i));
}
}
for (int i = 0; i < r; i++)
{
if (arr[i][0] == '.'&&!f[i][0])
{
ck++;
f[i][0] = 1;
s.push_back(make_pair(i, 0));
}
}
for (int i = 0; i < r; i++)
{
if (arr[i][c-1] == '.'&&!f[i][c-1])
{
ck++;
f[c-1][i] = 1;
s.push_back(make_pair(i, c - 1));
}
}
if (ck != 2)printf("invalid\n");
else
{
if (bfs(s))printf("valid\n");
else printf("invalid\n");
}
}
return 0;
}