📌  相关文章
📜  在二维平面中从给定源到目标的打印路径

📅  最后修改于: 2021-05-05 00:36:21             🧑  作者: Mango

给定源点(src x ,src y )和目标点(dst x ,dst y )的坐标任务是确定从源点到达目标点的可能路径。从任意点(x,y)来看只有两种有效的移动类型: (2 * x + y,y)(x,2 * y + x) 。如果该路径不存在,则打印-1
例子:

方法:想法是从每个点使用两次递归调用

  1. 在第一个递归调用中,将‘x’更新为(2 * x + y),y坐标不变。
  2. 在第二个递归调用中,将“ y”更新为(2 * y + x),并且x坐标不变。

如果当前坐标xy超过目标坐标,我们将终止递归调用。将路径存储在堆栈中,并在到达堆栈的目标打印元素之后
下面是上述方法的实现:

C++
// C++ program for printing a path from
// given source to destination
#include 
using namespace std;
 
// Function to print the path
void printExistPath(stack sx, stack sy, int last)
{
    // Base condition
    if (sx.empty() || sy.empty()) {
        return;
    }
    int x = sx.top();
    int y = sy.top();
     
    // Pop stores elements
    sx.pop();
    sy.pop();
     
    // Recursive call for printing stack
    // In reverse order
    printExistPath(sx, sy, last);
    if (sx.size() == last - 1) {
 
        cout << "(" << x << ", " << y << ")";
    }
    else {
        cout << "(" << x << ", " << y << ") -> ";
    }
}
 
// Function to store the path into
// The stack, if path exist
bool storePath(int srcX, int srcY, int destX, int destY,
            stack& sx, stack& sy)
{
    // Base condition
    if (srcX > destX || srcY > destY) {
        return false;
    }
     
    // Push current elements
    sx.push(srcX);
    sy.push(srcY);
     
    // Condition to check whether reach to the
    // Destination or not
    if (srcX == destX && srcY == destY) {
        printExistPath(sx, sy, sx.size());
        return true;
    }
     
    // Increament 'x' ordinate of source by (2*x+y)
    // Keeping 'y' constant
    if (storePath((2 * srcX) + srcY, srcY, destX, destY, sx, sy)) {
        return true;
    }
     
    // Increament 'y' ordinate of source by (2*y+x)
    // Keeping 'x' constant
    if (storePath(srcX, (2 * srcY) + srcX, destX, destY, sx, sy)) {
        return true;
    }
     
    // Pop current elements form stack
    sx.pop();
    sy.pop();
     
    // If no path exists
    return false;
}
 
// Utility function to check whether path exist or not
bool isPathExist(int srcX, int srcY, int destX, int destY)
{
    // To store x co-ordinate
    stack sx;
     
    // To store y co-ordinate
    stack sy;
     
    return storePath(srcX, srcY, destX, destY, sx, sy);
}
 
// Function to find the path
void printPath(int srcX, int srcY, int destX, int destY)
{
    if (!isPathExist(srcX, srcY, destX, destY))
    {
        // Print -1, if path doesn't exist
        cout << "-1";
    }
}
 
// Driver code
int main()
{
    int srcX = 5, srcY = 8;
     
    int destX = 83, destY = 21;
 
    // Function call
    printPath(srcX, srcY, destX, destY);
}


Java
// Java program for printing a path from
// given source to destination
import java.util.*;
 
class GFG{
 
// Function to print the path
static void printExistPath(Stack sx,
                Stack sy, int last)
{
    // Base condition
    if (sx.isEmpty() || sy.isEmpty()) {
        return;
    }
    int x = sx.peek();
    int y = sy.peek();
     
    // Pop stores elements
    sx.pop();
    sy.pop();
     
    // Recursive call for printing stack
    // In reverse order
    printExistPath(sx, sy, last);
    if (sx.size() == last - 1) {
 
        System.out.print("(" + x+ ", " + y+ ")");
    }
    else {
        System.out.print("(" + x+ ", " + y+ ")->");
    }
}
 
// Function to store the path into
// The stack, if path exist
static boolean storePath(int srcX, int srcY,
                        int destX, int destY,
            Stack sx, Stack sy)
{
    // Base condition
    if (srcX > destX || srcY > destY) {
        return false;
    }
     
    // Push current elements
    sx.add(srcX);
    sy.add(srcY);
     
    // Condition to check whether reach to the
    // Destination or not
    if (srcX == destX && srcY == destY) {
        printExistPath(sx, sy, sx.size());
        return true;
    }
     
    // Increament 'x' ordinate of source by (2*x+y)
    // Keeping 'y' constant
    if (storePath((2 * srcX) + srcY, srcY,
                    destX, destY, sx, sy))
    {
        return true;
    }
     
    // Increament 'y' ordinate of source by (2*y+x)
    // Keeping 'x' constant
    if (storePath(srcX, (2 * srcY) + srcX,
                    destX, destY, sx, sy))
    {
        return true;
    }
     
    // Pop current elements form stack
    sx.pop();
    sy.pop();
     
    // If no path exists
    return false;
}
 
// Utility function to check whether path exist or not
static boolean isPathExist(int srcX, int srcY,
                        int destX, int destY)
{
    // To store x co-ordinate
    Stack sx = new Stack();
     
    // To store y co-ordinate
    Stack sy = new Stack();
     
    return storePath(srcX, srcY, destX,
                    destY, sx, sy);
}
 
// Function to find the path
static void printPath(int srcX, int srcY,
                    int destX, int destY)
{
    if (!isPathExist(srcX, srcY, destX, destY))
    {
        // Print -1, if path doesn't exist
        System.out.print("-1");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int srcX = 5, srcY = 8;
     
    int destX = 83, destY = 21;
 
    // Function call
    printPath(srcX, srcY, destX, destY);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for printing a path from
# given source to destination
  
# Function to print the path
def printExistPath( sx,  sy, last):
 
    # Base condition
    if (len(sx) == 0 or len(sy) == 0):
        return
     
    x = sx[-1];
    y = sy[-1]
      
    # Pop stores elements
    sx.pop();
    sy.pop();
      
    # Recursive call for printing stack
    # In reverse order
    printExistPath(sx, sy, last);
     
    if (len(sx) == last - 1):
        print("(" + str(x) + ", " + str(y) + ")", end='')
 
    else:
        print("(" + str(x) + ", " + str(y) + ") -> ", end='')
         
# Function to store the path into
# The stack, if path exist
def storePath(srcX, srcY, destX, destY, sx,  sy):
 
    # Base condition
    if (srcX > destX or srcY > destY):
        return False;
      
    # Push current elements
    sx.append(srcX);
    sy.append(srcY);
      
    # Condition to check whether reach to the
    # Destination or not
    if (srcX == destX and srcY == destY):
        printExistPath(sx, sy, len(sx));
        return True;
     
    # Increament 'x' ordinate of source by (2*x+y)
    # Keeping 'y' constant
    if (storePath((2 * srcX) + srcY, srcY, destX, destY, sx, sy)):
        return True;
     
    # Increament 'y' ordinate of source by (2*y+x)
    # Keeping 'x' constant
    if (storePath(srcX, (2 * srcY) + srcX, destX, destY, sx, sy)):
        return True;
      
    # Pop current elements form stack
    sx.pop();
    sy.pop();
      
    # If no path exists
    return False;
 
# Utility function to check whether path exist or not
def isPathExist(srcX, srcY, destX, destY):
 
    # To store x co-ordinate
    sx = []
      
    # To store y co-ordinate
    sy = []
     
    return storePath(srcX, srcY, destX, destY, sx, sy);
 
# Function to find the path
def printPath(srcX, srcY, destX, destY):
 
    if (not isPathExist(srcX, srcY, destX, destY)):
     
        # Print -1, if path doesn't exist
        print("-1");
     
# Driver code
if __name__=='__main__':
     
    srcX = 5
    srcY = 8;
      
    destX = 83
    destY = 21;
  
    # Function call
    printPath(srcX, srcY, destX, destY);
 
    # This code is contributed by rutvik_56


C#
// C# program for printing a path from
// given source to destination
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to print the path
static void printExistPath(Stack sx,
                Stack sy, int last)
{
    // Base condition
    if (sx.Count == 0 || sy.Count == 0) {
        return;
    }
    int x = sx.Peek();
    int y = sy.Peek();
      
    // Pop stores elements
    sx.Pop();
    sy.Pop();
      
    // Recursive call for printing stack
    // In reverse order
    printExistPath(sx, sy, last);
    if (sx.Count == last - 1) {
  
        Console.Write("(" + x + ", " + y + ")");
    }
    else {
        Console.Write("(" + x + ", " + y + ")->");
    }
}
  
// Function to store the path into
// The stack, if path exist
static bool storePath(int srcX, int srcY,
                        int destX, int destY,
            Stack sx, Stack sy)
{
    // Base condition
    if (srcX > destX || srcY > destY) {
        return false;
    }
      
    // Push current elements
    sx.Push(srcX);
    sy.Push(srcY);
      
    // Condition to check whether reach to the
    // Destination or not
    if (srcX == destX && srcY == destY) {
        printExistPath(sx, sy, sx.Count);
        return true;
    }
      
    // Increament 'x' ordinate of source by (2*x+y)
    // Keeping 'y' constant
    if (storePath((2 * srcX) + srcY, srcY,
                    destX, destY, sx, sy))
    {
        return true;
    }
      
    // Increament 'y' ordinate of source by (2*y+x)
    // Keeping 'x' constant
    if (storePath(srcX, (2 * srcY) + srcX,
                    destX, destY, sx, sy))
    {
        return true;
    }
      
    // Pop current elements form stack
    sx.Pop();
    sy.Pop();
      
    // If no path exists
    return false;
}
  
// Utility function to check whether path exist or not
static bool isPathExist(int srcX, int srcY,
                        int destX, int destY)
{
    // To store x co-ordinate
    Stack sx = new Stack();
      
    // To store y co-ordinate
    Stack sy = new Stack();
      
    return storePath(srcX, srcY, destX,
                    destY, sx, sy);
}
  
// Function to find the path
static void printPath(int srcX, int srcY,
                    int destX, int destY)
{
    if (!isPathExist(srcX, srcY, destX, destY))
    {
        // Print -1, if path doesn't exist
        Console.Write("-1");
    }
}
  
// Driver code
public static void Main(String[] args)
{
    int srcX = 5, srcY = 8;
      
    int destX = 83, destY = 21;
  
    // Function call
    printPath(srcX, srcY, destX, destY);
}
}
 
// This code is contributed by 29AjayKumar


输出:
(5, 8) -> (5, 21) -> (31, 21) -> (83, 21)