原文:

给定源点 (src x 、src y ) 和目的点 (dst x 、dst y ) 的坐标,任务是确定从源点到达目的点的可能路径。从任何一点来看 (x,y) 只有两种有效招式: (2 * x y,y)(x,2 * y x) 。如果路径不存在,则打印 -1例:

输入:【src x 、src = { 5.8 },(dst x 、dst = { 83.21 }

方法:想法是使用从每个点 进行两次调用

  1. 在第一次递归调用中,通过 (2 * x y) 更新‘x’,并且不改变 y 坐标。
  2. 在第二次递归调用中,通过 (2 * y x) 更新‘y’,并且不改变 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;
    }
    // increment 'x' ordinate of source by (2*x y)
    // keeping 'y' constant
    if (storepath((2 * srcx)   srcy, srcy, destx, desty, sx, sy)) {
        return true;
    }
    // increment '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;
    }
    // increment 'x' ordinate of source by (2*x y)
    // keeping 'y' constant
    if (storepath((2 * srcx)   srcy, srcy,
                    destx, desty, sx, sy))
    {
        return true;
    }
    // increment '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

python 3

# 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;
    # increment 'x' ordinate of source by (2*x y)
    # keeping 'y' constant
    if (storepath((2 * srcx)   srcy, srcy, destx, desty, sx, sy)):
        return true;
    # increment '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;
    }
    // increment 'x' ordinate of source by (2*x y)
    // keeping 'y' constant
    if (storepath((2 * srcx)   srcy, srcy,
                    destx, desty, sx, sy))
    {
        return true;
    }
    // increment '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

java 描述语言


output: 

(5, 8) -> (5, 21) -> (31, 21) -> (83, 21)