You have made an all too common mistake. Far too many folks believe the primary goal in writing software is to tell the computer what to do. That is incorrect. The primary goal is to communicate CLEARLY what one is doing to other humans. The secondary goal is telling the computer what to do.
I suggest you don't use hardcoded numbers (-1 in map[][]=-1). Enums are made for this. Variable names could be more descriptive. ("blah"?) You can leave for clauses empty, e.g. for(; blah<(x+1); blah++). (Even for(;;) is valid.) Usually arrays go from 0..LIMIT-1 so you can say for(; blah<x; ++blah). Often ++foo is preferred to foo++, though it won't make much of a difference with ints. Watch out for underscores in names. It's easy to confuse path-found and path_found. ADD COMMENTS!!! Explain what you are doing. Add spaces while you are at it. Compact code is nobody's friend. At the very least, document that you are altering the values in MAP.
All that said... Where to begin...
Well, after changing nodes, I'd have started from the first path option, not at the current node plus 1. (E.g., Jump from 1-to-2, from 2-to-4, and then you start looking at map[4][5] instead of map[4][0] or map[4][1].
I would not have special cased 0 as an exit criteria.
Your line "for(blah;blah<(x+1);blah++){" does not exit (break) when a map[m][blah]==1 match is found, but marks it as -1 and continues looking for other matches. That should create an interesting effect with the prior "for(int n=0;n<(x+1);n++){" line.
Once you hit M=6, N=1 you can never hit your special-cased-0 exit criteria and print. (You do realize you have n=0 followed by n++???)
Once you have a map full of -1's, you can never proceed further. This is a real problem when some of your paths share the same links. E.g. 1-2-4-6-0 and 1-3-5-6-0 share 6-0.
You do not detect a map full of -1's. You have an endless loop. (If you are going to set path_found to false, do it right after "do{", before "for(int n=0;n<(x+1);n++){". Not in some easily missed exit spot deep in nested if statements.
I do not see the output 12460,13560,1250,124 when I run your program. I see 12460,135 and then it hangs. (And that only after I modified your software to flush output after every write.)
You show no scaffolding code. Something like:
void printMap(int theMap[][MAX] )
{
for( int i=0; i<MAX; ++i )
{
cout << "[" << i << "] ";
for ( int j=0; j<MAX; ++j )
cout << " " << theMap [i] [j];
cout << endl;
} /* for( int i=0; i<MAX; ++i ) */
cout << endl;
}
Coupled with a debugger (in gdb use "call printMap(map)") would be helpful to you. (It certainly was to me.)
Your code replicates itself. That's usually a sign of a mistake. E.g. the "if(m==0){ path=0; cout<<path<<endl; m=1; path_found=false; }" bits. Or the "if(map[m][FOO]==1){ map[m][FOO]=-1; path=m; path_found = true; cout<<path; m=FOO;" part, where FOO is "blah" or "n".
All this said, I've had to work with worse code (in professional situations no less). (Not often, but it does happen.) Keep at it, and you'll get better. We all started in the same bad place. Practice, keep asking questions, and you will improve.
Responding to comment here...
The only reason I had the special 0 case in there is because it was just hanging when it reached 0 because that row is empty and it stayed in an endless loop.
You need a better test for your exit condition.
When I compile and run it i get the output I described, not what you get.
I used g++ version 3.4.4 with:
#define MAX 7
/* 0 1 2 3 4 5 6 */
int map[MAX][MAX] = { { 0, 0, 0, 0, 0, 0, 0 }, /*0*/
{ 0, 0, 1, 1, 0, 0, 0 }, /*1*/
{ 0, 0, 0, 0, 1, 1, 0 }, /*2*/
{ 0, 0, 0, 0, 0, 1, 0 }, /*3*/
{ 0, 0, 0, 0, 0, 0, 1 }, /*4*/
{ 0, 0, 0, 0, 0, 0, 1 }, /*5*/
{ 1, 0, 0, 0, 0, 0, 0 } /*6*/
};
find_path( map, 6 );
Perhaps your input conditions are different?
I don't understand why it would reach m=6 n=1, other than [6][0], the rest of the row is 0s.
The code below is yours (with minor cosmetic changes).
N=0 occurs at Line=25. When the for loop at Line=16 completes, we resume the for loop at Line=9. The first step there is the final iteration (n++) component. Thus the next for loop iteration at Line=10 has m=blah=6 and n=1. At this stage, none of your conditional tests (==-1, ==1) can ever be true.
You really ought to try stepping through your code in a debugger. See what the code is actually doing vs what you expected it to do.
/* 0*/ void find_path( int map[][MAX], int x )
/* 1*/ {
/* 2*/ int path = 0;
/* 3*/ int m = 1;
/* 4*/ int blah = 0;
/* 5*/ bool path_found = false;
/* 6*/
/* 7*/ do
/* 8*/ {
/* 9*/ for ( int n=0; n < (x+1); n++ )
/*10*/ {
/*11*/ if ( map [m] [n] == -1 )
/*12*/ {
/*13*/ blah = (n+1);
/*14*/ if ( blah < (x+1) )
/*15*/ {
/*16*/ for ( ; blah < (x+1); blah++ )
/*17*/ {
/*18*/ if ( map [m] [blah] == 1 )
/*19*/ {
/*20*/ map [m] [blah] = -1;
/*21*/ path = m;
/*22*/ path_found = true;
/*23*/ cout << path;
/*24*/ m = blah;
/*25*/ n = 0;
/*26*/ } /* if ( map [m] [blah] == 1 ) */
/*27*/ } /* for ( ; blah < (x+1); blah++ ) */
/*28*/ } /* if ( blah < (x+1) ) */
/*29*/ else
/*30*/ {
/*31*/ path = m;
/*32*/ path_found = false;
/*33*/ cout << path;
/*34*/ m = n;
/*35*/ if ( m == 0 )
/*36*/ {
/*37*/ path = 0;
/*38*/ cout << path << endl;
/*39*/ m = 1;
/*40*/ path_found = false;
/*41*/ } /* if ( m == 0 ) */
/*42*/ } /* if ( blah < (x+1) ) ... ELSE ... */
/*43*/ } /* if ( map [m] [n] == -1 ) */
/*44*/ else if ( map [m] [n] == 1 )
/*45*/ {
/*46*/ map [m] [n] = -1;
/*47*/ path = m;
/*48*/ path_found = true;
/*49*/ cout << path;
/*50*/ m = n;
/*51*/ if ( m == 0 )
/*52*/ {
/*53*/ path = 0;
/*54*/ cout << path << endl;
/*55*/ m = 1;
/*56*/ path_found = false;
/*57*/ } /* if ( m == 0 ) */
/*58*/ } /* if(map[m][n]==-1) ... ELSE IF (map[m][n]==1) ... */
/*59*/
/*60*/ } /* for ( int n=0; n < (x+1); n++ ) */
/*61*/
/*62*/ } /* do { ... } */ while(m<(x+1) && path_found);
/*63*/
/*64*/ } /* void find_path( int map[][MAX], int x ) */