📌  相关文章
📜  最小化从给定源到达目的地所需的字符串定义的步骤

📅  最后修改于: 2021-09-06 06:25:30             🧑  作者: Mango

给定一个字符串str和四个整数X 1 , Y 1 , X 2Y 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] ,可以跳过。但是跳过的字符会增加使用的步骤。例子

方法:这个想法是遍历字符串并执行移动直到到达目的地。以下是步骤:

  1. 初始化四个变量pos1、pos2、pos3pos4分别存储E、W、N、S的位置。
  2. 现在,检查(x1, y1)是否等于(x2, y2)那么当前位置已经是目的地所以打印0
  3. 否则,遍历字符串并检查以下四个条件:
    • 如果x2 > x1则迭代直到出现‘E’并更新pos1中的索引。
    • 如果x2 < x1则迭代直到‘W’出现并更新pos2中的索引。
    • 如果y2 > y1则迭代直到出现‘N’并更新pos3中的索引。
    • 如果y2 < y1则迭代直到出现‘S’并更新pos4中的索引。
  4. 执行上述操作后检查是否x1 != x2y1 != y2然后打印“-1” ,表示不可能到达目的地。
  5. 否则,找到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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live