Dado un punto (x, y). Encuentra si es posible o no pasar de (0, 0) a (x, y) en exactamente n pasos. 4 tipos de pasos son válidos, puede moverse desde un punto (a, b) a cualquiera de (a, b+1), (a, b-1), (a-1, b), (a+1, b)
Ejemplos :
Input: x = 0, y = 0, n = 2 Output: POSSIBLE Input: x = 1, y = 1, n = 3 Output: IMPOSSIBLE
Enfoque :
En el camino más corto, uno puede pasar de (0, 0) a (x, y) en |x| + |y|. Entonces, no es posible pasar de (0, 0) a (x, y) en menos de |x| + |y| pasos. Después de llegar a uno, puede tomar dos pasos más como (x, y) -> (x, y+1) -> (x, y).
Entonces, es posible si
norte >= |x| + |y| y ( n-( |x| + |y| ) ) % 2 = 0.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps #include <bits/stdc++.h> using namespace std; // Function to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps bool Arrive(int a, int b, int n) { if (n >= abs(a) + abs(b) and (n - (abs(a) + abs(b))) % 2 == 0) return true; return false; } // Driver code int main() { int a = 5, b = 5, n = 11; if (Arrive(a, b, n)) cout << "Yes"; else cout << "No"; return 0; }
C
// C program to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps #include <stdio.h> #include <stdbool.h> int abs(int a) { int abs = a; if(abs < 0) abs = abs * (-1); return abs; } // Function to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps bool Arrive(int a, int b, int n) { if (n >= abs(a) + abs(b) && (n - (abs(a) + abs(b))) % 2 == 0) return true; return false; } // Driver code int main() { int a = 5, b = 5, n = 11; if (Arrive(a, b, n)) printf("Yes"); else printf("No"); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps import java.io.*; public class GFG { // Function to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps static boolean Arrive(int a, int b, int n) { if (n >= Math.abs(a) + Math.abs(b) && (n - (Math.abs(a) + Math.abs(b))) % 2 == 0) return true; return false; } // Driver code int main() { return 0; } public static void main (String[] args) { int a = 5, b = 5, n = 11; if (Arrive(a, b, n)) System.out.println( "Yes"); else System.out.println( "No"); } } //This code is contributed by shs..
Python3
# Python3 program to check whether # it is possible or not to move from # (0, 0) to (x, y) in exactly n steps # Function to check whether it is # possible or not to move from # (0, 0) to (x, y) in exactly n steps def Arrive(a, b, n): if (n >= abs(a) + abs(b) and (n - (abs(a) + abs(b))) % 2 == 0): return True return False # Driver code a = 5 b = 5 n = 11 if (Arrive(a, b, n)): print("Yes") else: print("No") # This code is contributed # by Yatin Gupta
C#
// C# program to check whether // it is possible or not to // move from (0, 0) to (x, y) // in exactly n steps using System; class GFG { // Function to check whether it // is possible or not to move // from (0, 0) to (x, y) in // exactly n steps static bool Arrive(int a, int b, int n) { if (n >= Math.Abs(a) + Math.Abs(b) && (n - (Math.Abs(a) + Math.Abs(b))) % 2 == 0) return true; return false; } // Driver code public static void Main () { int a = 5, b = 5, n = 11; if (Arrive(a, b, n)) Console.WriteLine( "Yes"); else Console.WriteLine( "No"); } } // This code is contributed by shashank
PHP
<?php // PHP program to check whether // it is possible or not to move // from (0, 0) to (x, y) in exactly n steps // Function to check whether it // is possible or not to move // from (0, 0) to (x, y) in exactly n steps function Arrive($a, $b, $n) { if ($n >= abs($a) + abs($b) and ($n - (abs($a) + abs($b))) % 2 == 0) return true; return false; } // Driver code $a = 5; $b = 5; $n = 11; if (Arrive($a, $b, $n)) echo "Yes"; else echo "No"; // This code is contributed // by Akanksha Rai(Abby_akku)
Javascript
<script> // JavaScript program to check whether it is // possible or not to move from (0, 0) to (x, y) // in exactly n steps // Function to check whether it is possible // or not to move from (0, 0) to (x, y) // in exactly n steps function Arrive(a, b, n) { if (n >= Math.abs(a) + Math.abs(b) && (n - (Math.abs(a) + Math.abs(b))) % 2 == 0) return true; return false; } // Driver code var a = 5, b = 5, n = 11; if (Arrive(a, b, n)) document.write("Yes"); else document.write("No"); // This code is contributed by Ankita saini </script>
No
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA