【发布时间】:2015-01-31 11:50:23
【问题描述】:
为什么这段代码会出现运行时错误。我试图找出它们是否存在迷宫内到达食物的路径(2)。 0代表障碍物,1代表路径,2代表目的地。
`{0,1,1,0,0},
{1,0,1,1,1},
{0,0,0,0,0},
{0,1,1,1,1},
{0,0,1,1,0},
{1,0,1,1,2}`
我以findpath(a,3,2) 作为起点,其中a 是迷宫,i=3、j=2 作为起点。
ideone上的代码:http://ideone.com/poF9um
谢谢大家帮助我。我已经纠正了我的错误。 这是更新代码的 ideone 链接:http://ideone.com/poF9um 谢谢。
#include <iostream>
using namespace std;
/*int insideMaze(int x, int y){
if(x>=6 || y >=5 || x<0 || y< 0)
{
return 0;
}
return 1;
}*/
bool findPath(int a[6][5], int n1, int m1){
if(n1>=6 || m1 >=5 || n1<0 || m1<0){
return false;
}
if(a[n1][m1] == 0){
return false;
}
if(a[n1][m1]==2){
return true;
}
//a[n1][m1] = 4;
if(findPath(a,n1-1,m1)==true){
return true;
}
if(findPath(a,n1,m1-1)==true){
return true;
}
if(findPath(a,n1+1,m1)==true){
return true;
}
if(findPath(a,n1,m1+1)==true){
return true;
}
return false;
}
int main() {
// your code goes here
//int a[10][10];
int a[6][5] = {
{0,1,1,0,0},
{1,0,1,1,1},
{0,0,0,0,0},
{0,1,1,1,1},
{0,0,1,1,0},
{1,0,1,1,2}
};
if(findPath(a,3,2)){
cout<<"Path Found";
}
return 0;
}
【问题讨论】:
-
让它打印它访问的位置,你应该会看到问题所在。