Matrix walking problem

Mar 13, 2022 at 9:12am
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 Mar 13, 2022 at 9:14am
Mar 13, 2022 at 9:25am
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 Mar 13, 2022 at 9:33am
Mar 13, 2022 at 9:34am
Yes, we can only walk along the edges of the matrix.
Mar 13, 2022 at 9:37am
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 Mar 13, 2022 at 9:39am
Mar 13, 2022 at 9:39am
NO,it like this:
_
.|_
....|_
Please ignore "."
(emmmm)
Last edited on Mar 13, 2022 at 9:41am
Mar 13, 2022 at 9:42am
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 Mar 13, 2022 at 9:43am
Mar 13, 2022 at 9:44am
That is, to find x m-1s from m+n-2, and x is the answer to this question, right?
Mar 13, 2022 at 9:45am
If it's right, I can do this:
a=m+ n-2;
b=m-1;
x=a/b;
cout<<x;
right?
Mar 13, 2022 at 9:46am
(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.
Mar 13, 2022 at 9:48am
How do I use this function in C++?
Mar 13, 2022 at 10:01am
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 Mar 13, 2022 at 10:04am
Mar 13, 2022 at 10:06am
Ok,thank you!
Topic archived. No new replies allowed.