给定字符串str和四个整数X 1 , Y 1 , X 2和Y 2 ,其中(X 1 ,Y 1 )表示源坐标,而(X 2 ,Y 2 )表示目标坐标。给定字符串str ,任务是找到从源到达目标所需的以下四种类型的最小步数:
- 如果str [i] =’E’:将(X 1 ,Y 1 )转换为(X 1 +1,Y 1 )。
- 如果str [i] =’W’:将(X 1 ,Y 1 )转换为(X 1 – 1,Y 1 )。
- 如果str [i] =’N’:将(X 1 ,Y 1 )转换为(X 1 ,Y 1 +1)。
- 如果str [i] =’S’:将(X 1 ,Ysub> 1)转换为(X 1 ,Y 1 – 1)。
如果无法到达目的地,请打印-1 。
注意:不必始终使用str [i]并且可以跳过。但是跳过的字符将添加到所使用的步骤中。例子
Input: str = “SESNW”, x1 = 0, y1 = 0, x2 = 1, y2 = 1
Output: 4
Explanation:
To move from (0, 0) to (1, 1), it requires one ‘E’ and one ‘N’.
Therefore, the path defined by the substring “SESN” ensures that the destination is reached {(0, 0) -> skip S -> E(1, 0) -> skip S -> (1, 1)}.
Therefore, the minimum length of the string traversed is 4.
Input: str = “NNNNNNNN”, x1 = 1, y1 = 1, x2 = 1, y2 = 2
Output: 1
Explanation:
From current position (1, 1) it can move to co-ordinate (1, 2) using first ‘N’ direction.
Therefore, the minimum length of the string traverse is 1.
方法:想法是遍历字符串并执行移动直到到达目的地。步骤如下:
- 初始化四个变量pos1,pos2,pos3和pos4 ,分别存储E,W,N,S的位置。
- 现在,检查(x1,y1)是否等于(x2,y2),那么当前位置已经是目的地,因此打印0 。
- 否则,遍历字符串并检查以下四个条件:
- 如果x2> x1,则进行迭代直到出现‘E’并更新pos1中的索引。
- 如果x2
则进行迭代,直到出现“ W”并更新pos2中的索引。 - 如果y2> y1,则进行迭代,直到出现“ N”并更新pos3中的该索引。
- 如果y2
则进行迭代,直到出现“ S”并更新pos4中的索引。
- 执行上述操作后,检查x1!= x2和y1!= y2,然后打印“ -1” ,这意味着无法到达目的地。
- 否则,找到pos1,pos2,pos3和pos4的最大值并打印。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum length
// of string required to reach from
// source to destination
void minimum_length(int x1, int y1,
int x2, int y2,
string str)
{
// Size of the string
int n = str.size();
// Stores the index of the four
// directions E, W, N, S
int pos1, pos2, pos3, pos4;
pos1 = -1;
pos2 = -1;
pos3 = -1;
pos4 = -1;
// If destination reached
if (x1 == x2 && y1 == y2) {
cout << 0 << endl;
}
// Iterate over the string
else {
for (int i = 0; i < n; i++) {
// Move east
if (x2 > x1) {
// Change x1 according
// to direction E
if (str[i] == 'E') {
x1 = x1 + 1;
if (x1 == x2) {
pos1 = i;
}
}
}
// Move west
if (x2 < x1) {
// Change x1 according
// to direction W
if (str[i] == 'W') {
x1 = x1 - 1;
if (x1 == x2) {
pos2 = i;
}
}
}
// Move north
if (y2 > y1) {
// Change y1 according
// to direction N
if (str[i] == 'N') {
y1 = y1 + 1;
if (y1 == y2) {
pos3 = i;
}
}
}
// Move south
if (y2 < y1) {
// Change y1 according
// to direction S
if (str[i] == 'S') {
y1 = y1 - 1;
if (y1 == y2) {
pos4 = i;
}
}
}
}
int z;
// Store the max of all positions
z = max(max(max(pos1, pos2),
pos3),
pos4);
// Print the minimum length of
// string required
if (x1 == x2 && y1 == y2) {
cout << z + 1 << endl;
}
// Otherwise, it is impossible
else {
cout << "-1" << endl;
}
}
}
// Driver Code
int main()
{
// Given string
string str = "SESNW";
// Given source and destination
int x1 = 0, x2 = 1, y1 = 0, y2 = 1;
// Function Call
minimum_length(x1, y1, x2, y2, str);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// Function to find the minimum length
// of string required to reach from
// source to destination
static void minimum_length(int x1, int y1,
int x2, int y2,
String str)
{
// Size of the string
int n = str.length();
// Stores the index of the four
// directions E, W, N, S
int pos1, pos2, pos3, pos4;
pos1 = -1;
pos2 = -1;
pos3 = -1;
pos4 = -1;
// If destination reached
if (x1 == x2 && y1 == y2)
{
System.out.println("0");
}
// Iterate over the string
else
{
for(int i = 0; i < n; i++)
{
// Move east
if (x2 > x1)
{
// Change x1 according
// to direction E
if (str.charAt(i) == 'E')
{
x1 = x1 + 1;
if (x1 == x2)
{
pos1 = i;
}
}
}
// Move west
if (x2 < x1)
{
// Change x1 according
// to direction W
if (str.charAt(i) == 'W')
{
x1 = x1 - 1;
if (x1 == x2)
{
pos2 = i;
}
}
}
// Move north
if (y2 > y1)
{
// Change y1 according
// to direction N
if (str.charAt(i) == 'N')
{
y1 = y1 + 1;
if (y1 == y2)
{
pos3 = i;
}
}
}
// Move south
if (y2 < y1)
{
// Change y1 according
// to direction S
if (str.charAt(i) == 'S')
{
y1 = y1 - 1;
if (y1 == y2)
{
pos4 = i;
}
}
}
}
int z;
// Store the max of all positions
z = Math.max(pos1,
Math.max(Math.max(pos2, pos3),
pos4));
// Print the minimum length of
// string required
if (x1 == x2 && y1 == y2)
{
System.out.println(z + 1);
}
// Otherwise, it is impossible
else
{
System.out.println("-1");
}
}
}
// Driver Code
public static void main (String[] args)
throws java.lang.Exception
{
// Given string
String str = "SESNW";
// Given source and destination
int x1 = 0, x2 = 1, y1 = 0, y2 = 1;
// Function call
minimum_length(x1, y1, x2, y2, str);
}
}
// This code is contributed by bikram2001jha
Python3
# Python3 program for the above approach
# Function to find the minimum length
# of string required to reach from
# source to destination
def minimum_length(x1, y1, x2, y2, str):
# Size of the string
n = len(str)
# Stores the index of the four
# directions E, W, N, S
pos1 = -1
pos2 = -1
pos3 = -1
pos4 = -1
# If destination reached
if (x1 == x2 and y1 == y2):
print("0")
# Iterate over the string
else:
for i in range(n):
# Move east
if (x2 > x1):
# Change x1 according
# to direction E
if (str[i] == 'E'):
x1 = x1 + 1
if (x1 == x2):
pos1 = i
# Move west
if (x2 < x1):
# Change x1 according
# to direction W
if (str[i] == 'W'):
x1 = x1 - 1
if (x1 == x2):
pos2 = i
# Move north
if (y2 > y1):
# Change y1 according
# to direction N
if (str[i] == 'N'):
y1 = y1 + 1
if (y1 == y2):
pos3 = i
# Move south
if (y2 < y1):
# Change y1 according
# to direction S
if (str[i] == 'S'):
y1 = y1 - 1
if (y1 == y2):
pos4 = i
z = 0
# Store the max of all positions
z = max(pos1, max(max(pos2, pos3), pos4))
# Print the minimum length of
# string required
if (x1 == x2 and y1 == y2):
print(z + 1)
# Otherwise, it is impossible
else:
print("-1")
# Driver Code
# Given string
str = "SESNW"
# Given source and destination
x1 = 0
x2 = 1
y1 = 0
y2 = 1
# Function call
minimum_length(x1, y1, x2, y2, str)
# This code is contributed by Amit Katiyar
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum length
// of string required to reach from
// source to destination
static void minimum_length(int x1, int y1,
int x2, int y2,
string str)
{
// Size of the string
int n = str.Length;
// Stores the index of the four
// directions E, W, N, S
int pos1, pos2, pos3, pos4;
pos1 = -1;
pos2 = -1;
pos3 = -1;
pos4 = -1;
// If destination reached
if (x1 == x2 && y1 == y2)
{
Console.WriteLine("0");
}
// Iterate over the string
else
{
for(int i = 0; i < n; i++)
{
// Move east
if (x2 > x1)
{
// Change x1 according
// to direction E
if (str[i] == 'E')
{
x1 = x1 + 1;
if (x1 == x2)
{
pos1 = i;
}
}
}
// Move west
if (x2 < x1)
{
// Change x1 according
// to direction W
if (str[i] == 'W')
{
x1 = x1 - 1;
if (x1 == x2)
{
pos2 = i;
}
}
}
// Move north
if (y2 > y1)
{
// Change y1 according
// to direction N
if (str[i] == 'N')
{
y1 = y1 + 1;
if (y1 == y2)
{
pos3 = i;
}
}
}
// Move south
if (y2 < y1)
{
// Change y1 according
// to direction S
if (str[i] == 'S')
{
y1 = y1 - 1;
if (y1 == y2)
{
pos4 = i;
}
}
}
}
int z;
// Store the max of all positions
z = Math.Max(pos1,
Math.Max(Math.Max(pos2, pos3),
pos4));
// Print the minimum length of
// string required
if (x1 == x2 && y1 == y2)
{
Console.WriteLine(z + 1);
}
// Otherwise, it is impossible
else
{
Console.WriteLine("-1");
}
}
}
// Driver Code
public static void Main ()
{
// Given string
string str = "SESNW";
// Given source and destination
int x1 = 0, x2 = 1, y1 = 0, y2 = 1;
// Function call
minimum_length(x1, y1, x2, y2, str);
}
}
// This code is contributed by sanjoy_62
Javascript
4
时间复杂度: O(N)
辅助空间: O(1)