USACO Shuttle Puzzle

Code

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
/*
ID: wcr19961
PROG: shuttle
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

const int maxn = 10;
int n, cnt;

void print(int x) {
printf("%d%c",x, ++cnt % 20 == 0 ? '\n' : ' ');
}

int main() {
freopen("shuttle.in", "r", stdin);
freopen("shuttle.out", "w", stdout);
int n, cnt = 0;
scanf("%d", &n);
int p = n, sig = 1, sum = n * (n + 2);
print(p);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
print(p += 2 * sig);
}
print(p += sig);
sig = -sig;
}
for (int i = 0; i < n; ++i) {
print(p += 2 * sig);
}
sig = -sig;
for (int i = n - 1; i > 0; --i) {
print(p += sig);
for (int j = 0; j < i; ++j) {
print(p += 2 * sig);
}
sig = -sig;
}
printf("%d\n", --p);
return 0;
}