summaryrefslogtreecommitdiff
path: root/alg_r.c
blob: 755fb6554d3f9405c0443289bb7f802871362fb1 (plain)
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
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

int main(int argv, char **argc) {
    if (argv < 3) {
        assert(argv > 0);
        printf("Usage: %s [n] [t]\n", argc[0]);
        return -1;
    }

    size_t n = atoi(argc[1]),
           t = atoi(argc[2]);

    assert(1 < t && t < n);

    size_t *c = calloc(t + 2, sizeof(*c));
    size_t j;

R1_initialize:
    for (size_t j = 1; j <= t; j++)
        c[j] = j - 1;
    c[t + 1] = n;

R2_visit:
    for (size_t k = t; k >= 1; k--)
        printf("%lu ", c[k]);
    printf("\n");

R3_maybe_easy_case:
    if ((t % 2) == 1) {
        if (c[1] + 1 < c[2]) {
            c[1] = c[1] + 1;
            goto R2_visit;
        }
        j = 2;
        goto R4_try_decrease_c_j;
    } else {
        if (c[1] > 0) {
            c[1] = c[1] - 1;
            goto R2_visit;
        } else {
            j = 2;
            goto R5_try_increase_c_j;
        }
    }

R4_try_decrease_c_j:
    if (c[j] >= j) {
        c[j] = c[j - 1];
        c[j - 1] = j - 2;
        goto R2_visit;
    }
    j = j + 1;

R5_try_increase_c_j:
    if (c[j] + 1 < c[j + 1]) {
        c[j - 1] = c[j];
        c[j] = c[j] + 1;
        goto R2_visit;
    } else {
        j = j + 1;
        if (j <= t) goto R4_try_decrease_c_j;
    }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback