我想在运行程序时自动解决迷宫问题。
一开始我的迷宫就是这样。
1 0 0 0
0 0 1 0
0 1 1 0
0 1 1 0
最后应该看起来像这样:
0 1 1 1
1 1 0 1
1 0 0 1
1 0 0 1
我有一个3维的数组(用于行,列和侧面)。
侧面可以在(0),右(1),上方(2)和左(3)处。因此,我检查每个单元是否有墙。如果是,我在该单元格中输入是。
mazeTab[0, 0, 0] = 0;
mazeTab[0, 0, 1] = 1;
mazeTab[0, 0, 2] = 1;
mazeTab[0, 0, 3] = 1;
mazeTab[1, 0, 0] = 0;
mazeTab[1, 0, 1] = 0;
mazeTab[1, 0, 2] = 1;
mazeTab[1, 0, 3] = 1;
mazeTab[2, 0, 0] = 0;
mazeTab[2, 0, 1] = 0;
mazeTab[2, 0, 2] = 1;
mazeTab[2, 0, 3] = 0;
mazeTab[3, 0, 0] = 0;
mazeTab[3, 0, 1] = 1;
mazeTab[3, 0, 2] = 1;
mazeTab[3, 0, 3] = 0;
//=================================
mazeTab[0, 1, 0] = 0;
mazeTab[0, 1, 1] = 0;
mazeTab[0, 1, 2] = 1;
mazeTab[0, 1, 3] = 1;
mazeTab[1, 1, 0] = 1;
mazeTab[1, 1, 1] = 1;
mazeTab[1, 1, 2] = 0;
mazeTab[1, 1, 3] = 0;
mazeTab[2, 1, 0] = 1;
mazeTab[2, 1, 1] = 1;
mazeTab[2, 1, 2] = 1;
mazeTab[2, 1, 3] = 1;
mazeTab[3, 1, 0] = 0;
mazeTab[3, 1, 1] = 1;
mazeTab[3, 1, 2] = 0;
mazeTab[3, 1, 3] = 1;
//===================================
mazeTab[0, 2, 0] = 0;
mazeTab[0, 2, 1] = 1;
mazeTab[0, 2, 2] = 0;
mazeTab[0, 2, 3] = 1;
mazeTab[1, 2, 0] = 1;
mazeTab[1, 2, 1] = 1;
mazeTab[1, 2, 2] = 1;
mazeTab[1, 2, 3] = 1;
mazeTab[2, 2, 0] = 1;
mazeTab[2, 2, 1] = 1;
mazeTab[2, 2, 2] = 1;
mazeTab[2, 2, 3] = 1;
mazeTab[3, 2, 0] = 0;
mazeTab[3, 2, 1] = 1;
mazeTab[3, 2, 2] = 0;
mazeTab[3, 2, 3] = 1;
//===================================
mazeTab[0, 3, 0] = 1;
mazeTab[0, 3, 1] = 1;
mazeTab[0, 3, 2] = 0;
mazeTab[0, 3, 3] = 1;
mazeTab[1, 3, 0] = 1;
mazeTab[1, 3, 1] = 1;
mazeTab[1, 3, 2] = 1;
mazeTab[1, 3, 3] = 1;
mazeTab[2, 3, 0] = 1;
mazeTab[2, 3, 1] = 1;
mazeTab[2, 3, 2] = 1;
mazeTab[2, 3, 3] = 1;
mazeTab[3, 3, 0] = 0;
mazeTab[3, 3, 1] = 1;
mazeTab[3, 3, 2] = 0;
mazeTab[3, 3, 3] = 1;
然后我使用一种方法来解决整个迷宫,但是我很确定某些地方出了问题。使用它时,我检查是否有下一个单元格,例如:如果我有一个0且我可以走到上方,我检查上方的单元格是否也可以转到下方的单元格,以验证我们是否没有墙在那里。
int solvemaze(int horizontal, int vertical,int cote)
{
//horizontalestination is the last cell(maze[taille-1][taille-1])
if ((horizontal == taille - 1) && (vertical == taille - 1))
{
solution[horizontal,vertical] = 1;
return 1;
}
if (horizontal >= 0 && vertical >= 0 && horizontal < taille && vertical < taille && solution[horizontal,vertical] == 0)
{
printsolution();
//if safe to visit then visit the cell
solution[horizontal,vertical] = 1;
//under
if (mazeTab[horizontal,vertical,cote]==0)
return solvemaze(horizontal,vertical+1,(cote+1)%4);
cote = (cote + 1) % 4;
//right
if (mazeTab[horizontal, vertical, 1] == 0)
return solvemaze(horizontal+1, vertical, (cote + 1)% 4);
cote = (cote + 1) % 4;
//up
if (mazeTab[horizontal, vertical, 2] == 0)
return solvemaze(horizontal, vertical -1, (cote + 1) % 4);
cote = (cote + 1) % 4;
//left
if (mazeTab[horizontal, vertical, 3] == 0)
return solvemaze(horizontal -1, vertical, (cote + 1) % 4);
cote = (cote + 1) % 4;
//backtracking
solution[horizontal,vertical] = 0;
return 1;
}
return 1;
提醒您,如果您可以转到特定的单元格,则为1。因此解决迷宫的路径充满了1。
最后,我从0,3,0单元格开始迷宫,也就是这个迷宫:
1 0 0 0
0 0 1 0
0 1 1 0
**0** 1 1 0
当我跑步时,我得到的是这样:
0 1 1 1
1 1 0 0
1 0 0 0
0 0 0 0
以下0应该上交1。
0 1 1 1
1 1 0 **0**
1 0 0 **0**
**0** 0 0 **0**
你们能帮我找到错误吗?
答案 0 :(得分:0)
由于@HHLV,我已经弄清楚了如何解决此问题。
所以我的方法resolvemaze看起来像这样:
{{1}}
知道它可以工作,谢谢!