# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Define MAXSIZE 50
# Define ERROR-1
# Define OK 0
# Define FALSE 0
# Define TRUE 1
Typedef enum {RIGHT, DOWN, LEFT, UP} Direction;
Typedef enum {YES, NO} MarkTag;
Typedef struct position {
Int x;
Int y;
} Position;
Typedef struct {
Int order;
Position seat;
Direction di;
} SElemType;
Typedef struct {
SElemType * elem;
Int top;
} Stack;
Char maze [MAXSIZE + 2] [MAXSIZE + 2];
Int InitStack (Stack * S ){
S-> elem = (SElemType *) malloc (MAXSIZE * sizeof (SElemType ));
If (! S-> elem) return ERROR;
S-> top = 0;
Return OK;
}
Int Push (Stack * S, SElemType e ){
If (S-> top> = MAXSIZE * MAXSIZE) return ERROR;
S-> elem [S-> top ++] = e;
Return OK;
}
Int Pop (Stack * S, SElemType * e ){
If (S-> top <= 0) return ERROR;
* E = S-> elem [-- S-> top];
Return OK;
}
Int Empty (Stack S ){
If (S. top = 0) return TRUE;
Return FALSE;
}
Int createMaze (char * filename, Position * startpos, Position * endpos ){
FILE * fp;
Int I, j, rows, cols, temp;
Position start, end;
Fp = fopen (filename, "r ");
If (! Fp ){
Printf ("open file % s error! \ N ", filename );
Return ERROR;
}
If (! Feof (fp )){
Fscanf (fp, "% d", & rows, & cols );
Fscanf (fp, "% d", & start. x, & start. y );
Fscanf (fp, "% d", & end. x, & end. y );
}
For (I = 1; I <= rows; I ++ ){
For (j = 1; j <= cols; j ++ ){
Fscanf (fp, "% d", & temp );
Maze [I] [j] = 48 + temp;
}
}
Fclose (fp );
For (I = 0, j = 0; I <= rows + 1; I ++) maze [I] [j] = '1 ';
For (I = 0, j = cols + 1; I <= rows + 1; I ++) maze [I] [j] = '1 ';
For (I = 0, j = 0; j <= cols + 1; j ++) maze [I] [j] = '1 ';
For (I = rows + 1, j = 0; j <= cols + 1; j ++) maze [I] [j] = '1 ';
* Startpos = start;
* Endpos = end;
For (I = 0; I <= rows + 1; I ++)
{
For (j = 0; j <= cols + 1; j ++)
{
Printf ("% c", maze [I] [j]);
}
Printf ("\ n ");
}
Return OK;
}
Int canPass (Position curpos ){
If (maze [curpos. x] [curpos. y] = '0') return TRUE;
Return FALSE;
}
Void markPos (Position curpos, MarkTag tag ){
Switch (tag ){
Case YES: maze [curpos. x] [curpos. y] = '.'; break;
Case NO: maze [curpos. x] [curpos. y] = '#'; break;
}
}
Position nextPos (Position curpos, Direction dir ){
Position nextpos;
Switch (dir ){
Case RIGHT: nextpos. x = curpos. x; nextpos. y = curpos. y + 1; break;
Case DOWN: nextpos. x = curpos. x + 1; nextpos. y = curpos. y; break;
Case LEFT: nextpos. x = curpos. x; nextpos. y = curpos. Y-1; break;
Case UP: nextpos. x = curpos. X-1; nextpos. y = curpos. y; break;
}
Return nextpos;
}
Direction nextDir (Direction dir ){
Switch (dir ){
Case RIGHT: return DOWN;
Case DOWN: return LEFT;
Case LEFT: return UP;
}
}
Int Solve (Stack * S, Position start, Position end ){
Position curpos;
SElemType e;
Int curstep = 1;
If (InitStack (S) = ERROR) return FALSE;
Curpos = start;
Do {
If (canPass (curpos )){
MarkPos (curpos, YES );
E. order = curstep; e. seat = curpos; e. di = RIGHT;
Push (S, e );
If (curpos. x = end. x & curpos. y = end. y)
Return TRUE;
Curpos = nextPos (curpos, RIGHT );
Curstep ++;
}
Else
{
If (! Empty (* S )){
If (Pop (S, & e) = ERROR) return FALSE;
While (e. di = UP &&! Empty (* S )){
Curpos = e. seat; markPos (curpos, NO );
If (Pop (S, & e) = ERROR) return FALSE;
}
If (e. di! = UP ){
E. di = nextDir (e. di );
Push (S, e );
Curpos = nextPos (e. seat, e. di );
}
}
}
} While (! Empty (* S ));
Return FALSE;
}
Void main (void)
{
Position startPos, endPos;
Stack path;
SElemType e;
Char * fname = "in.txt ";
If (createMaze (fname, & startPos, & endPos) = ERROR) return;
Solve (& path, startPos, endPos );
While (! Empty (path )){
Pop (& path, & e );
Printf ("(% d, % d) \ n", e. seat. x, e. seat. y );
}
System ("pause ");
} In.txt 8 8
1 1 8 8
0 0 1 0 0 1 0
0 0 1 0 0 1 0
0 0 0 0 1 1 0 0
0 1 1 1 0 0 0
0 0 0 1 0 0 0
0 1 0 0 0 1 0
0 1 1 1 0 1 0
1 0 0 0 0 0 0 0