📜  计算网格中给定方向的可能移动

📅  最后修改于: 2021-05-04 18:07:44             🧑  作者: Mango

给定一个网格N x M大小和初始位置(X,Y),并进行一系列K移动。
每一步包含2个整数DxDy
单步移动包含以下操作,该操作也可以执行多次。

任务是计算已执行的移动次数。
请注意,在每个时间点,当前位置都必须在网格内。

例子:

方法:可以观察到,我们可以独立处理XY ,并将它们合并为当前移动的步数=的最小值( X的步数, Y的步数)。要找到X的移动次数,我们可以使用Dx的符号来知道我们要接近的一端,并找到到该端的距离,速度为Dx ,我们可以找到它要走的步数。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
#define int long long
using namespace std;
  
// Function to return the count of
// possible steps in a single direction
int steps(int cur, int x, int n)
{
  
    // It can cover infinte steps
    if (x == 0)
        return INT_MAX;
  
    // We are approaching towards X = N
    if (x > 0)
        return abs((n - cur) / x);
  
    // We are approaching towards X = 1
    else
        return abs((cur - 1) / x);
}
  
// Function to return the count of steps
int countSteps(int curx, int cury, int n, int m,
               vector > moves)
{
    int count = 0;
    int k = moves.size();
    for (int i = 0; i < k; i++) {
        int x = moves[i].first;
        int y = moves[i].second;
  
        // Take the minimum of both moves independently
        int stepct
            = min(steps(curx, x, n), steps(cury, y, m));
  
        // Update count and current positions
        count += stepct;
        curx += stepct * x;
        cury += stepct * y;
    }
    return count;
}
  
// Driver code
main()
{
    int n = 4, m = 5, x = 1, y = 1;
    vector > moves = { { 1, 1 },
                                      { 1, 1 },
                                      { 0, -2 } };
    int k = moves.size();
    cout << countSteps(x, y, n, m, moves);
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
  
    // Function to return the count of
    // possible steps in a single direction
    static int steps(int cur, int x, int n) 
    {
  
        // It can cover infinte steps
        if (x == 0)
            return Integer.MAX_VALUE;
  
        // We are approaching towards X = N
        if (x > 0)
            return Math.abs((n - cur) / x);
  
        // We are approaching towards X = 1
        else
            return Math.abs((cur - 1) / x);
    }
  
    // Function to return the count of steps
    static int countSteps(int curx, int cury,
                             int n, int m, 
                             int[][] moves) 
    {
        int count = 0;
        int k = moves.length;
        for (int i = 0; i < k; i++)
        {
            int x = moves[i][0];
            int y = moves[i][1];
  
            // Take the minimum of  
            // both moves independently
            int stepct = Math.min(steps(curx, x, n), 
                                  steps(cury, y, m));
  
            // Update count and current positions
            count += stepct;
            curx += stepct * x;
            cury += stepct * y;
        }
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 4, m = 5, x = 1, y = 1;
        int[][] moves = { { 1, 1 }, { 1, 1 },
                          { 0, -2 } };
  
        System.out.print(countSteps(x, y, n, m, moves));
    }
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to return the count of 
# possible steps in a single direction 
def steps(cur, x, n): 
  
    # It can cover infinte steps 
    if x == 0:
        return float('inf') 
  
    # We are approaching towards X = N 
    elif x > 0:
        return abs((n - cur) // x) 
  
    # We are approaching towards X = 1 
    else:
        return abs(int((cur - 1) / x)) 
  
# Function to return the count of steps 
def countSteps(curx, cury, n, m, moves): 
  
    count = 0
    k = len(moves) 
    for i in range(0, k): 
        x = moves[i][0] 
        y = moves[i][1]
  
        # Take the minimum of both moves 
        # independently 
        stepct = min(steps(curx, x, n), 
                     steps(cury, y, m)) 
  
        # Update count and current positions 
        count += stepct 
        curx += stepct * x 
        cury += stepct * y
      
    return count 
  
# Driver code 
if __name__ == "__main__":
  
    n, m, x, y = 4, 5, 1, 1
    moves = [[1, 1], [1, 1], [0, -2]]
      
    print(countSteps(x, y, n, m, moves))
  
# This code is contributed 
# by Rituraj Jain


C#
// C# implementation of the above approach
using System;
  
class GFG
{
  
    // Function to return the count of
    // possible steps in a single direction
    static int steps(int cur, int x, int n) 
    {
  
        // It can cover infinte steps
        if (x == 0)
            return int.MaxValue;
  
        // We are approaching towards X = N
        if (x > 0)
            return Math.Abs((n - cur) / x);
  
        // We are approaching towards X = 1
        else
            return Math.Abs((cur - 1) / x);
    }
  
    // Function to return the count of steps
    static int countSteps(int curx, int cury,
                          int n, int m, 
                          int[,] moves) 
    {
        int count = 0;
        int k = moves.GetLength(0);
        for (int i = 0; i < k; i++)
        {
            int x = moves[i, 0];
            int y = moves[i, 1];
  
            // Take the minimum of 
            // both moves independently
            int stepct = Math.Min(steps(curx, x, n), 
                                  steps(cury, y, m));
  
            // Update count and current positions
            count += stepct;
            curx += stepct * x;
            cury += stepct * y;
        }
        return count;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 4, m = 5, x = 1, y = 1;
        int[,] moves = { { 1, 1 }, { 1, 1 },
                         { 0, -2 } };
  
        Console.Write(countSteps(x, y, n, m, moves));
    }
}
  
// This code is contributed by Rajput-Ji


PHP
 0) 
        return floor(abs(($n - $cur) / $x)); 
  
    // We are approaching towards X = 1 
    else
        return floor(abs(($cur - 1) / $x)); 
} 
  
// Function to return the count of steps 
function countSteps($curx, $cury, $n, $m, $moves) 
{ 
    $count = 0; 
    $k = sizeof($moves); 
    for ($i = 0; $i < $k; $i++) 
    { 
        $x = $moves[$i][0]; 
        $y = $moves[$i][1]; 
  
        // Take the minimum of both moves independently 
        $stepct = min(steps($curx, $x, $n), steps($cury, $y, $m)); 
  
        // Update count and current positions 
        $count += $stepct; 
        $curx += $stepct * $x; 
        $cury += $stepct * $y; 
    } 
    return $count; 
} 
  
    // Driver code 
    $n = 4 ;
    $m = 5 ;
    $x = 1 ;
    $y = 1 ; 
    $moves = array( array( 1, 1 ), 
                        array( 1, 1 ), 
                        array( 0, -2 ) ); 
    $k = sizeof($moves);
      
    echo countSteps($x, $y, $n, $m, $moves); 
      
// This code is contributed by Ryuga
?>


输出:
4

时间复杂度: O(N)