原文:

给定一个包含字母 a-z 的屏幕,我们可以使用遥控器从一个字符切换到另一个字符。遥控器包含左、右、上、下键。 找到最短的可能路径,使用遥控器键入给定字符串的所有字符。初始位置在左上角,输入字符串的所有字符都应该按顺序打印。 屏幕:

a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z

例:

input: “geek”
output: 
move down
move right
press ok
move up
move right
move right
move right
press ok
press ok
move left
move left
move left
move left
move down
move down
press ok

这个想法是将屏幕视为人物的 2d 矩阵。然后我们逐个考虑给定字符串的所有字符,打印出当前字符和矩阵中下一个字符之间的最短路径。为了找到最短路径,我们考虑矩阵中当前字符和下一个字符的坐标。根据当前和下一个角色坐标的 x 和 y 值之间的差异,我们向左、向右、向上或向下移动。即

if row difference is negative, we move up
if row difference is positive, we move down
if column difference is negative, we go left
if column difference is positive, we go right

以下是上述想法的实现

c

// c   program to print shortest possible path to
// type all characters of given string using a remote
#include 
using namespace std;
// function to print shortest possible path to
// type all characters of given string using a remote
void printpath(string str)
{
    int i = 0;
    // start from character 'a' present at position (0, 0)
    int curx = 0, cury = 0;
    while (i < str.length())
    {
        // find coordinates of next character
        int nextx = (str[i] - 'a') / 5;
        int nexty = (str[i] - 'b'   1) % 5;
        // move up if destination is above
        while (curx > nextx)
        {
            cout << "move up" << endl;
            curx--;
        }
        // move left if destination is to the left
        while (cury > nexty)
        {
            cout << "move left" << endl;
            cury--;
        }
        // move down if destination is below
        while (curx < nextx)
        {
            cout << "move down" << endl;
            curx  ;
        }
        // move right if destination is to the right
        while (cury < nexty)
        {
            cout << "move right" << endl;
            cury  ;
        }
        // at this point, destination is reached
        cout << "press ok" << endl;
        i  ;
    }
}
// driver code
int main()
{
    string str = "cozy";
    printpath(str);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print shortest possible path to
// type all characters of given string using a remote
class gfg
{
    // function to print shortest possible path to
    // type all characters of given string using a remote
    static void printpath(string str)
    {
        int i = 0;
        // start from character 'a' present at position (0, 0)
        int curx = 0, cury = 0;
        while (i < str.length())
        {
            // find coordinates of next character
            int nextx = (str.charat(i) - 'a') / 5;
            int nexty = (str.charat(i) - 'b'   1) % 5;
            // move up if destination is above
            while (curx > nextx)
            {
                system.out.println("move up");
                curx--;
            }
            // move left if destination is to the left
            while (cury > nexty)
            {
                system.out.println("move left");
                cury--;
            }
            // move down if destination is below
            while (curx < nextx)
            {
                system.out.println("move down");
                curx  ;
            }
            // move right if destination is to the right
            while (cury < nexty)
            {
                system.out.println("move right");
                cury  ;
            }
            // at this point, destination is reached
            system.out.println("press ok");
            i  ;
        }
    }
    // driver program
    public static void main (string[] args)
    {
        string str = "cozy";
        printpath(str);
    }
}
// contributed by pramod kumar

python 3

# python 3 program to print shortest possible
# path to type all characters of given string
# using a remote
# function to print shortest possible path
# to type all characters of given string
# using a remote
def printpath(str):
    i = 0
    # start from character 'a' present
    # at position (0, 0)
    curx = 0
    cury = 0
    while (i < len(str)):
        # find coordinates of next character
        nextx = int((ord(str[i]) - ord('a')) / 5)
        nexty = (ord(str[i]) - ord('b')   1) % 5
        # move up if destination is above
        while (curx > nextx):
            print("move up")
            curx -= 1
        # move left if destination is to the left
        while (cury > nexty):
            print("move left")
            cury -= 1
        # move down if destination is below
        while (curx < nextx):
            print("move down")
            curx  = 1
        # move right if destination is to the right
        while (cury < nexty):
            print("move right")
            cury  = 1
        # at this point, destination is reached
        print("press ok")
        i  = 1
# driver code
if __name__ == '__main__':
    str = "cozy"
    printpath(str)
# this code is contributed by
# sanjit_prasad

c

// c# program to print shortest
// possible path to type all
// characters of given string
// using a remote
using system;
class gfg {
    // function to print shortest
    // possible path to type all
    // characters of given string
    // using a remote
    static void printpath(string str)
    {
        int i = 0;
        // start from character 'a'
        // present at position (0, 0)
        int curx = 0, cury = 0;
        while (i < str.length)
        {
            // find coordinates of
            // next character
            int nextx = (str[i] - 'a') / 5;
            int nexty = (str[i] - 'b'   1) % 5;
            // move up if destination
            // is above
            while (curx > nextx)
            {
                console.writeline("move up");
                curx--;
            }
            // move left if destination
            // is to the left
            while (cury > nexty)
            {
                console.writeline("move left");
                cury--;
            }
            // move down if destination
            // is below
            while (curx < nextx)
            {
                console.writeline("move down");
                curx  ;
            }
            // move right if destination
            // is to the right
            while (cury < nexty)
            {
                console.writeline("move right");
                cury  ;
            }
            // at this point, destination
            // is reached
            console.writeline("press ok");
            i  ;
        }
    }
    // driver code
    public static void main ()
    {
        string str = "cozy";
        printpath(str);
    }
}
// this code is contributed by nitin mittal.

服务器端编程语言(professional hypertext preprocessor 的缩写)

 $nextx)
        {
            echo "move up\n";
            $curx--;
        }
        // move left if destination is to the left
        while ($cury > $nexty)
        {
            echo "move left\n";
            $cury--;
        }
        // move down if destination is below
        while ($curx < $nextx)
        {
            echo "move down\n";
            $curx  ;
        }
        // move right if destination is to the right
        while ($cury < $nexty)
        {
            echo "move right\n";
            $cury  ;
        }
        // at this point, destination is reached
        echo "press ok\n";
        $i  ;
    }
}
// driver code
    $str = "cozy";
    printpath($str);
// this code is contributed by chandan_jnu
?>

java 描述语言


输出:

move right
move right
press ok
move down
move down
move right
move right
press ok
move left
move left
move left
move left
move down
move down
move down
press ok
move up
move right
move right
move right
move right
press ok

本文由阿迪蒂亚·戈尔供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果您发现任何不正确的地方,请写评论,或者您想分享更多关于上面讨论的主题的信息