-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASCIILabyrinth.cpp
137 lines (127 loc) · 3.65 KB
/
ASCIILabyrinth.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
ifstream fin("ASCIILabyrinth.in");
ofstream fout("ASCIILabyrinth.out");
bool g(int y, int x, int m, int n)
{
return y >= 0 && y <= m - 1 && x >= 0 && x <= n - 1;
}
int solve(int nowY, int nowX, char lastDirection, vector<vector<char>> & a, int m, int n, vector<vector<bool>> path)
{
if (lastDirection == '-')
{
path[nowY][nowX] = true;
return solve(nowY + 1, nowX, 'd', a, m, n, path) + solve(nowY, nowX + 1, 'r', a, m, n, path);
}
else if (g(nowY, nowX, m, n) == false || a[nowY][nowX] == 'b' || path[nowY][nowX] == true)
{
return 0;
}
else if (nowY == m - 1 && nowX == n - 1)
{
return 1;
}
else
{
path[nowY][nowX] = true;
if (a[nowY][nowX] == 'l')
{
if (lastDirection == 'u')
{
return solve(nowY - 1, nowX, 'u', a, m, n, path);
}
else if (lastDirection == 'd')
{
return solve(nowY + 1, nowX, 'd', a, m, n, path);
}
else if (lastDirection == 'l')
{
return solve(nowY, nowX - 1, 'l', a, m, n, path);
}
else
{
return solve(nowY, nowX + 1, 'r', a, m, n, path);
}
}
else
{
if (lastDirection == 'u' || lastDirection == 'd')
{
return solve(nowY, nowX - 1, 'l', a, m, n, path) + solve(nowY, nowX + 1, 'r', a, m, n, path);
}
else
{
return solve(nowY - 1, nowX, 'u', a, m, n, path) + solve(nowY + 1, nowX, 'd', a, m, n, path);
}
}
}
}
int main()
{
int testCase;
fin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
int m, n;
fin >> m >> n;
string in;
getline(fin, in);
vector<vector<char>> a(m, vector<char>(n));
vector<vector<bool>> path(m, vector<bool>(n, false));
for (int i = 0; i <= m - 1; ++i)
{
for (int j = 1; j <= 4; ++j)
{
getline(fin, in);
if (j == 3)
{
string temp = "";
int pos = 0;
for (int k = 1; k <= 4 * n; ++k)
{
if (in[k] == '|')
{
if (temp == " ")
{
a[i][pos] = 'b';
}
else if (temp == "***")
{
a[i][pos] = 'l';
}
else
{
a[i][pos] = 'c';
}
temp = "";
++pos;
}
else
{
temp += in[k];
}
}
}
}
}
getline(fin, in);
if (m == 1 && n == 1)
{
fout << "Number of solutions: 2\n";
}
else
{
fout << "Number of solutions: " << solve(0, 0, '-', a, m, n, path) << '\n';
}
}
return 0;
}