Richtung beim letzten quadratischen Block

Gegeben sei ein R x C (1 <= R C <= 1000000000) grid and initial position as top left corner and direction as east. Now we start running in forward direction and cross each square blocks of matrix. Whenever we find dead end or reach a cell that is already visited we take right because we can not cross the visited square blocks again. Tell the direction when we will be at last square block.

Zum Beispiel : Betrachten Sie den Fall mit R = 3 C = 3. Der verfolgte Pfad ist (0 0) – (0 1) – (0 2) – (1 2) – (2 2) – (2 1) – (2 0) – (1 0) – (1 1). Zu diesem Zeitpunkt sind alle Plätze besucht und die Ausrichtung ist nach rechts gerichtet. 

Beispiele:  

Input : R = 1 C = 1 Output : Right Input : R = 2 C = 2 Output : Left Input : R = 3 C = 1 Output : Down Input : R = 3 C = 3 Output : Right 

Einfache Lösung: Eine einfache Lösung für dieses Problem besteht darin, die R x C-Matrix mit Null zu initialisieren, sie spiralförmig zu durchlaufen und eine Variable „Dir“ zu verwenden, die die aktuelle Richtung angibt. Immer wenn wir am Ende einer Zeile oder Spalte sind, gehen Sie nach rechts und ändern Sie den Wert von „Dir“ entsprechend Ihrer aktuellen Richtung. Befolgen Sie nun die vorgegebenen Bedingungen: 

  • Wenn Sie die oberste Reihe durchqueren, ist Ihre aktuelle Richtung rechts.
  • Wenn Sie sich in der rechten Spalte befinden, ist Ihre aktuelle Richtung Abwärts.
  • Wenn Sie die unterste Reihe durchqueren, ist Ihre aktuelle Richtung links.
  • Wenn Sie die linke Spalte durchqueren, ist Ihre aktuelle Richtung Aufwärts.

Wenn wir das letzte Quadrat erreicht haben, geben Sie einfach die aktuelle Richtung aus, d. h. Wert der Variable „Dir“. 
Die zeitliche und räumliche Komplexität für dieses Problem beträgt O(R x C) und dies funktioniert nur für kleine Werte von R C, aber hier sind R und C zu groß, sodass die Erstellung einer R x C-Matrix für zu große Werte von R und C nicht möglich ist.

Effizienter Ansatz: Dieser Ansatz erfordert wenig Beobachtung und etwas Schreibarbeit. Hier müssen wir alle möglichen Fälle für R und C berücksichtigen und dann nur noch die IF-Bedingung für alle möglichen Fälle festlegen. Hier sind wir mit allen möglichen Bedingungen: 

  1. R != C und R ist gerade und C ist ungerade und R
  2. R != C und R ist ungerade und C ist gerade und R
  3. R != C und R ist gerade und C ist gerade und R
  4. R != C und R ist ungerade und C ist ungerade und R
  5. R != C und R ist gerade und C ist ungerade und die R>C-Richtung ist nach unten.
  6. R != C und R ist ungerade und C ist gerade und die R>C-Richtung ist nach oben.
  7. R != C und R ist gerade und C ist gerade und die R>C-Richtung ist nach oben.
  8. R != C und R ist ungerade und C ist ungerade und die R>C-Richtung ist nach unten.
  9. R == C und R ist gerade und C ist gerade. Die Richtung ist links.
  10. R == C und R ist ungerade und C ist ungerade. Die Richtung ist rechts.

Nachfolgend finden Sie die Umsetzung der obigen Idee. 

C++
   // C++ program to tell the Current direction in   // R x C grid   #include          using     namespace     std  ;   typedef     long     long     int     ll  ;   // Function which tells the Current direction   void     direction  (  ll     R       ll     C  )   {      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      cout      < <     'Left'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      cout      < <     'Up'      < <     endl  ;      return  ;      }      if     (  R     ==     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0  )     {      cout      < <     'Right'      < <     endl  ;      return  ;      }      if     (  R     ==     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0  )     {      cout      < <     'Left'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      cout      < <     'Right'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      cout      < <     'Down'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      cout      < <     'Left'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      cout      < <     'Up'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      cout      < <     'Down'      < <     endl  ;      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      cout      < <     'Right'      < <     endl  ;      return  ;      }   }   // Driver program to test the Cases   int     main  ()   {      ll     R     =     3       C     =     1  ;      direction  (  R       C  );      return     0  ;   }   
C
   // C program to tell the Current direction in   // R x C grid   #include         typedef     long     long     int     ll  ;   // Function which tells the Current direction   void     direction  (  ll     R       ll     C  )   {      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      printf  (  'Left  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      printf  (  'Up  n  '  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0  )     {      printf  (  'Right  n  '  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0  )     {      printf  (  'Left  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      printf  (  'Right  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      printf  (  'Down  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      printf  (  'Left  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      printf  (  'Up  n  '  );;      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      printf  (  'Down  n  '  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      printf  (  'Right  n  '  );      return  ;      }   }   // Driver program to test the Cases   int     main  ()   {      ll     R     =     3       C     =     1  ;      direction  (  R       C  );      return     0  ;   }   // This code is contributed by kothavvsaakash.   
Java
   // Java program to tell the Current direction in   // R x C grid   import     java.io.*  ;   class   GFG     {      // Function which tells the Current direction       static     void     direction  (  int     R       int     C  )      {      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      System  .  out  .  println  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      System  .  out  .  println  (  'Up'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0  )     {      System  .  out  .  println  (  'Right'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0  )     {      System  .  out  .  println  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R      <     C  )     {      System  .  out  .  println  (  'Right'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      System  .  out  .  println  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      System  .  out  .  println  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     ==     0     &&     R     >     C  )     {      System  .  out  .  println  (  'Up'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&     C     %     2     !=     0     &&     R     >     C  )     {      System  .  out  .  println  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&     C     %     2     ==     0     &&     R      <     C  )     {      System  .  out  .  println  (  'Right'  );      return  ;      }      }      // Driver code      public     static     void     main  (  String  []     args  )      {      int     R     =     3       C     =     1  ;          direction  (  R       C  );      }   }   // This code is contributed by KRV.   
Python3
   # Python3 program to tell the Current    # direction in R x C grid   # Function which tells the Current direction   def   direction  (  R     C  ):   if   (  R   !=   C   and   R   %   2   ==   0   and   C   %   2   !=   0   and   R    <   C  ):   print  (  'Left'  )   return   if   (  R   !=   C   and   R   %   2   ==   0   and   C   %   2   ==   0   and   R   >   C  ):   print  (  'Up'  )   return   if   R   ==   C   and   R   %   2   !=   0   and   C   %   2   !=   0  :   print  (  'Right'  )   return   if   R   ==   C   and   R   %   2   ==   0   and   C   %   2   ==   0  :   print  (  'Left'  )   return   if   (  R   !=   C   and   R   %   2   !=   0   and   C   %   2   !=   0   and   R    <   C  ):   print  (  'Right'  )   return   if   (  R   !=   C   and   R   %   2   !=   0   and   C   %   2   !=   0   and   R   >   C  ):   print  (  'Down'  )   return   if   (  R   !=   C   and   R   %   2   ==   0   and   C   %   2   !=   0   and   R    <   C  ):   print  (  'Left'  )   return   if   (  R   !=   C   and   R   %   2   ==   0   and   C   %   2   ==   0   and   R   >   C  ):   print  (  'Up'  )   return   if   (  R   !=   C   and   R   %   2   !=   0   and   C   %   2   !=   0   and   R   >   C  ):   print  (  'Down'  )   return   if   (  R   !=   C   and   R   %   2   !=   0   and   C   %   2   !=   0   and   R    <   C  ):   print  (  'Right'  )   return   # Driver code   R   =   3  ;   C   =   1   direction  (  R     C  )   # This code is contributed by Shrikant13   
C#
   // C# program to tell the Current   // direction in R x C grid   using     System  ;   class     GFG   {          // Function which tells       // the Current direction       static     void     direction  (  int     R       int     C  )      {      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     !=     0     &&     R      <     C  )         {      Console  .  WriteLine  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     ==     0     &&     R     >     C  )         {      Console  .  WriteLine  (  'Up'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     !=     0     &&         C     %     2     !=     0  )         {      Console  .  WriteLine  (  'Right'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     ==     0     &&         C     %     2     ==     0  )         {      Console  .  WriteLine  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&      C     %     2     !=     0     &&     R      <     C  )         {      Console  .  WriteLine  (  'Right'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     !=     0     &&     R     >     C  )         {      Console  .  WriteLine  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     ==     0     &&     R      <     C  )         {      Console  .  WriteLine  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&      C     %     2     ==     0     &&     R     >     C  )         {      Console  .  WriteLine  (  'Up'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     !=     0     &&     R     >     C  )         {      Console  .  WriteLine  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     ==     0     &&     R      <     C  )         {      Console  .  WriteLine  (  'Right'  );      return  ;      }      }      // Driver code      static     public     void     Main     ()      {      int     R     =     3       C     =     1  ;          direction  (  R       C  );      }   }   // This code is contributed by m_kit   
PHP
      // PHP program to tell the Current    // direction in R x C grid   // Function which tells   // the Current direction   function   direction  (  $R     $C  )   {   if   (  $R   !=   $C   &&   $R   %   2   ==   0   &&   $C   %   2   !=   0   &&   $R    <   $C  )   {   echo   'Left'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   !=   0   &&   $C   %   2   ==   0   &&   $R   >   $C  )   {   echo   'Up'     '  n  '  ;   return  ;   }   if   (  $R   ==   $C   &&   $R   %   2   !=   0   &&   $C   %   2   !=   0  )   {   echo   'Right'     '  n  '  ;   return  ;   }   if   (  $R   ==   $C   &&   $R   %   2   ==   0   &&   $C   %   2   ==   0  )   {   echo   'Left'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   !=   0   &&   $C   %   2   !=   0   &&   $R    <   $C  )   {   echo   'Right'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   !=   0   &&   $C   %   2   !=   0   &&   $R   >   $C  )   {   echo   'Down'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   ==   0   &&   $C   %   2   ==   0   &&   $R    <   $C  )   {   echo   'Left'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   ==   0   &&   $C   %   2   ==   0   &&   $R   >   $C  )   {   echo   'Up'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   ==   0   &&   $C   %   2   !=   0   &&   $R   >   $C  )   {   echo   'Down'     '  n  '  ;   return  ;   }   if   (  $R   !=   $C   &&   $R   %   2   !=   0   &&   $C   %   2   ==   0   &&   $R    <   $C  )   {   echo   'Right'     '  n  '  ;   return  ;   }   }   // Driver Code   $R   =   3  ;   $C   =   1  ;   direction  (  $R     $C  );   // This code is contributed by aj_36   ?>   
JavaScript
    <  script  >      // Javascript program to tell the Current      // direction in R x C grid          // Function which tells       // the Current direction       function     direction  (  R       C  )      {      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     !=     0     &&     R      <     C  )         {      document  .  write  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     ==     0     &&     R     >     C  )         {      document  .  write  (  'Up'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     !=     0     &&         C     %     2     !=     0  )         {      document  .  write  (  'Right'  );      return  ;      }      if     (  R     ==     C     &&     R     %     2     ==     0     &&         C     %     2     ==     0  )         {      document  .  write  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     !=     0     &&     R      <     C  )         {      document  .  write  (  'Right'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&      C     %     2     !=     0     &&     R     >     C  )         {      document  .  write  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&      C     %     2     ==     0     &&     R      <     C  )         {      document  .  write  (  'Left'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     ==     0     &&     R     >     C  )         {      document  .  write  (  'Up'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     ==     0     &&         C     %     2     !=     0     &&     R     >     C  )         {      document  .  write  (  'Down'  );      return  ;      }      if     (  R     !=     C     &&     R     %     2     !=     0     &&         C     %     2     ==     0     &&     R      <     C  )         {      document  .  write  (  'Right'  );      return  ;      }      }          let     R     =     3       C     =     1  ;          direction  (  R       C  );        <  /script>   

Ausgabe
Down 

Zeitkomplexität: O(1) 
Hilfsraum: O(1)

Dieser Artikel wurde vom Team GeeksforGeeks überprüft.