diff options
author | Matthew Sotoudeh <matthewsot@outlook.com> | 2022-10-07 12:46:02 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthewsot@outlook.com> | 2022-10-07 12:46:02 -0700 |
commit | cf87e5f67ae18767b933edb3b1371696f6b1c582 (patch) | |
tree | ad114627f109778a894b64c1c2bb3c996229072c /alg_c.c |
Diffstat (limited to 'alg_c.c')
-rw-r--r-- | alg_c.c | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -0,0 +1,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; +} |