// 7966044 VRS 3984 accepted 376 K 0 Ms GCC 2108b 13:38:57
// Poj 3984 second top embedded cup finals question B
// Search through the maze
// Compile with GCC without using STL. Depressed, only write queue operations by yourself
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define bool int
Int map [4] [2] = {1, 0}, {0, 1}, {-1}, {0,-1 }};
Char A [10] [10];
Int before [10] [10];
Bool State [10] [10];
Int outputstr [30] [2];
Int row, Col;
Typedef struct _ nodestru
{
Int Col;
Int row;
Struct _ nodestru * next;
Struct _ nodestru * last;
} Nodestru;
Nodestru tempall, start, end, * tempget;
Nodestru * head, * tail, * ptrtemp;
Void Init ()
{
Head = 0;
Tail = head;
}
Void push (nodestru K)
{
Nodestru * newnode = (nodestru *) malloc (sizeof (nodestru ));
If (Head! = 0)
Head-> last = newnode;
Newnode-> ROW = K. row;
Newnode-> Col = K. Col;
Newnode-> next = head;
Newnode-> last = 0;
Head = newnode;
If (Head-> next = 0)
Tail = newnode;
}
Bool isempty ()
{
If (Head = 0)
Return 1;
Return 0;
}
Nodestru * Front ()
{
Return tail;
}
Void POP ()
{
Nodestru * temp;
If (Head-> next = 0)
{
Temp = tail;
Head = tail = 0;
}
Else
{
Temp = tail;
Tail = tail-> last;
Tail-> next = 0;
}
Free (temp );
}
Void clear ()
{
Nodestru * temp;
While (Head-> next! = 0)
{
Temp = head;
Head = head-> next;
Free (temp );
}
Free (head );
Head = tail = 0;
}
int BFS ()
{< br> int I;
while (! Isempty ()
{< br> tempget = Front ();
tempall. row = tempget-> row;
tempall. col = tempget-> Col;
POP ();
If (tempall. row = 4 & tempall. col = 4)
return 1;
for (I = 0; I <4; I ++)
{< br> Col = tempall. col + map [I] [1];
ROW = tempall. row + map [I] [0];
If (row> = 0 & Row <5 & Col> = 0 & Col <5 & (State [row] [col] = 0) & (A [row] [col] = 0)
{< br> Start. row = row;
Start. col = Col;
before [row] [col] = I;
push (start);
State [row] [col] = 1;
}< BR >}< br>
}< br> return 0;
}
Int main ()
{
Int I, J, K;
Int temprow, tempcol, mapid;
For (I = 0; I <5; I ++)
For (j = 0; j <5; j ++)
Scanf ("% d", & A [I] [J]);
Memset (before, 0, sizeof (Before ));
Memset (State, 0, sizeof (State ));
Init ();
Start. Row = 0;
Start. Col = 0;
State [0] [0] = 1;
Push (start );
BFS ();
Clear ();
Temprow = 4;
Tempcol = 4;
K = 0;
While (temprow> = 0 & tempcol> = 0)
{
Outputstr [k] [0] = temprow;
Outputstr [k ++] [1] = tempcol;
Mapid = before [temprow] [tempcol];
Temprow-= map [mapid] [0];
Tempcol-= map [mapid] [1];
}
For (I = K-1; I> = 0; I --)
Printf ("(% d, % d) \ n", outputstr [I] [0], outputstr [I] [1]);
Return 0;
}