[英]How to find all possible reachable numbers from a position?
Given 2 elements n, s and an array A of size m
, where s is initial position
which lies between 1 <= s <= n
, our task is to perform m operations to s and in each operation we either make s = s + A[i] or s = s - A[i], and we have to print all the values which are possible after the m operation and all those value should lie between 1 - n (inclusive). 给定
2 elements n, s and an array A of size m
,其中s is initial position
介于1 <= s <= n
之间的s is initial position
,我们的任务是对s执行m次操作,在每项操作中,我们都使s = s + A [i]或s = s-A [i],并且我们必须打印在m运算之后可能出现的所有值,并且所有这些值都应在1-n(含)之间。
Important Note: If during an operation we get a value s < 1 or s > n, we don't go further with that value of s.
重要说明:如果在操作过程中获得的值s <1或s> n,则无需进一步处理s的值。
I solved the problem using BFS, but the problem is BFS approach is not optimal here, can someone suggest any other more optimal approach to me or an algorithm will greatly help. 我使用BFS解决了问题,但问题是BFS方法在这里不是最佳方法,有人可以向我建议任何其他更理想的方法,否则算法将有很大帮助。
For example:- 例如:-
If n = 3, s = 3, and A = {1, 1, 1} 如果n = 3,s = 3,并且A = {1,1,1}
3
/ \
operation 1: 2 4 (we don’t proceed with 4 as it is > n)
/ \ / \
operation 2: 1 3 3 5
/ \ / \ / \ / \
operation 3: 0 2 2 4 2 4 4 6
So final values reachable by following above rules are 2 and 2 (that is two times 2). 因此,遵循上述规则可获得的最终值是2和2(即2的两倍)。 we don't consider the third two as it has an intermediate state which is > n ( same case applicable if < 1).
我们不考虑第三个参数,因为它的中间状态为> n(如果<1,则适用相同情况)。
There is this dynamic programming solution, which runs in O(nm)
time and requires O(n)
space. 有这种动态编程解决方案,其运行时间为
O(nm)
,需要O(n)
空间。
First establish a boolean array called reachable
, initialize it to false
everywhere except for reachable[s]
, which is true
. 首先建立一个名为
reachable
的布尔数组,在所有地方都将其初始化为false
,除非reachable[s]
为true
。
This array now represents whether a number is reachable in 0
steps. 现在,此数组表示数字是否可以
0
步到达。 Now for every i
from 1
to m
, we update the array so that reachable[x]
represents whether the number x
is reachable in i
steps. 现在,对于从
1
到m
每个i
,我们都更新数组,以使reachable[x]
表示i
步中x
是否可达。 This is easy: x
is reachable in i
steps if and only if either x - A[i]
or x + A[i]
is reachable in i - 1
steps. 这是容易的:
x
是可到达的i
的步骤,当且仅如果任一x - A[i]
或x + A[i]
是在到达i - 1
步骤。
In the end, the array becomes the final result you want. 最后,数组成为您想要的最终结果。
EDIT: pseudo-code here. 编辑:此处为伪代码。
// initialization:
for x = 1 to n:
r[x] = false
r[s] = true
// main loop:
for k = 1 to m:
for x = 1 to n:
last_r[x] = r[x]
for x = 1 to n:
r[x] = (last_r[x + A[k]] or last_r[x - A[k]])
Here last_r[x]
is by convention false
if x
is not in the range [1 .. n]
. 如果
x
不在[1 .. n]
范围内,则last_r[x]
按照惯例为false
。
If you want to maintain the number of ways that each number can be reached, then you do the following changes: 如果要保持达到每个数字的方式的数量,请进行以下更改:
r
to an integer array; r
更改为整数数组; r[x]
to 0
, except r[s]
to 1
; r[x]
初始化为0
,除r[s]
初始化为1
; In the main loop, change the key line to: 在主循环中,将关键行更改为:
r[x] = last_r[x + A[k]] + last_r[x - A[k]] r [x] = last_r [x + A [k]] + last_r [x-A [k]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.