/* Problem 1--Mobius Maze Anyone who has looked at old contest problems should know by now that I love maze problems. This is solved by BFS just as all mazes are; just some care has to be taken when crossing the east/west border. */ #include #include #include /* structure used to keep track of our path through the maze: dist is the distance to that position; r, c is that position; p is the polarity: 0 is normal, 1 is reversed; dir is the path to that position. */ typedef struct QStuff { int dist, r, c, p; char dir[101]; } QStuff; FILE *in, *out; int r, c, lr, lc, tr, tc, qb, qe; char Maze[10][10]; int V[10][10]; QStuff Q[100]; int main (int argc, char **argv); void Process (void); void AddQ (int newr, int newc, char dir, int p, int dist, char *ds); int main (int argc, char **argv) { int i, j; char ch; in = fopen ("prob1.in","r"); out = fopen ("prob1.out","w"); while (fscanf(in,"%d",&r),r > 0) { fscanf (in,"%d %d %d %d %d",&c,&lr,&lc,&tr,&tc); fscanf (in,"%c",&ch); /* read trailing eoln */ for (i=0; i < r; i++) { for (j=0; j < c; j++) fscanf (in,"%c",&Maze[i][j]); /*read maze char */ fscanf (in,"%c",&ch); /* read eoln */ } lr--;lc--;tr--;tc--; /* it's easier if we do 0--n-1 rather than 1-n */ Process (); } fclose (in); fclose (out); return EXIT_SUCCESS; } /* Processes one maze by using BFS, visited positions are put into a queue. The first path found must be the shortest path. */ void Process (void) { int i, j; QStuff q; qb = qe = 0; for (i=0; i < r; i++) for (j=0; j < c; j++) V[i][j] = 0; V[tr][tc] = 1; /* Put Thurston's square into the queue */ Q[0].dist = 0; Q[0].r = tr; Q[0].c = tc; Q[0].p = 0; Q[0].dir[0] = 0; while (1) { q = Q[qb++]; /* Attempt to move in all directions */ if (q.r==lr && q.c == lc) break; if (q.p?q.r>0:q.r0:q.r0?q.r:r-1-q.r,q.c>0?q.c-1:c-1,'W', q.c>0?q.p:!q.p,q.dist+1,q.dir); AddQ (q.c