给定两个整数N和M代表2D网格的尺寸,两个整数R和C代表一个块在该网格中的位置,任务是找到访问网格所有角的最小步骤数,从(R,C)开始。在每个步骤中,都允许在网格中移动侧面相邻的块。
例子:
Input: N = 2, M = 2, R = 1, C = 2
Output: 3
Explanation:
(1, 2) -> (1, 1) -> (2, 1) -> (2, 2)
Therefore, the required output is 3.
Input: N = 2, M = 3, R = 2, C = 2
Output: 5
Explanation:
(2, 2) -> (2, 3) -> (1, 3) -> (1, 2) -> (1, 1) -> (2, 1)
Therefore, the required output is 5.
方法:可以根据以下观察结果解决问题。
Minimum count of steps required to visit the block (i2, j2) starting from (i1, j1) is equal to abs(i2 – i1) + abs(j2 – j1)
请按照以下步骤解决问题:
- 首先使用上述观察结果,参观需要最少步骤的角落。
- 通过顺时针或逆时针遍历网格的边界来访问网格的其他角,具体取决于哪一个将花费最少的步数来访问这些角。
- 最后,打印获得的最小步数。
下面是上述方法的实现:
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)