📌  相关文章
📜  检查在给定方向移动后是否可以返回到起始位置

📅  最后修改于: 2021-10-26 06:33:31             🧑  作者: Mango

给定一个人行进的具有 N 个方向的字符串S。任务是检查他/她是否能够回到他/她开始的同一个地方。在第 i(1 <= i <= N) 天,他将向以下方向移动正距离:

如果他能在第 n 天后回到他开始的地方,打印“YES”,否则打印“NO”。
例子:

方法: N 的数量必须与 S 的数量相同,E 的数量必须与 W 的数量相同。因此,计算给定的每种类型的方向,然后检查它们是否相等。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include
using namespace std;
 
int main()
    {
        string st = "NNNWEWESSS" ;
        int len = st.length();
 
        int n = 0 ; // Count of North
        int s = 0 ; // Count of South
        int e = 0 ; // Count of East
        int w = 0 ; // Count of West
 
        for (int i = 0; i < len ; i++ )
        {
            if(st[i]=='N')
                n += 1;
            if(st[i] == 'S')
                s += 1;
            if(st[i] == 'W')
                w+= 1 ;
            if(st[i] == 'E')
                e+= 1 ;
        }
         
        if(n == s && w == e)
            cout<<("YES")<


Java
// Java implementation of above approach
 
public class GFG {
     
    public static void main(String args[])
    {
                String st = "NNNWEWESSS" ;
                int len = st.length();
                 
                int n = 0 ; // Count of North
                int s = 0 ; // Count of South
                int e = 0 ; // Count of East
                int w = 0 ; // Count of West
                 
                for (int i = 0; i < len ; i++ )
                {
                    if(st.charAt(i)=='N')
                        n+= 1 ;
                    if(st.charAt(i) == 'S')
                        s+= 1 ;
                    if(st.charAt(i) == 'W')
                        w+= 1 ;
                    if(st.charAt(i) == 'E')
                        e+= 1 ;
                }
                if(n == s && w == e)
                    System.out.println("YES");
                else
                    System.out.println("NO") ;
       
    }
    // This Code is contributed by ANKITRAI1
}


Python
# Python implementation of above approach
 
st = "NNNWEWESSS"
length = len(st)
n = 0 # Count of North
s = 0 # Count of South
e = 0 # Count of East
w = 0 # Count of West
for i in range(length):
    if(st[i]=="N"):
        n+= 1
    if(st[i]=="S"):
        s+= 1
    if(st[i]=="W"):
        w+= 1
    if(st[i]=="E"):
        e+= 1
if(n == s and w == e):
    print("YES")
else:
    print("NO")


C#
// C# implementation of above approach
using System;
 
class GFG {
     
    // Main Method
    public static void Main()
    {
         
        string st = "NNNWEWESSS" ;
        int len = st.Length;
         
        int n = 0 ; // Count of North
        int s = 0 ; // Count of South
        int e = 0 ; // Count of East
        int w = 0 ; // Count of West
         
        for (int i = 0; i < len ; i++ )
        {
            if(st[i]=='N')
                n += 1 ;
            if(st[i] == 'S')
                s += 1 ;
            if(st[i] == 'W')
                w += 1 ;
            if(st[i] == 'E')
                e += 1 ;
        }
         
        if(n == s && w == e)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO") ;
         
    }
 
}
 
// This code is contributed by Subhadeep


PHP


Javascript


输出:
YES

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程