C++迷宫路线问题

来源:百度知道 编辑:UC知道 时间:2024/06/19 08:01:06
#include <iostream>
#include <string>
using namespace std;
bool mousAndCheese::displayPath (int robot_x , int robot_y)
{
if (mazeArray[robot_x][robot_y] == mazeArray[cheese_x][cheese_y])//Base case
{
return true;
}
if (mazeArray[robot_x][robot_y] != ' ') //Base case
{
return false;
}
mazeArray[robot_x][robot_y] = '+';
if (displayPath(robot_x +1,robot_y) == true)
{
return true;
}
if (displayPath(robot_x -1,robot_y) == true)
{
return true;
}
if (displayPath(robot_x,robot_y +1) == true)
{
return true;
}
if (displayPath(robot_x,robot_y -1) == true)
{
return true;
}
mazeArray[robot_x][robot_y] = 'X';
return false;
}
void mousAndCheese::findPosition ()
{
for (int y = 0;y<8;y++)
{
for (int x = 0;x<16;x++)
{

if (displayPath(robot_x +1,robot_y) == true)
{
return true;
}
if (displayPath(robot_x -1,robot_y) == true)
{
return true;
}
if (displayPath(robot_x,robot_y +1) == true)
{
return true;
}
if (displayPath(robot_x,robot_y -1) == true)
{
return true;
}
你应该四个方向都判断好后在返回 不然你只能判断一个方向 其他方向就不会判断
int flag=0;
if (displayPath(robot_x +1,robot_y) == true)
{
flag=1;
}
if (displayPath(robot_x -1,robot_y) == true)
{
flag=1;
}
if (displayPath(robot_x,robot_y +1) == true)
{
flag=1;
}
if (displayPath(robot_x,robot_y -1) == true)
{
flag=1;
}
if(flag==1) return true;

有什么问题 hi上探讨