}
}
void PrintWay() //输出运动路径
{
int i, j;
int count = 0;
for (i=0; i
for (j=0; j
if (i==0 || i==M+1)
printf("%02d", j);
else if (j==0 || j==N+1)
printf("%02d", i);
else if (ROAD == c_map[i][j])
{
count++;
printf("%c%c", 2, 2);
}
else if (PASSED == c_map[i][j])
printf("%c%c", 3, 3);
else if (OPEN == c_map[i][j])
printf(" ");
else
printf("##");
}
printf("\n");
}
printf("共%d步\n", count);
}
int ExtentSearchWay()//寻找路径:广度搜索
{
int i, j, x, y;
int front = 0; //队首
int rear = 0; //队尾
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (front<=rear)
{
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
x = way[front].x + zx[i];
y = way[front].y + zy[i];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入队列
{
rear++;
way[rear].x = x;
way[rear].y = y;
way[rear].pre = front;
c_map[x][y] = PASSED;
}
if (IsEnd(x, y)) //找到出口
{
PutQueue(rear); //把队列路径显示到迷宫中
return true;
}
}
front++;
}
return false;
}
void PutQueue(int rear) //把队列路径显示到迷宫中
{
int i = rear;
CopyMiGong();
do
{
c_map[way[i].x][way[i].y] = ROAD;
i = way[i].pre;
} while (!IsBegin(way[i].x, way[i].y));
c_map[begin[0]][begin[1]] = ROAD;
}
int DeepSearchWay()//寻找路径:深度搜索
CopyMiGong();
if (c_map[begin[0]][begin[1]] == OPEN && Search(begin[0], begin[1]))
{
c_map[begin[0]][begin[1]] = ROAD;
return true;
}
return false;
}
int Search(int x, int y)//深度搜索递归子函数
{
int i;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
c_map[x][y] = ROAD;
return true;
}
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
if (c_map[x+zx[i]][y+zy[i]] == OPEN && Search(x+zx[i], y+zy[i]))
{
c_map[x][y] = ROAD;
return true;
}
}
return false;
}
int DeepSearchWay_2()//寻找路径:深度搜索
{
int x, y;
int top = 0; //栈顶指针
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
way[0].pre = 0;
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (top >= 0)
{
if (way[top].pre < 4)
{
x = way[top].x + zx[way[top].pre];
y = way[top].y + zy[way[top].pre];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入栈
{
top++;
way[top].x = x;
way[top].y = y;
way[top].pre = 0;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
PutStack(top); //把栈路径显示到迷宫中
return true;
}
}
else
way[top].pre++;
}
else
top--;
}
return false;
}
void PutStack(int top) //把栈路径显示到迷宫中
{
CopyMiGong();
while (top >= 0)
{
c_map[way[top].x][way[top].y] = ROAD;
top--;
}
}