Étant donné un 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.
Par exemple : Considérons le cas avec R = 3 C = 3. Le chemin suivi sera (0 0) -- (0 1) -- (0 2) -- (1 2) -- (2 2) -- (2 1) -- (2 0) -- (1 0) -- (1 1). À ce stade, toutes les places ont été visitées et font face à droite.
Exemples :
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
Solution simple : Une solution simple à ce problème consiste à en faire une matrice R x C initialisée à zéro, à la parcourir sous forme de spirale et à prendre une variable « Dir » qui indique la direction actuelle. Chaque fois que nous sommes à la fin d'une ligne ou d'une colonne, prenez à droite et modifiez la valeur de « Dir » en fonction de votre direction actuelle. Suivez maintenant les conditions données :
- Si vous traversez la rangée supérieure, votre direction actuelle est la droite.
- Si vous êtes dans la colonne de droite, votre direction actuelle est vers le bas.
- Si vous traversez la rangée du bas, votre direction actuelle est la gauche.
- Si vous traversez la colonne de gauche, votre direction actuelle est vers le haut.
Lorsque nous atteignons le dernier carré, imprimons simplement la direction actuelle, c'est-à-dire : valeur de la variable 'Dir'.
La complexité temporelle et spatiale de ce problème est O(R x C) et cela ne fonctionnera que pour de petites valeurs de R C mais ici R et C sont trop grands, donc la création d'une matrice R x C n'est pas possible pour des valeurs trop grandes de R et C.
Approche efficace : Cette approche nécessite peu d’observation et un peu de travail sur papier. Ici, nous devons considérer tous les cas possibles pour R et C, puis il nous suffit de mettre la condition IF pour tous les cas possibles. Nous voici avec toutes les conditions possibles :
- R != C et R est pair et C est impair et R
- R != C et R est impair et C est pair et R
- R != C et R est pair et C est pair et R
- R != C et R est impair et C est impair et R
- R != C et R est pair et C est impair et la direction R>C sera vers le bas.
- R != C et R est impair et C est pair et la direction R>C sera vers le haut.
- R != C et R est pair et C est pair et la direction R>C sera vers le haut.
- R != C et R est impair et C est impair et la direction R>C sera vers le bas.
- R == C et R est pair et C est pair, la direction sera Gauche.
- R == C et R est impair et C est impair, la direction sera droite.
- R != C et R est impair et C est pair et R
Vous trouverez ci-dessous la mise en œuvre de l’idée ci-dessus.
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>
Sortir
Down
Complexité temporelle : O(1)
Espace auxiliaire : O(1)
Cet article est révisé par l'équipe GeeksforGeeks.