关于Google挑战赛

博客包含多道编程模拟题和比赛题。模拟题涉及文本编辑光标位置模拟、简单绘图程序模拟;比赛题有寻找最长反转子串、在网格中查找单词路径数量等。还给出部分题目的代码实现,如光标位置和单词路径计数的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google挑战赛资格赛我参加了。就做对了一题,不过总算入围了。不知道下一次有没有这么好的运气。现在我贴出来挑战赛的模拟题与比赛题以及写的代码。

模拟题1

Problem Statement
    
When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.  You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.
Definition
    
Class:
CursorPosition
Method:
getPosition
Parameters:
String, int
Returns:
int
Method signature:
int getPosition(String keystrokes, int N)
(be sure your method is public)
    

Constraints
-
keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
-
N will be between 1 and 100, inclusive.
Examples
0)

    
"ERLLL"
10
Returns: 7
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
1)

    
"EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line.
2)

    
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3

3)

    
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12

代码:

public class CursorPosition<?XML:NAMESPACE PREFIX = O />

{

    public int getPosition(string keystrokes, int N)

    {

        if (keystrokes == null || keystrokes == "")

            return N;

 

        int nRet = N;

 

        for (int i = 0; i < keystrokes.Length; i++)

        {

            switch (keystrokes[i])

            {

                case 'L':

                    nRet--;

                    break;

                case 'R':

                    nRet++;

                    break;

                case 'H':

                    nRet = 0;

                    break;

                case 'E':

                    nRet = N;

                    break;

                default:

                    continue;

 

            }

 

            if (nRet < 0) nRet = 0;

            if (nRet > N) nRet = N;

        }

 

        return nRet;

    }

}

模拟题2

Problem Statement
    
A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. The upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19). You are given a string[], commands, each element of which contains one of two possible commands. A command of the form "FORWARD x" means that the cursor should move forward by x pixels. Each pixel on its path, including the start and end points, is painted black. The only other command is "LEFT", which means that the cursor should change its direction by 90 degrees counterclockwise. So, if the cursor is initially pointing straight down and it receives a single "LEFT" command, it will end up pointing straight to the right. Execute all the commands in order and return the resulting 20 x 20 pixel canvas as a string[] where character j of element i represents the pixel at (i, j). Black pixels should be represented as uppercase 'X' characters and blank pixels should be represented as '.' characters.
Definition
    
Class:
DrawLines
Method:
execute
Parameters:
string[]
Returns:
string[]
Method signature:
string[] execute(string[] commands)
(be sure your method is public)
    

Notes
-
The cursor only paints the canvas if it moves (see example 1).
Constraints
-
commands will contain between 1 and 50 elements, inclusive.
-
Each element of commands will be formatted as either "LEFT" or "FORWARD x" (quotes for clarity only), where x is an integer between 1 and 19, inclusive, with no extra leading zeros.
-
When executing the commands in order, the cursor will never leave the 20 x 20 pixel canvas.
Examples
0)

    
{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"}
Returns:
{"XXXXXXXXXXXXXXXXXXXX",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "XXXXXXXXXXXXXXXXXXXX" }
This sequence of commands draws a 20 x 20 outline of a square. The cursor is initially at (0, 0) pointing straight down. It then travels to (0, 19) after the first FORWARD command, painting each pixel along its path with a '*'. It then rotates 90 degrees left, travels to (19, 19), rotates 90 degrees left, travels to (19, 0), rotates 90 degrees left, and finally travels back to (0, 0).
1)

    
{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"}
Returns:
{"....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
The cursor spins round and round, but never actually paints any pixels. The result is an empty canvas.
2)

    
{"FORWARD 1"}
Returns:
{"X...................",
 "X...................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
Going forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted.
3)

    
{"LEFT", "FORWARD 19", "LEFT", "LEFT", "LEFT",
 "FORWARD 18", "LEFT", "LEFT", "LEFT", "FORWARD 17",
 "LEFT", "LEFT", "LEFT", "FORWARD 16", "LEFT",
 "LEFT", "LEFT", "FORWARD 15", "LEFT", "LEFT", "LEFT",
 "FORWARD 14", "LEFT", "LEFT", "LEFT", "FORWARD 13",
 "LEFT", "LEFT", "LEFT", "FORWARD 12", "LEFT", "LEFT",
 "LEFT", "FORWARD 11", "LEFT", "LEFT", "LEFT", "FORWARD 10",
 "LEFT", "LEFT", "LEFT", "FORWARD 9", "LEFT", "LEFT",
 "LEFT", "FORWARD 8", "LEFT", "LEFT", "LEFT", "FORWARD 7"}
Returns:
{"XXXXXXXXXXXXXXXXXXXX",
 "...................X",
 "..XXXXXXXXXXXXXXXX.X",
 "..X..............X.X",
 "..X.XXXXXXXXXXXX.X.X",
 "..X.X..........X.X.X",
 "..X.X.XXXXXXXX.X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.XXXXXXXXXX.X.X",
 "..X.X............X.X",
 "..X.XXXXXXXXXXXXXX.X",
 "..X................X",
 "..XXXXXXXXXXXXXXXXXX",
 "...................." }

代码:

public class DrawLines

{

 

    public string[] execute(string[] commands)

    {

        //down 0,left,1,up,2,right3

        const string FW = "FORWARD";

        const string L = "LEFT";

        char[,] lines = new char[20,20];

        for(int i=0;i<20;i++)

            for(int j=0;j<20;j++)

               lines[i, j] = '.';

 

        int row = 0, col = 0;

        int flag = 0;

        int[,] np = new int[4,2] { {1,0},{0,1},{-1,0},{0,-1} };

        for(int i=0;i<commands.Length;i++)

        {

            if(commands[i] == L)

            {

                flag = (flag + 1) % 4;

            }

 

            if(commands[i].StartsWith(FW) &&

                (commands[i].Length > 8 && commands[i].Length <11) )

            {

                string s = commands[i].Replace(FW, "").Trim();

                if (s == null || s == "")

                    continue;

                try

                {

                    int n = int.Parse(s);

                    for( int k=0;k<n;k++)

                    {

                        if (!(row < 0 || row >= 20 || col < 0 || col >= 20))

                            lines[row, col] = 'X';

                        row = row + np[flag, 0];

                        col = col + np[flag, 1];

 

                        if (!(row < 0 || row >= 20 || col < 0 || col >= 20))

                            lines[row, col] = 'X';

                    }

                   

                }

                catch(FormatException E)

                {

                    continue;

                }

            }

        }

 

        return print(lines,20);

       

    }

 

    public string[] print(char[,] lines,int n)

    {

        string os = "{";

        string[] dirs = new string[20];

        for(int i=0;i<n;i++)

        {

            if (i != 0)

                os += " ";

            os += "\"";

 

            dirs[i] = "";

            for(int j=0;j<n;j++)

            {

                os += lines[i, j];

                dirs[i] += lines[i, j];

            }

            os += "\",\n";

       }

 

       Console.WriteLine(os);

 

       return dirs;

    }

};

=======================================================

以下是今天我参加比赛的二题:

题1(250 points)

Problem Statement
    
You are given a String input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Definition
    
Class:
ReverseSubstring
Method:
findReversed
Parameters:
String
Returns:
String
Method signature:
String findReversed(String input)
(be sure your method is public)
    

Notes
-
The substring and its reversal may overlap partially or completely.
-
The entire original string is itself a valid substring (see example 4).
Constraints
-
input will contain between 1 and 50 characters, inclusive.
-
Each character of input will be an uppercase letter ('A'-'Z').
Examples
0)

    
"XBCDEFYWFEDCBZ"
Returns: "BCDEF"
We see that the reverse of BCDEF is FEDCB, which appears later in the string.
1)

    
"XYZ"
Returns: "X"
The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.
2)

    
"ABCABA"
Returns: "ABA"
The string ABA is a palindrome (it's its own reversal), so it meets the criteria.
3)

    
"FDASJKUREKJFDFASIREYUFDHSAJYIREWQ"
Returns: "FDF"

4)

    
"ABCDCBA"
Returns: "ABCDCBA"
Here, the entire string is its own reversal.

这一题不难,就不写代码了。

题2(750 points)

Problem Statement
    
You are given a String[] grid representing a rectangular grid of letters. You are also given a String find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
You are to return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000, return -1.
Definition
    
Class:
WordPath
Method:
countPaths
Parameters:
String[], String
Returns:
int
Method signature:
int countPaths(String[] grid, String find)
(be sure your method is public)
    

Constraints
-
grid will contain between 1 and 50 elements, inclusive.
-
Each element of grid will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
-
Each element of grid will contain the same number of characters.
-
find will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
Examples
0)

    
{"ABC",
 "FED",
 "GHI"}
"ABCDEFGHI"
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.
1)

    
{"ABC",
 "FED",
 "GAI"}
"ABCDEA"
Returns: 2
Once we get to the 'E', we can choose one of two directions for the final 'A'.
2)

    
{"ABC",
 "DEF",
 "GHI"}
"ABCD"
Returns: 0
We can trace a path for "ABC", but there's no way to complete a path to the letter 'D'.
3)

    
{"AA",
 "AA"}
"AAAA"
Returns: 108
We can start from any of the four locations. From each location, we can then move in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.
4)

    
{"ABABA",
 "BABAB",
 "ABABA",
 "BABAB",
 "ABABA"}
"ABABABBA"
Returns: 56448
There are a lot of ways to trace this path.
5)

    
{"AAAAA",
 "AAAAA",
 "AAAAA",
 "AAAAA",
 "AAAAA"}
"AAAAAAAAAAA"
Returns: -1
There are well over 1,000,000,000 paths that can be traced.
6)

    
{"AB",
 "CD"}
"AA"
Returns: 0
Since we can't stay on the same cell, we can't trace the path at all.

这一题我在规定的时间内没有完成。郁闷.......,后来写的代码如下
public class WordPath
{

    private string[,] m_Str = null;

    private int m_Count = 0;

    private int m_Dim = 0;

    private readonly int[,] m_V = new int[8, 2]{{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

   

    private void InitStrArray(string[] grid)

    {

        for (int i = 0; i < m_Dim; i++)

            for (int j = 0; j < m_Dim; j++)

                m_Str[i, j] = grid[i].Substring(j, 1);

    }

 

    public int countPaths(string[] grid, string find)

    {

        m_Dim = grid.Length;

        m_Str = new string[m_Dim,m_Dim];
        InitStrArray(grid);

        string start = find.Substring(0, 1);

 

        for (int i = 0; i < m_Dim; i++)

            for (int j = 0; j < m_Dim; j++)

            {

                if (m_Str[i, j] == start)

                {
                    if (m_Count > 1000000000) return -1;

                    getStrCountRecursion(i, j, find,m_Str[i, j]);

                }

            }

       

        return m_Count;

    }

 

    private void getStrCountRecursion(int x, int y,string find,string search)

    {

      

        int x1, y1;

        string sTmp = "";

        for(int i=0;i<8;i++)

        {

            x1 = x + m_V[i, 0]; y1 = y + m_V[i, 1];

            if(x1 >=0 && x1 < m_Dim && y1 >= 0 && y1 < m_Dim)

            {

                sTmp = search + m_Str[x1, y1];

                if (sTmp.Length == find.Length)

                {

                    if(sTmp == find)

                    {

                        m_Count++;

                    }              

                }

                else

                {

                   getStrCountRecursion(x1, y1, find, sTmp);

                }                              

            }

        }

 

        return;   

    }
}

转载于:https://www.cnblogs.com/StoneTao/archive/2005/12/14/297377.html

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值