简体   繁体   English

如何从一个位置查找所有可能的可到达数字?

[英]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. 现在,对于从1m每个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: 如果要保持达到每个数字的方式的数量,请进行以下更改:

  1. Change the array r to an integer array; 将数组r更改为整数数组;
  2. In the initialization, initialize all r[x] to 0 , except r[s] to 1 ; 在初始化中,将所有r[x]初始化为0 ,除r[s]初始化为1
  3. 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.

相关问题 如何找到某个顶点可到达的所有边的总权重? - How to find the sum weight of all edges reachable from some vertex? 给定一个字节中的 8 位,如何找到翻转该字节中一位或多位所产生的所有可能数字 - Given 8 bits in a byte, how to find all the possible numbers resulting from flipping of one or more bits in this byte 在有向图中查找所有其他节点均可访问的节点 - Find nodes in directed graph that is reachable from all other nodes 如何遍历数组并找到C中的所有可到达点? - How to traverse an array and find all reachable points in C? 如何检查图中的所有节点是否可以从所有其他节点访问? - How to check if all nodes in a graph are reachable from all other nodes? 查找数组中数字的所有可能排列 - Find all possible arrangements of a n numbers in an array 如何从数字列表中生成所有可能的最大堆? - How to generate all possible max-heaps from a list of numbers? 如何在不遍历所有多边形的情况下找到所有视线可达的点? - How can I find all sight-reachable points without iterating over all polygons? 从多个子集中,找到所有可能的数字组合以获得给定的总和 - From multiple subsets, find all possible combinations of numbers to obtain a given sum 从4个输入数字中查找所有可能的组合,最多可添加24个 - Find all possible combinations from 4 input numbers which can add up to 24
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM