text: Check if a sequence of path visits any coordinate twice or not | C # program for the above approach ; Function to check if the man crosses previous visited coordinate or not ; Stores the count of crossed vertex ; Stores ( x , y ) coordinates ; The coordinates for the origin ; Iterate over the String ; Condition to increment X or Y co - ordinates respectively ; Check if ( x , y ) is already visited ; Print the result ; Driver Code ; Given String ; Function call
code: using System ; using System . Collections . Generic ; class GFG { static void isCrossed ( String path ) { if ( path . Length == 0 ) return ; bool ans = false ; HashSet < KeyValuePair < int , int > > mySet = new HashSet < KeyValuePair < int , int > > ( ) ; int x = 0 , y = 0 ; mySet . Add ( new KeyValuePair < int , int > ( x , y ) ) ; for ( int i = 0 ; i < path . Length ; i ++ ) { if ( path [ i ] == ' N ' ) mySet . Add ( new KeyValuePair < int , int > ( x , y ++ ) ) ; if ( path [ i ] == ' S ' ) mySet . Add ( new KeyValuePair < int , int > ( x , y -- ) ) ; if ( path [ i ] == ' E ' ) mySet . Add ( new KeyValuePair < int , int > ( x ++ , y ) ) ; if ( path [ i ] == ' W ' ) mySet . Add ( new KeyValuePair < int , int > ( x -- , y ) ) ; if ( mySet . Contains ( new KeyValuePair < int , int > ( x , y ) ) ) { ans = true ; break ; } } if ( ans ) Console . Write ( " Crossed " ) ; else Console . Write ( " Not ▁ Crossed " ) ; } public static void Main ( String [ ] args ) { String path = " NESW " ; isCrossed ( path ) ; } }