Question
A robot is located at the top-left corner of a65 x45 grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
Note: The grid below is 7x3, and is used to illustrate the problem. It is not drawn to scale.

*Image not to scale.
How many possible unique paths are there?
(Note: Answer must be an exact, decimal representation of the number.)
Your answer:
For a M*N grid,the result is as simple as (N+M-2)!/(N-1)!/(M-1)!
-
- Function G_C_D(ByValxAsLong,ByValyAsLong)AsLong'GETGreatestCommonDivisor
- DimtempAsLong
- Ifx>yThentemp=x:x=y:y=temp'LETX<Y
- Do
- temp=yModx
- Iftemp=0ThenG_C_D=x:ExitFunction
- y=x
- x=temp
- Loop
- EndFunction
- FunctionRobot(ByValNAsLong,ByValmAsLong)AsString
- Ifm=1ThenRobot=1:ExitFunction
- DimS()AsString,result()AsLong,nn(),nm(),iAsLong,jAsLong,kAsLong,tempAsLong,numlenAsLong,lastAsLong
- ReDimnn(1Tom-1)
- ReDimnm(1Tom-1)
- Fori=1Tom-1
- nn(i)=N+m-1-i'(n+m-2)(n+m-3)*....(n-1)
- nm(i)=i'1*2*3*...*(m-1)
- Next
- j=1
- DoWhilej<m-1
- j=j+1
- Fori=1Tom-1
- temp=G_C_D(nm(j),nn(i))'GreatestCommonDivisor
- Iftemp>1Thennn(i)=nn(i)/temp:nm(j)=nm(j)/temp
- Ifnm(j)=1ThenExitFor
- Next
- Loop
- k=0
- Fori=1Tom-1
- Ifnn(i)>1Then
- k=k+1
- nn(k)=nn(i)
- EndIf
- Next
- ReDimPreservenn(1Tok)
- numlen=1
- ReDimresult(1Tonumlen)
- result(1)=1
- i=0
- DoWhilei<k
- i=i+1
- last=0
- Forj=1Tonumlen
- temp=result(j)*nn(i)+last
- result(j)=tempMod100000
- last=temp\100000
- Next
- DoWhileNotlast=0
- ReDimPreserveresult(1Tonumlen+1)
- result(numlen+1)=lastMod100000
- last=last\100000
- numlen=UBound(result)
- Loop
- Loop
- ReDimS(1Tonumlen)
- Fori=2Tonumlen
- S(i)=Format(result(numlen+1-i),"00000")
- Next
- S(1)=result(numlen)
- Erasenn()
- Robot=Join(S,"")
- EndFunction
- PrivateSubCommand1_Click()
- MsgBoxRobot(65,45)
- EndSub
It returns:
3927192747782241611458841012775
Later , I use 3 lines python codes to solve it:
- fac=lambdan:[1,0][n>0]orfac(n-1)*n
- robot=lambdam,n:fac(m+n-2)/(fac(m-1)*fac(n-1))
- printrobot(65,45)
Conclusion:
It seemsVB is the worst choice for numbers calculating.
本文介绍了一种计算机器人从网格左上角到右下角所有唯一路径数量的方法。使用组合数学原理,通过VB和Python实现算法,展示了不同编程语言处理大规模数值计算的能力差异。
5万+

被折叠的 条评论
为什么被折叠?



