为什么这段代码会产生运行时错误。我正在尝试找到在迷宫中到达食物(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;
}