이 코드가 런타임 오류를주는 이유는 무엇입니까? 나는 그들의 존재가 음식에 도달하는 미로 내부의 경로인지 찾아 내려고 노력 중입니다 (2). 0은 장애물, 1은 경로, 2는 목적지를 나타냅니다. A는 미로와 출발점으로 i=3
, j=2
입니다2-d 미로에서 경로 찾기
`{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)
로 시작점을 통과하고있다. 코드를 ideone : http://ideone.com/poF9um
나를 도와 주셔서 감사합니다. 나는 내 실수를 바로 잡았다. 다음은 업데이트 된 코드의 ideone 링크입니다. http://ideone.com/poF9um Thanks.
#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;
}
방문한 위치를 인쇄하여 문제가 무엇인지 확인해야합니다. – interjay
고마워! 정말 도움이되었습니다. – nitte93user3232918