bool FindPath(Position start, Position finish, int& PathLen, Position *& path) {
if (start.row == finish.row && start.col == finish.col) {
PathLen = 0;
return true;
}
int NumOfNbrs = 4, i, j;
for (i = 0; i <= m + 1; i ++) {
grid[0][i] = grid[m+1][i] = 1;
grid[i][0] = grid[i][m+1] = 1;
}
Position offset[4];
offsets[0].row = 0;
offsets[0].col = 1;
offsets[1].row = 1;
offsets[1].col = 0;
offsets[2].row = 0;
offsets[2].col = -1;
offsets[3].row = -1;
offsets[3].col = 0;
Position here, nbr;
here.row = start.row;
here.col = start.col;
grid[start.row][start.col] = 2;
LinkedQueue <Position> Q;
do {
for (i = 0; i < NumOfNbrs; i ++) {
nbr.row = here.row + offsets[i].row;
nbr.col = here.row + offsets[i].col;
if (grid[nbr.row][nbr.col] == 0) {
grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
if (nbr.row == finish.row && nbr.col == finish.col) {
break;
}
Q.EnQueue(nbr);
}
}
if (nbr.row == finish.row && nbr.col == finish.col) {
break;
}
if (Q.IsEmpty() == true) {
return false;
}
Q.DeQueue(here);
} while (true);
PathLen grid[finish.row][finish.col] - 2;
path = new Position[PathLen];
here = finish;
for (j = PathLen - 1; j >= 0; j --) {
path[j] = here;
for (i = 0; i < NumOfNbrs; i ++) {
nbr.row = here.row + offsets[i].row;
nbr.col = here.col + offsets[i].col;
if (grid[nbr.row][nbr.col] == j + 2) {
break;
}
}
here = nbr;
}
return true;
};