Given a NxN matrix, how could I find all possible paths to a location (i,i). Navigation would be only downwards and towards the right. The starting point would be (0,0).
P.S. Not a homework.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easiest (for me) would be to do it recursively:
1) the stop condition would be arriving at (i,i)
2) you would try two next moves at each recursion level:
Assuming that your current position is (x,y):
You cannot go right if your current “y” plus 1 would be bigger than “i” in the (i,i).
You cannot go down if your current “x” plus 1 would be bigger than “i” in the (i,i).
You’d just have to remember the path in some variable or pass it downwards and print it when the stop condition occurs.
So assuming you start at (0,0) you remember this and then check if you can go right or down, if yes for both then you recursively call this same method for (0,1) and (1,0).