summaryrefslogtreecommitdiff
path: root/alg_c.c
blob: fd8b7d3a90ce657739f558e6e729aa638caf414c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#define MAX(A, B) (((A) > (B)) ? (A) : (B))

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

    long n = atoi(argc[1]),
         t = atoi(argc[2]),
         s = n - t;

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

    char *a = calloc(n, sizeof(*a)),
         *w = calloc(n + 1, sizeof(*a));

C1_initialize:
    for (size_t j = 0; j < s; j++)
        a[j] = 0;
    for (size_t j = s; j < n; j++)
        a[j] = 1;
    for (size_t j = 0; j <= n; j++)
        w[j] = 1;
    size_t r = (s > 0) ? s : t;

C2_visit:
    for (size_t k = n; k --> 0;)
        printf("%lu", a[k]);
    printf("\n");

C3_find_j_and_branch:
    size_t j = r;
    while (w[j] == 0) {
        w[j] = 1;
        j = j + 1;
    }
    if (j == n) return 0;
    w[j] = 0;
    if (j % 2 == 1 && a[j] != 0) goto C4_move_right_one;
    if (j % 2 == 0 && a[j] != 0) goto C5_move_right_two;
    if (j % 2 == 0 && a[j] == 0) goto C6_move_left_one;
    if (j % 2 == 1 && a[j] == 0) goto C7_move_left_two;

C4_move_right_one:
    a[j - 1] = 1;
    a[j] = 0;
    if (r == j && j > 1)    r = j - 1;
    else if (r == j - 1)    r = j;
    goto C2_visit;

C5_move_right_two:
    if (a[j - 2] != 0) goto C4_move_right_one;
    a[j - 2] = 1;
    a[j] = 0;
    if (r == j)             r = MAX(j - 2, 1);
    else if (r == j - 2)    r = j - 1;
    goto C2_visit;

C6_move_left_one:
    a[j] = 1;
    a[j - 1] = 0;
    if (r == j && j > 1)    r = j - 1;
    else if (r == j - 1)    r = j;
    goto C2_visit;

C7_move_left_two:
    if (a[j - 1] != 0) goto C6_move_left_one;
    a[j] = 1;
    a[j - 2] = 0;
    if (r == j - 2)         r = j;
    else if (r == j - 1)    r = j - 2;
    goto C2_visit;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback