Matrix walking problem

I'm new to C++. I'm solving the "Matrix walking problem", but I can't find the law of this question.The problem goes like this:

Given a pane of n*m cells, how many different paths are there from the upper left corner to the lower right corner?
You can only walk down or to the right at each step, and you can only walk along the edges of the matrix.

I don't know how to fix this, so please help me.
Thanks.
(my English is not well, sorry)
Last edited on
You can only walk down or to the right at each step, and you can only walk along the edges of the matrix.


Well, if you take the second statement at face value then there are only 2 such paths. Since I doubt that is what you mean then you had better have another go at translating it.

Otherwise, if you can step on any cell of the matrix then there are C(m+n-2,m-1) paths, since you have to choose (m-1) steps across and (n-1) steps down; i.e. you have to choose which of the (m-1)+(n-1) points you are going to put your (m-1) across steps.

But I think you had better clarify your problem.
Last edited on
Yes, we can only walk along the edges of the matrix.
SYX00 wrote:
Yes, we can only walk along the edges of the matrix.


Then your only possible paths would look like
|
|
|
|__________

and
___________
           |
           |
           |
           |


But I would be very surprised if that were what was being asked
Last edited on
NO,it like this:
_
.|_
....|_
Please ignore "."
(emmmm)
Last edited on
If you can go across the centre of the matrix (which contradicts what you said) then there are
C(m+n-2,m-1) paths.
(That's a binomial coefficient, also written m+n-2Cm-1. Number of ways of choosing m-1 things from m+n-2 things.) Somewhere along the line you have to take m-1 steps across and n-1 steps down.
Last edited on
That is, to find x m-1s from m+n-2, and x is the answer to this question, right?
If it's right, I can do this:
a=m+ n-2;
b=m-1;
x=a/b;
cout<<x;
right?
(If I understand what you are asking - which is not a given) you have to calculate the binomial coefficient
m+n-2Cm-1

But I'll give you a hint on that: DON'T try calculating large factorials.
How do I use this function in C++?
How do I use this function in C++?

You write your OWN function to calculate it.


BTW I missed your earlier post:
a=m+ n-2;
b=m-1;
x=a/b;
cout<<x;
right?

Wrong!
Last edited on
Ok,thank you!
Topic archived. No new replies allowed.