English 中文(简体)
Stuck in 无限入侵
原标题:Stuck in Infinite Recursion

我有这一职能在C++中解决马奇问题,但当我管理该方案时,我就在再次入侵中获得了巴登通道。 我认为可能是无限的 lo。 我不知道什么地方和什么是错的。

bool Solve_Maze(int coorx,int coory) {
    if((Map[coorx][coory]==Start)||(Map[coorx][coory]==path)) {
        Map[coorx][coory]=wall;
        Solve_Maze(coorx+1,coory);
        Solve_Maze(coorx-1,coory);
        Solve_Maze(coorx,coory+1);
        Solve_Maze(coorx,coory-1);
    }else if(Map[coorx][coory]==End) {
        cout<<"You Solved the Maze!"<<endl;
        delete Map;
        return(true);
    }
}
问题回答

1) You are not returning any value in if statement
2) Map[coorx][coory] always assigned to wall in all function call..Does wall refers to a global state?

I changed the function to return void since the value wasn t properly being returned up the stack. In this case you will just use the global found variable to check if the end was found. (this will require you to set found = false every time before you run the function).

bool found = false;

You also want to do some input validation

if( coorx > maxX || coorx < 0 || coory > maxY || coory < 0) return;

您将需要用比您的 co和ory的最高值高出1的数值来取代max和max。 这将确保你不会遇到不利的出入错误。

bool found = false; // this will be global scope or pass it by reference
Solve_Maze(x,y);
// if(found) - found will be true if you found the end

void Solve_Maze(int coorx,int coory) {
    if( coorx > maxX || coorx < 0 || coory > maxY || coory < 0) return;
    else if(((Map[coorx][coory]==Start)||(Map[coorx][coory]==path))) {
        Map[coorx][coory]=wall;
        Solve_Maze(coorx+1,coory);
        Solve_Maze(coorx-1,coory);
        Solve_Maze(coorx,coory+1);
        Solve_Maze(coorx,coory-1);
    }else if(Map[coorx][coory]==End) {
        cout<<"You Solved the Maze!"<<endl;
        delete Map;
        found = true;
    }
}

在一个夸张(gdb或dbx)中进行。 与旗帜相容,使你的方案得以脱节。 如果你不了解如何使用夸张,那么,就把“dbx cheatsheet”gle到别处。 你可以孤立地.在 lo中(如果你的gues客是正确的),走过你的道路。 要使你能够做得足够多,就必须花一点时间来做这项工作,实际上,这还不到你花了多少时间思考这个问题。

没有任何东西方意——人们实际上常常高估工作,学习一小gger,因此,我真的要说,即便是一个简单的问题,它也是值得的,而且要为大问题付出巨大的代价。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签