📌  相关文章
📜  访问N * M网格的所有角所需的最少步骤

📅  最后修改于: 2021-04-27 21:39:11             🧑  作者: Mango

给定两个整数NM代表2D网格的尺寸,两个整数RC代表一个块在该网格中的位置,任务是找到访问网格所有角的最小步骤数,从(R,C)开始。在每个步骤中,都允许在网格中移动侧面相邻的块。

例子:

方法:可以根据以下观察结果解决问题。

请按照以下步骤解决问题:

  • 首先使用上述观察结果,参观需要最少步骤的角落。
  • 通过顺时针或逆时针遍历网格的边界来访问网格的其他角,具体取决于哪一个将花费最少的步数来访问这些角。
  • 最后,打印获得的最小步数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum count of steps
// required to visit all the corners of the grid
int min_steps_required(int n, int m, int r, int c)
{
 
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = INT_MAX;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for rigthmost upper corner
    i = 1;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for rigthmost down corner
    i = n;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
 
// Driver Code
int main()
{
 
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    cout << min_steps_required(n, m, r, c);
 
    return 0;
}


Java
// Java Program to implement the
// above approach
import java.util.*;
class GFG
{
  
// Function to find the minimum count of steps
// required to visit all the corners of the grid
static int min_steps_required(int n, int m, int r, int c)
{
 
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = Integer.MAX_VALUE;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for rigthmost upper corner
    i = 1;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for rigthmost down corner
    i = n;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = Math.min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    System.out.print(min_steps_required(n, m, r, c));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program to implement
# the above approach
import sys
INT_MAX = sys.maxsize;
 
# Function to find the minimum count of steps
# required to visit all the corners of the grid
def min_steps_required(n, m, r, c) :
 
    # Stores corner of the grid
    i = 0; j = 0;
 
    # Stores minimum count of steps required
    # to visit the first corner of the grid
    corner_steps_req = INT_MAX;
 
    # Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for rigthmost upper corner
    i = 1;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for rigthmost down corner
    i = n;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Stores minimum count of steps required
    # to visit remaining three corners of the grid
    minimum_steps = min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
 
# Driver Code
if __name__ == "__main__" :
    n = 3;
    m = 2;
    r = 1;
    c = 1;
    print(min_steps_required(n, m, r, c));
 
    # This code is contributed by AnkThon


C#
// C# program to implement the
// above approach
using System;
 
class GFG{
  
// Function to find the minimum count
// of steps required to visit all the
// corners of the grid
static int min_steps_required(int n, int m,
                              int r, int c)
{
     
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = int.MaxValue;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for rigthmost upper corner
    i = 1;
    j = m;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for rigthmost down corner
    i = n;
    j = m;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = Math.Min(2 * (n - 1) + m - 1,
                                 2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    Console.Write(min_steps_required(n, m, r, c));
}
}
 
// This code is contributed by shikhasingrajput


输出:
4

时间复杂度: O(1)
辅助空间: O(1)