-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_dominoes.cpp
74 lines (55 loc) · 1.22 KB
/
n_dominoes.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
#include <fstream>
#include <iostream>
using namespace std;
int const maxSize = 51;
int field[maxSize][maxSize];
int w = 5;
int h = 5;
void clear() {
for (int i = 0; i < maxSize; i++) {
for (int j = 0; j < maxSize; j++) {
field[i][j] = 0;
}
}
}
long long dominoesLayoutNumber(int x, int y) {
if (w * h % 2) {
return 0;
}
if (x == (w - 1) && y == (h - 1)) {
return field[x][y];
}
if (x >= w) {
return dominoesLayoutNumber(0, y + 1);
}
if (field[x][y]) {
// next domino
return dominoesLayoutNumber(x + 1, y);
}
long long result = 0;
//trying vertical domino
if ((y + 1 < h) && (field[x][y + 1] == 0)) {
field[x][y] = field[x][y + 1] = 1;
result += dominoesLayoutNumber(x + 1, y);
field[x][y] = field[x][y + 1] = 0;
}
//trying horizontal domino
if ((x + 1 < w) && (field[x + 1][y] == 0)) {
field[x][y] = field[x + 1][y] = 1;
result += dominoesLayoutNumber(x + 1, y);
field[x][y] = field[x + 1][y] = 0;
}
return result;
}
int main() {
int const maxWH = 50;
for (int i = 1; i <= maxWH; i++) {
for (int j = 1; j * i <= maxWH; j++) {
w = i;
h = j;
cout << "result[" << i << "][" << j << "]"
<< " = " << dominoesLayoutNumber(0, 0) << ";" << endl;
clear();
}
}
}