By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

A Maze problem in C++ using recursion

P: 1
I have a C++ problem that i just can not get right. I has been driving me crazy for quite some time.

The problem is as follows :

Numbers have been randomly inputed into an integer 2D array by the user. The program has to find a path from the top left corner to the bottom right corner but also keeping in mind to find the path/s whose sum is minimum. Only up and down and sideways movement is allowed.Diagonal movement is not allowed.

I have already written a code but it just hangs or goes into an infinite recursions.The code I have used is :

int pathSum(int a[MAX][MAX],int r,int c, int row,int col,int sum)
{int ci,ri;
static int min=0;
for(ri=-1;ri<=1;ri++)
{if(r+ri<0||r+ri>=row)
continue;
for(ci=-1;ci<=1;ci++)
{if(c+ci<0||c+ci>=col||(ci+ri)%2==0)
continue;
if(r!=row||c!=col)
{sum+=a[r][c];
pathSum(a, r+ri,c+ci,row,col,sum);
}
else
{if(sum<min)
min=sum;
}
}
}
return min;
}
I am still a student but this problem has taken a firm hold on me. Please help.
Aug 5 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
Have a look at Dijkstra's Shortest Path Algorithm.

kind regards,

Jos
Aug 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.