forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10401 Injured Queen Problem.cpp
69 lines (57 loc) · 1.51 KB
/
10401 Injured Queen Problem.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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const int GridSize = 16;
int N;
string status;
long long numPossible[GridSize][GridSize];
int visited[GridSize][GridSize];
long long GetNumPossible(int col, int previousRow, const int visit);
long long Calculate(int col, int previousRow, const int visit)
{
if (status[col] != '?')
{
int currentRow = (status[col] - '1');
if (currentRow > 10 || currentRow < 0)
currentRow = (status[col] - 'A' + 9); // A is 9
// Only allow if the difference is more than 1
if (abs(previousRow - currentRow) <= 1)
return 0;
return GetNumPossible(col + 1, currentRow, visit);
}
else
{
long long total = 0;
for (int i = 0; i < N; ++i)
{
if (abs(i - previousRow) <= 1)
continue;
total += GetNumPossible(col + 1, i, visit);
}
return total;
}
}
long long GetNumPossible(int col, int previousRow, const int visit)
{
if (col == N)
return 1;
if (visited[col][previousRow] != visit)
{
numPossible[col][previousRow] = Calculate(col, previousRow, visit);
visited[col][previousRow] = visit;
}
return numPossible[col][previousRow];
}
int main()
{
int T = 1;
while (cin >> status)
{
if (status.empty())
continue;
N = status.size();
cout << GetNumPossible(0, -3, T) << '\n';
++T;
}
}