-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path2020-1A-pascal-walk-TLE.cpp
73 lines (60 loc) · 1.52 KB
/
2020-1A-pascal-walk-TLE.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
/*
graphs > traversal
difficulty: medium
date: 11/Apr/2020
by: @brpapa
*/
#include <bits/stdc++.h>
using namespace std;
#define MAX_S 505
int C[MAX_S][MAX_S];
bool foundSolution;
map<pair<int, int>, bool> visited;
map<pair<int,int>, pair<int,int>> parent;
void walk(int r, int k, int n) {
// posicao atual (r, k), soma N-n atual
if (foundSolution) return;
if (n == 0) {
foundSolution = true;
vector<pair<int, int>> ans;
pair<int, int> p = parent[make_pair(r, k)];
while (p != make_pair(-1, -1)) {
ans.push_back(p);
p = parent[p];
}
reverse(ans.begin(), ans.end());
for (auto a : ans)
cout << a.first << " " << a.second << endl;
}
if (r < 1 || k < 1 || k > r || n < 0) return;
visited[make_pair(r, k)] = true;
int dr[6] = {-1, -1, 0, 0, 1, 1};
int dk[6] = {-1, 0, -1, 1, 0, 1};
for (int d = 0; d < 6; d++) {
int nr = r + dr[d];
int nk = k + dk[d];
if (!visited[make_pair(nr, nk)]) {
parent[make_pair(nr, nk)] = make_pair(r, k);
walk(nr, nk, n-C[r][k]);
}
}
}
int main() {
for (int r = 1; r < MAX_S; r++) {
C[r][1] = 1;
C[r][r] = 1;
for (int k = 2; k < r; k++)
C[r][k] = C[r-1][k-1] + C[r-1][k];
}
int T, t = 1; cin >> T;
while (T--) {
int N; cin >> N;
foundSolution = false;
parent.clear();
visited.clear();
printf("Case #%d:\n", t++);
parent[make_pair(1, 1)] = make_pair(-1, -1);
walk(1, 1, N);
}
return 0;
}