Retning ved siste kvadratiske blokk

Gitt en 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.

For eksempel: Tenk på tilfellet med R = 3 C = 3. Banen som følges vil være (0 0) -- (0 1) -- (0 2) -- (1 2) -- (2 2) -- (2 1) -- (2 0) -- (1 0) -- (1 1). På dette tidspunktet er alle plassene besøkt og vender mot høyre. 

Eksempler:  

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 

Enkel løsning: En enkel løsning på dette problemet er å gjøre den R x C-matrisen initialisert med null og krysse den i spiralform og ta en variabel 'Dir' som forteller gjeldende retning. Når vi er på slutten av en rad og kolonne, ta til høyre og endre verdien av 'Dir' i henhold til din nåværende retning. Følg nå de gitte betingelsene: 

  • Hvis du krysser øverste rad, er din nåværende retning Høyre.
  • Hvis du er høyre kolonne, er din nåværende retning Ned.
  • Hvis du krysser nederste rad, er gjeldende retning Venstre.
  • Hvis du krysser venstre kolonne, er gjeldende retning Opp.

Når vi kommer til den siste ruten er det bare å skrive ut gjeldende retning dvs.; verdien av 'Dir'-variabelen. 
Tids- og romkompleksiteten for dette problemet er O(R x C), og dette vil bare fungere for små verdier av RC, men her er R og C for store, så å lage R x C-matrise er ikke mulig for for store verdier av R og C.

Effektiv tilnærming: Denne tilnærmingen krever lite observasjon og litt pennepapirarbeid. Her må vi vurdere alle mulige tilfeller for R og C, så trenger vi bare å sette IF condition for alle mulige tilfeller. Her er vi med alle mulige forhold: 

  1. R != C og R er partall og C er oddetall og R
  2. R != C og R er oddetall og C er partall og R
  3. R != C og R er partall og C er partall og R
  4. R != C og R er oddetall og C er oddetall og R
  5. R != C og R er partall og C er oddetall og R>C retning vil være Ned.
  6. R != C og R er oddetall og C er partall og R>C retning vil være opp.
  7. R != C og R er partall og C er partall og R>C retning vil være opp.
  8. R != C og R er oddetall og C er oddetall og R>C retning vil være Ned.
  9. R == C og R er partall og C er partall retning vil være venstre.
  10. R == C og R er oddetall og C er oddetall retning vil være Høyre.

Nedenfor er implementering av ideen ovenfor. 

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>   

Produksjon
Down 

Tidskompleksitet: O(1) 
Hjelpeområde: O(1)

Denne artikkelen er anmeldt av teamet GeeksforGeeks.