Go to diff to previous submission
#include <stdlib.h> #include <stdio.h> #define NOT_VISITED -1 int R, C, GR, GC, LR, LC; /* R, C ... area size */ int area[102][102]; /* R-major */ typedef struct {int r, c;} point; typedef struct {point list[102*102]; int len;} data; data aa, bb; int in_grid(point x) { int rv = (x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C); /*printf("%d,%d = %d (R=%d, C=%d)\n", x.r, x.c, rv, R, C);*/ return rv; } int main(void) { int i, step, row, col, j; point *p; int *len_ptr; point dest[8]; int flag; data *a, *b, *tmp; a = &aa; b = &bb; for (i = 0; i < 102*102; ++i) ((int*)(area))[i] = NOT_VISITED; if (GR == LR && GC == LC) { continue; } area[GR][GC] = 0; a->len = 0; b->len = 0; a->list[0].r = GR; a->list[0].c = GC; a->len = 1; for (step = 1; ; ++step) { b->len = 0; flag = 0; for (i = 0; i < a->len; ++i) { row = a->list[i].r; col = a->list[i].c; /* dest = { {row+1, col+2}, {row+1, col-2}, {row-1, col+2}, {row-1, col-2}, {row+2, col+1}, {row+2, col-1}, {row-2, col+1}, {row-2, col-1} }; */ dest[0].r = row+1; dest[0].c = col+2; dest[1].r = row-1; dest[1].c = col+2; dest[2].r = row+1; dest[2].c = col-2; dest[3].r = row-1; dest[3].c = col-2; dest[4].r = row+2; dest[4].c = col+1; dest[5].r = row-2; dest[5].c = col+1; dest[6].r = row+2; dest[6].c = col-1; dest[7].r = row-2; dest[7].c = col-1; for (j = 0; j < 8; ++j) { if (in_grid(dest[j])) { if (area[ dest[j].r ][ dest[j].c ] == NOT_VISITED) { /*printf("setting flag\n");*/ area[ dest[j].r ][ dest[j].c ] = step; flag = 1; b->list[ b->len++ ] = dest[j]; /*printf("r=%d, c=%d, LR=%d, LC=%d\n", dest[j].r, dest[j].c, LR, LC);*/ if (dest[j].r == LR && dest[j].c == LC) { goto endloop; } } } } } if (flag == 0) { goto endloop; } tmp = b; b = a; a = tmp; } endloop: ; } return 0; }
--- c4.s1104.cteam007.grasshop.c.0.grasshop.c +++ c4.s1157.cteam007.grasshop.c.0.grasshop.c @@ -8,5 +8,5 @@ int area[102][102]; /* R-major */ -typedef struct {unsigned char r, c;} point; +typedef struct {int r, c;} point; typedef struct {point list[102*102]; int len;} data; @@ -15,5 +15,7 @@ int in_grid(point x) { - return x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C; + int rv = (x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C); + /*printf("%d,%d = %d (R=%d, C=%d)\n", x.r, x.c, rv, R, C);*/ + return rv; } @@ -32,4 +34,10 @@ for (i = 0; i < 102*102; ++i) ((int*)(area))[i] = NOT_VISITED; + + if (GR == LR && GC == LC) { + printf("0\n"); + continue; + } + area[GR][GC] = 0; @@ -80,9 +88,12 @@ for (j = 0; j < 8; ++j) { - if (in_grid(dest[j])) { + if (in_grid(dest[j])) + { if (area[ dest[j].r ][ dest[j].c ] == NOT_VISITED) { + /*printf("setting flag\n");*/ area[ dest[j].r ][ dest[j].c ] = step; flag = 1; b->list[ b->len++ ] = dest[j]; + /*printf("r=%d, c=%d, LR=%d, LC=%d\n", dest[j].r, dest[j].c, LR, LC);*/ if (dest[j].r == LR && dest[j].c == LC) { printf("%d\n", step);