Irány az utolsó négyzettömbnél

Adott egy 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.

Például: Tekintsük azt az esetet, amikor R = 3 C = 3. A követett út a következő lesz: (0 0) -- (0 1) -- (0 2) -- (1 2) -- (2 2) -- (2 1) -- (2 0) -- (1 0) -- (1 1). Ezen a ponton minden teret meglátogattak, és jobbra néz. 

Példák:  

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 

Egyszerű megoldás: Az egyik egyszerű megoldás erre a problémára az, hogy az R x C mátrixot nullával inicializáljuk, és spirális alakban bejárjuk, és veszünk egy „Dir” változót, amely megmondja az áram irányát. Amikor egy sor vagy oszlop végén vagyunk, forduljunk jobbra, és változtassuk meg a „Dir” értékét az aktuális iránynak megfelelően. Most kövesse a megadott feltételeket: 

  • Ha a felső sorban halad, akkor az aktuális irány a jobb.
  • Ha a jobb oldali oszlopban van, akkor az aktuális irány lefelé.
  • Ha az alsó sorban halad, akkor az aktuális irány balra.
  • Ha bal oldali oszlopban halad, akkor az aktuális irány felfelé.

Amikor elérjük az utolsó négyzetet, csak írja ki az aktuális irányt, azaz; a 'Dir' változó értéke. 
A probléma idő- és térbonyolultsága O(R x C), és ez csak kis R C érték esetén működik, de itt R és C túl nagyok, így az R x C mátrix létrehozása nem lehetséges túl nagy R és C értékekkel.

Hatékony megközelítés: Ez a megközelítés kevés megfigyelést és némi tollpapír-munkát igényel. Itt figyelembe kell vennünk az összes lehetséges esetet R és C esetén, majd csak IF feltételt kell feltenni az összes lehetséges esetre. Itt állunk az összes lehetséges feltétellel: 

  1. R != C és R páros, C pedig páratlan és R
  2. R != C és R páratlan és C páros és R
  3. R != C és R páros, C pedig páros és R
  4. R != C és R páratlan, C pedig páratlan és R
  5. R != C és R páros és C páratlan és R>C irány lefelé lesz.
  6. R != C és R páratlan és C páros és R>C irány felfelé lesz.
  7. R != C és R páros és C páros és R>C irány felfelé lesz.
  8. R != C és R páratlan és C páratlan és R>C irány lefelé lesz.
  9. R == C és R páros, C pedig páros irány balra lesz.
  10. R == C és R páratlan és C páratlan irány lesz jobb.

Alább látható a fenti ötlet megvalósítása. 

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>   

Kimenet
Down 

Időbonyolultság: O(1) 
Segédtér: O(1)

Ezt a cikket a GeeksforGeeks csapat értékelte.