我正在尝试使用右手规则解决迷宫,但我的回溯无法正常工作。逻辑是错误的,如果yPost
增加,则不遵循右手规则。谁会告诉我我的功能有什么问题?谢谢。这就是我对迷宫的看法:
public char[,] navigateMouse()
{
// check south
if (mouseMaze[yPos + 1, xPos] == ' ')
{
// keep track of the trail
// by marking it with '.'
mouseMaze[yPos, xPos] = '.';
mouseMaze[yPos + 1, xPos] = 'M';
yPos++;
}
// check east
else if (mouseMaze[yPos, xPos + 1] == ' ')
{
mouseMaze[yPos, xPos] = '.';
mouseMaze[yPos, xPos + 1] = 'M';
xPos++;
}
else if (mouseMaze[yPos - 1, xPos] == ' ')
{
mouseMaze[yPos, xPos] = '.';
mouseMaze[yPos - 1, xPos] = 'M';
yPos--;
}
else if (mouseMaze[yPos, xPos - 1] == ' ')
{
mouseMaze[yPos, xPos] = '.';
mouseMaze[yPos, xPos - 1] = 'M';
xPos--;
}
// backtrack
else
{
if (mouseMaze[yPos + 1, xPos] == '.')
{
mouseMaze[yPos + 1, xPos] = 'M';
yPos++;
}
else if (mouseMaze[yPos, xPos + 1] == '.')
{
mouseMaze[yPos, xPos + 1] = 'M';
xPos++;
}
else if (mouseMaze[yPos - 1, xPos] == '.')
{
mouseMaze[yPos - 1, xPos] = 'M';
yPos--;
}
else if (mouseMaze[yPos, xPos - 1] == '.')
{
mouseMaze[yPos, xPos - 1] = 'M';
xPos--;
}
}
if (mouseMaze[mazeHeight/2, mazeWidth - 1] == 'M')
gameWon = true;
return mouseMaze;
}
答案 0 :(得分:0)
你应该把行mouseMaze [ypos,xpos] ='。'在导航功能的开头,标记您当前的位置。当你回溯你没有标记你在哪里,你只是一遍又一遍地去那个地方