给定一个N×N的矩阵,我怎么能找到的所有可能路径的位置(我,我)? [英] Given a NxN matrix, how could I find all possible paths to a location (i,i)?

查看:468
本文介绍了给定一个N×N的矩阵,我怎么能找到的所有可能路径的位置(我,我)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 N×N个矩阵,我怎么能找到所有可能的路径到一个位置(I,I)。导航将只有向下和朝向右键。其出发点是(0,0)

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。没有一门功课。

P.S. Not a homework.

推荐答案

最简单的(对我来说)将是递归做到这一点:

The easiest (for me) would be to do it recursively:

1)停止条件将到达(I,I)

1) the stop condition would be arriving at (i,i)

2)你会尝试两个下移动在每个递归级别:

2) you would try two next moves at each recursion level:

  • 您可以去
  • 您可向右走

假设你的当前位置(X,Y): 你不能去的权利,如果你目前的Y加1会大于我在(I,I)。 你不能下去,如果你目前的x加1会大于我在(I,I)。

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.

因此​​,假如你开始在(0,0)你还记得这个,然后检查,如果你可以去向右或向下,如果是两个,那么你递归调用同样的方法为(0,1)和(1,0)

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).

这篇关于给定一个N×N的矩阵,我怎么能找到的所有可能路径的位置(我,我)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆