/* Output from p2c, the Pascal-to-C translator */ /* From input file "plain1.pas" */ #include /* straight counting algorithme, plain + optimizing on double ends. CONST trace = 'showing current board or not' */ #define trace true #define size 30 #define sqsize 900 #define sqsizem1 899 #define left (-1) #define right 1 #define down 30 #define up (-30) typedef boolean t_field[sqsizem1 + 1]; Static t_field field, border; Static uchar neighbour[sqsizem1 + 1]; /* path : ARRAY[0..sqsize] OF char; */ Static long gn, gm, maxdepth, count, total, depth; Static long depth_count[sqsize]; Static FILE *result_file; Static Void initialize_field(n, m) uchar n, m; { unsigned short i, j, c; for (i = 0; i <= sqsizem1; i++) { field[i] = false; neighbour[i] = 0; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { field[i + size * j] = true; c = 4; if (i == 1) c--; if (i == n) c--; if (j == 1) c--; if (j == m) c--; neighbour[i + size * j] = c; } } /* FOR i := 0 TO sqsizem1 DO path[i] := ' '; */ for (i = 0; i <= sqsizem1; i++) border[i] = false; for (i = 1; i <= n; i++) { border[size + i] = true; border[size * m + i] = true; } for (i = 1; i <= m; i++) { border[i * size + 1] = true; border[n + i * size] = true; } } Static Void show_depth_count() { uchar i1, j1, FORLIM, FORLIM1; putchar('\n'); FORLIM = gn; for (i1 = 0; i1 < FORLIM; i1++) { FORLIM1 = gm; for (j1 = 1; j1 <= FORLIM1; j1++) printf("%8ld", depth_count[i1 + size * j1]); putchar('\n'); } printf("%12ld", count); } Static boolean no_border, bow; Static Void seek PP((int pos, long dir)); /* Local variables for seek: */ struct LOC_seek { long dir, left_turn, right_turn; } ; Local Void check2(left_pos, left_dir, right_pos, right_dir, LINK) long left_pos, left_dir, right_pos, right_dir; struct LOC_seek *LINK; { /* gotoxy(1,21); clreol; write('check2 ',left_pos:3, left_dir:3, right_pos:3, right_dir : 3); */ neighbour[right_pos]--; neighbour[left_pos]--; if (bow) { if (neighbour[left_pos] == 1) { if (neighbour[right_pos] != 1) /* blocking */ seek((int)left_pos, left_dir); } else { seek((int)right_pos, right_dir); if (neighbour[right_pos] > 1) seek((int)left_pos, left_dir); } } else { bow = (neighbour[left_pos] == 1); seek((int)right_pos, right_dir); bow = (neighbour[right_pos] == 1); seek((int)left_pos, left_dir); bow = false; } neighbour[right_pos]++; neighbour[left_pos]++; } Local Void check3(pos_left_turn, pos_dir, pos_right_turn, LINK) unsigned short pos_left_turn, pos_dir, pos_right_turn; struct LOC_seek *LINK; { /* gotoxy(1,21); clreol; write('check3 ',pos_left_turn:3, pos_dir:3, pos_right_turn:3); */ neighbour[pos_right_turn]--; neighbour[pos_dir]--; neighbour[pos_left_turn]--; if (bow) { if (neighbour[pos_left_turn] == 1) { if (neighbour[pos_right_turn] != 1) { /* blocking */ seek(pos_dir, LINK->dir); seek(pos_left_turn, LINK->left_turn); } } else { seek(pos_right_turn, LINK->right_turn); if (neighbour[pos_right_turn] > 1) { seek(pos_dir, LINK->dir); seek(pos_left_turn, LINK->left_turn); } } } else if (neighbour[pos_left_turn] == 1) { if (neighbour[pos_right_turn] == 1) { bow = true; seek(pos_right_turn, LINK->right_turn); seek(pos_left_turn, LINK->left_turn); bow = false; } else { bow = true; seek(pos_right_turn, LINK->right_turn); seek(pos_dir, LINK->dir); bow = false; seek(pos_left_turn, LINK->left_turn); } } else if (neighbour[pos_right_turn] == 1) { seek(pos_right_turn, LINK->right_turn); bow = true; seek(pos_dir, LINK->dir); seek(pos_left_turn, LINK->left_turn); bow = false; } else { seek(pos_right_turn, LINK->right_turn); seek(pos_dir, LINK->dir); seek(pos_left_turn, LINK->left_turn); } neighbour[pos_right_turn]++; neighbour[pos_dir]++; neighbour[pos_left_turn]++; } Static Void seek(pos, dir_) unsigned short pos; long dir_; { struct LOC_seek V; Char ch; V.dir = dir_; if (depth == maxdepth) { count++; depth_count[pos - 1]++; return; } depth++; field[pos] = false; switch (V.dir) { case right: /* path[pos-dir] := '>'; */ V.left_turn = up; V.right_turn = down; break; case left: /* path[pos-dir] := '<'; */ V.left_turn = down; V.right_turn = up; break; case down: /* path[pos-dir] := 'v'; */ V.left_turn = right; V.right_turn = left; break; case up: /* path[pos-dir] := '^'; */ V.left_turn = left; V.right_turn = right; break; } if (no_border && border[pos]) { no_border = false; if (field[pos + right]) neighbour[pos + right]--; if (field[pos + left]) neighbour[pos + left]--; if (field[pos + down]) neighbour[pos + down]--; if (field[pos + up]) neighbour[pos + up]--; if (field[pos + down]) seek(pos + down, (long)down); if (field[pos + left]) seek(pos + left, (long)left); if (field[pos + right]) seek(pos + right, (long)right); if (field[pos + up]) seek(pos + up, (long)up); if (field[pos + right]) neighbour[pos + right]++; if (field[pos + left]) neighbour[pos + left]++; if (field[pos + down]) neighbour[pos + down]++; if (field[pos + up]) neighbour[pos + up]++; no_border = true; } else { if (field[pos + V.dir]) { if (field[pos + V.right_turn]) { if (field[pos + V.left_turn]) { if (field[pos + V.right_turn + V.dir] && field[pos + V.left_turn + V.dir]) check3((int)(pos + V.left_turn), (int)(pos + V.dir), (int)(pos + V.right_turn), &V); } else if (field[pos + V.right_turn + V.dir]) check2(pos + V.right_turn, V.right_turn, pos + V.dir, V.dir, &V); } else if (field[pos + V.left_turn]) { if (field[pos + V.left_turn + V.dir]) check2(pos + V.left_turn, V.left_turn, pos + V.dir, V.dir, &V); } else seek((int)(pos + V.dir), V.dir); } else if (field[pos + V.right_turn]) { if (!field[pos + V.left_turn]) /* blokking straight */ seek((int)(pos + V.right_turn), V.right_turn); } else if (field[pos + V.left_turn]) seek((int)(pos + V.left_turn), V.left_turn); } field[pos] = true; /* path[pos-dir] := ' ';*/ depth--; /* blocking right/left ahead */ /* blokking right ahead */ /* blokking left ahead */ /* dead end */ } Static Void init_depth_count() { unsigned short i; for (i = 0; i < sqsize; i++) depth_count[i] = 0; } Static Void print_depth_count(n, m, i, j) uchar n, m, i, j; { uchar i1, j1, c; if (count <= 0) return; if (n == m) { if (i == j) { if (i * 2 == n + 1) c = 1; else c = 4; } else if (j * 2 == m + 1) c = 4; else c = 8; } else if (i * 2 == n + 1) { if (j * 2 == m + 1) c = 1; else c = 2; } else if (j * 2 == m + 1) c = 2; else c = 4; fprintf(result_file, "%4d%4d%4d%4d%12ld * %12d = %12ld\n", n, m, i, j, count, c, count * c); total += count * c; for (i1 = 0; i1 < n; i1++) { for (j1 = 1; j1 <= m; j1++) fprintf(result_file, "%8ld", depth_count[i1 + size * j1]); putc('\n', result_file); } putc('\n', result_file); } Static Void seek_from_pos(n, m, i, j) uchar n, m, i, j; { unsigned short pos; gn = n; gm = m; printf("seek from %3d%3d%3d%3d\n", n, m, i, j); count = 0; no_border = border[pos]; init_depth_count(); pos = i + size * j; no_border = !border[pos]; field[pos] = false; bow = false; if (field[pos + right]) neighbour[pos + right]--; if (field[pos + left]) neighbour[pos + left]--; if (field[pos + down]) neighbour[pos + down]--; if (field[pos + up]) neighbour[pos + up]--; if (i < n) seek(pos + right, (long)right); if (j < m) seek(pos + down, (long)down); if (i > 1) seek(pos + left, (long)left); if (j > 1) seek(pos + up, (long)up); field[pos] = true; if (field[pos + right]) neighbour[pos + right]++; if (field[pos + left]) neighbour[pos + left]++; if (field[pos + down]) neighbour[pos + down]++; if (field[pos + up]) neighbour[pos + up]++; print_depth_count(n, m, i, j); } Static Void count_for(n, m, si, sj) uchar n, m, si, sj; { uchar i, j, tn, tm; boolean not_both_odd; total = 0; maxdepth = n * m; initialize_field(n, m); count = 0; depth = 2; not_both_odd = !((n & 1) && (m & 1)); tn = (n + 1) / 2; tm = (m + 1) / 2; if (n == m) { for (i = 1; i <= tn; i++) { for (j = i; j <= tm; j++) { if ((i > si || i == si && j >= sj) && (not_both_odd || !((i + j) & 1))) seek_from_pos(n, m, i, j); } } } else { for (i = 1; i <= tn; i++) { for (j = 1; j <= tm; j++) { if (i > si || i == si && j >= sj) seek_from_pos(n, m, i, j); } } } fprintf(result_file, "total =%12ld\n\n\n", total / 2); } main(argc, argv) int argc; Char *argv[]; { long i; PASCAL_MAIN(argc, argv); result_file = NULL; /* p2c: plain1.pas, line 395: Warning: * Don't know how to ASSIGN to a non-explicit file variable [207] */ result_file = fopen("result.dat", "w"); /* p2c: plain1.pas, line 396: Note: REWRITE does not specify a name [181] */ if (result_file != NULL) rewind(result_file); else result_file = tmpfile(); if (result_file == NULL) _EscIO(FileNotFound); count_for(7, 7, 1, 1); return 1; count_for(4, 11, 1, 1); count_for(4, 12, 1, 1); count_for(4, 13, 1, 1); count_for(4, 15, 1, 1); count_for(4, 16, 1, 1); count_for(4, 17, 1, 1); count_for(4, 18, 1, 1); count_for(6, 9, 1, 1); /*54*/ count_for(7, 8, 1, 1); /*56*/ count_for(6, 10, 1, 1); /*60*/ count_for(7, 9, 1, 1); /*63*/ count_for(8, 8, 1, 1); /*64*/ count_for(7, 10, 1, 1); /*70*/ count_for(8, 9, 1, 1); /*72*/ count_for(8, 10, 1, 1); /*80*/ if (result_file != NULL) fclose(result_file); result_file = NULL; if (result_file != NULL) fclose(result_file); exit(EXIT_SUCCESS); } /* End. */