zoukankan      html  css  js  c++  java
  • 参加Google™ Code Jam 中国编程挑战赛(1)

            很久没有来管理自己的blog。今天看到左下角的google广告有Google™ Code Jam - 中国编程挑战赛,参加一下看看大赛的题目有多难呢!申请帐号,下载java经过半个小时终于进入竞技场的练习区,打开一道250分的题目做了一下,才得249.22分,晕~~~~~~~~~不知道自己的代码那里不对,请路过的高手指点。

    ps: 仔细阅读说明,原来分数的高低和编译错误次数,时间有直接关系。

            题目:
    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
            
    {
                
    /// <summary>
                
    /// 坐标点
                
    /// </summary>

                class Point
                
    {
                    
    private int px=0;
                    
    public int X
                    
    {
                        
    get {return px;}
                        
    set {px = value;}
                    }

                    
    private int py=0;
                    
    public int Y
                    
    {
                        
    get {return py;}
                        
    set {py = value;}
                    }

                    
    public Point()
                    
    {
                    }

                    
    public Point(int x,int y)
                    
    {
                        px 
    = x;
                        py 
    = y;
                    }

                }

                

                
    /// <summary>
                
    /// 指示前进方向
                
    /// </summary>

                private enum Directional
                
    {
                    up,down,right,left
                }

                

                
    public DrawLines()
                
    {
                
                }

                
    /// <summary>
                
    /// 执行命令
                
    /// </summary>
                
    /// <param name="commands">命令列表</param>
                
    /// <returns>图形</returns>

                public string[] execute(string[] commands)
                
    {

                    
                    
    //初始化
                    string[][] retrunValues = new string[20][];
                    
    for(int i = 0; i< 20; i++)
                    
    {
                        retrunValues[i] 
    = new string[20];
                        
    for (int j = 0; j<20; j++)
                        
    {
                             retrunValues[i][j] 
    = ".";
                        }

                    }
        

                    Directional dir 
    = Directional.down;
                    Point currentPoint 
    = new Point();
                    
    int offset = 0;

                    
    if (commands.Length<=50)
                    
    {

                        
    //处理命令
                        foreach (string command in commands)
                        
    {
                            
    switch (command)
                            
    {
                                
    case "LEFT":
                                
    switch (dir) 
                                
    {
                                    
    case Directional.down:
                                        dir 
    = Directional.right;
                                        
    break;
                                    
    case Directional.right:
                                        dir 
    = Directional.up;
                                        
    break;
                                    
    case Directional.up:
                                        dir 
    = Directional.left;
                                        
    break;
                                    
    case Directional.left:
                                        dir 
    = Directional.down;
                                        
    break;
                                }

                                    
    break;
                                
    default:
                                    
    if (command.IndexOf("FORWARD",0==0)
                                    
    {
                                        offset 
    = System.Convert.ToInt32(command.Substring(8,command.Length-8));
                                        retrunValues 
    =    moveForward(ref currentPoint,dir,offset,retrunValues);
                                        
    break;
                                    }

                                    
    else
                                    
    {
                                        
    goto case "LEFT";
                                    }

                                
                            }
            
                        }

                    
                    }




                    
    //转换
                    string[] RetrunValues = new string[20];
                    
    string tempString = "";
                    
    for(int i = 0; i<20 ;i++)
                    
    {
                        tempString 
    = "";
                        
    for(int j = 0; j<20 ;j++)
                        
    {
                            tempString 
    =tempString + retrunValues[i][j];    
                        }

                        RetrunValues[i] 
    = tempString;
                    }

                    
    return RetrunValues;
                }


                
    /// <summary>
                
    /// 前进
                
    /// </summary>
                
    /// <param name="dir">前进方向</param>
                
    /// <param name="offset">步长</param>
                
    /// <param name="returnValues">结果</param>
                
    /// <returns>结果</returns>

                private string[][] moveForward(ref Point startPoint,Directional dir,int offset,string[][] returnValues)
                
    {    
                    
    int i = 0;
                    
    switch (dir)
                    
    {
                        
    case Directional.up:
                            
    for(i= startPoint.X;i>=(startPoint.X-offset)%20;i--)
                            
    {
                                returnValues[i][startPoint.Y] 
    = "X";
                                    
                            }

                            startPoint.X 
    = i+1;        
                            
    break;
                        
    case Directional.left:
                            
    for(i = startPoint.Y;i>=(startPoint.Y-offset)%20;i--)
                            
    {
                                returnValues[startPoint.X][i] 
    = "X";
                            }

                            startPoint.Y 
    = i+1;
                            
    break;
                        
    case Directional.down:
                            
    for(i = startPoint.X;i<=(startPoint.X+offset)%20;i++)
                            
    {
                                returnValues[i][startPoint.Y] 
    = "X";
                            }

                            startPoint.X 
    = i-1;
                            
    break;
                        
    case Directional.right:
                            
    for(i = startPoint.Y;i<=(startPoint.Y+offset)%20;i++)
                            
    {
                                returnValues[startPoint.X][i] 
    = "X";
                            }

                            startPoint.Y 
    = i-1;
                            
    break;
                    }

                    
    return returnValues;
                }

            }


  • 相关阅读:
    Android activity生命周期
    Android ListView用法
    Android TextWatcher的使用方法(监听ExitText的方法)
    Android EditText禁止换行键(回车键enter)
    Android 数据存储-文件读写操作
    Android Spinner组件的使用方法
    IntelliJ IDEA下maven Spring MVC配置
    MyBatis配置文件中报错:URI is not registered(Settings | Languages & Frameworks | Schemas and DTDs)
    Eclipse下创建Spring MVC web程序--非maven版
    C# 时间与时间戳互转
  • 原文地址:https://www.cnblogs.com/pfengk/p/292579.html
Copyright © 2011-2022 走看看