Givet ett 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.
Till exempel: Betrakta fallet med R = 3 C = 3. Vägen som följs kommer att vara (0 0) -- (0 1) -- (0 2) -- (1 2) -- (2 2) -- (2 1) -- (2 0) -- (1 0) -- (1 1). Vid det här laget har alla torg besökts och är vända åt höger.
Exempel:
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å detta problem är att göra den R x C-matris initialiserad med noll och korsa den i spiralform och ta en variabel 'Dir' som talar om den aktuella riktningen. När vi är i slutet av en rad och kolumn, ta Höger och ändra värdet på 'Dir' enligt din nuvarande riktning. Följ nu de angivna villkoren:
- Om du korsar översta raden är din nuvarande riktning Höger.
- Om du är högerkolumnen är din nuvarande riktning Nedåt.
- Om du korsar den nedre raden är din nuvarande riktning Vänster.
- Om du korsar vänster kolumn är din nuvarande riktning Upp.
När vi når den sista kvadraten skriv bara ut aktuell riktning dvs; värdet för variabeln 'Dir'.
Tid och rumskomplexitet för detta problem är O(R x C) och detta fungerar bara för små värden på RC men här är R och C för stora så att skapa R x C-matris är inte möjligt för för stora värden på R och C.
Effektivt tillvägagångssätt: Detta tillvägagångssätt kräver lite observation och en del pennpappersarbete. Här måste vi överväga alla möjliga fall för R och C så behöver vi bara sätta IF-villkor för alla möjliga fall. Här är vi med alla möjliga förutsättningar:
- R != C och R är jämnt och C är udda och R
- R != C och R är udda och C är jämn och R
- R != C och R är jämn och C är jämn och R
- R != C och R är udda och C är udda och R
- R != C och R är jämnt och C är udda och R>C-riktningen kommer att vara nedåt.
- R != C och R är udda och C är jämnt och R>C-riktningen kommer att vara Upp.
- R != C och R är jämn och C är jämn och R>C-riktningen kommer att vara uppåt.
- R != C och R är udda och C är udda och R>C-riktningen kommer att vara nedåt.
- R == C och R är jämn och C är jämn riktning kommer att vara vänster.
- R == C och R är udda och C är udda riktning kommer att vara Höger.
- R != C och R är udda och C är jämn och R
Nedan är implementeringen av ovanstående idé.
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('Leftn'); return; } if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) { printf('Upn'); return; } if (R == C && R % 2 != 0 && C % 2 != 0) { printf('Rightn'); return; } if (R == C && R % 2 == 0 && C % 2 == 0) { printf('Leftn'); return; } if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) { printf('Rightn'); return; } if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) { printf('Downn'); return; } if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) { printf('Leftn'); return; } if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) { printf('Upn');; return; } if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) { printf('Downn'); return; } if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) { printf('Rightn'); 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>
Produktion
Down
Tidskomplexitet: O(1)
Hjälputrymme: O(1)
Den här artikeln har granskats av teamet GeeksforGeeks.