题目如下:
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:
Public Function execute(commands As String()) As String()
(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",
 "...................." }This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

解决方案 »

  1.   

    我开始没有考虑清楚就动手写了,结果代码写的不理想,百且也超时了。。 :(代码如下:
    'Class1.clsOption Explicit
    Private Enum Direction
        d_down = 0
        d_right = 1
        d_up = 2
        d_left = 3
    End EnumPrivate Type Point
        X As Integer
        Y As Integer
    End TypePrivate resultArr(19, 19) As String
    Private CurrentDirection As Direction
    Private CurrentPosition As PointPublic Function execute(commands() As String) As String()
        Dim i As Integer, j As Integer
        CurrentDirection = d_down
        
        For i = 0 To 19
            For j = 0 To 19
                resultArr(i, j) = "."
            Next
        Next
        CurrentPosition.X = 0
        CurrentPosition.Y = 0
        
        For i = 0 To UBound(commands)
            ExecuteCommand commands(i)
        Next
        
        execute = resultArr
    End FunctionPrivate Sub ExecuteCommand(strCommand As String)
        Dim ForwardStep As Integer
        
        If strCommand = "LEFT" Then
            If CurrentDirection = d_left Then
                CurrentDirection = d_down
            Else
                CurrentDirection = CurrentDirection + 1
            End If
        ElseIf Left(strCommand, 8) = "FORWARD " Then
            ForwardStep = Val(Mid(strCommand, 9))
            Forward ForwardStep
        End If
    End SubPrivate Sub Forward(ForwardStep As Integer)
        Dim i As Integer
        Select Case CurrentDirection
            Case d_down
                For i = 0 To ForwardStep
                    SetPixel CurrentPosition.Y + i, CurrentPosition.X
                Next
                CurrentPosition.Y = CurrentPosition.Y + i - 1
            Case d_up
                For i = 0 To ForwardStep
                    SetPixel CurrentPosition.Y - i, CurrentPosition.X
                Next
                CurrentPosition.Y = CurrentPosition.Y - i + 1
            Case d_right
                For i = 0 To ForwardStep
                    SetPixel CurrentPosition.Y, CurrentPosition.X + i
                Next
                CurrentPosition.X = CurrentPosition.X + i - 1
            Case d_left
                For i = 0 To ForwardStep
                    SetPixel CurrentPosition.Y, CurrentPosition.X - i
                Next
                CurrentPosition.X = CurrentPosition.X - i + 1
        End Select
    End SubPrivate Sub SetPixel(Y As Integer, X As Integer)
        resultArr(Y, X) = "X"
    End Sub
    'Form1.frm
    Option ExplicitPrivate Sub Form_Load()
        Dim c As New Class1
        Dim A() As String
        Dim result() As String
        Dim strTemp As String
        A = Split("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", ",")
        result = c.execute(A)
        
        Dim i, j
        
        For i = 0 To 19
            strTemp = ""
            For j = 0 To 19
                strTemp = strTemp & result(i, j)
            Next
            Debug.Print strTemp
        Next
    End Sub输出结果为
    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
    ....................
    基本通过上面的代码我是在VB里写的,不知道在TopCoder中如何通过编译和测试。哪位指点一下?
      

  2.   

    今天选择了一个100分的题,想不到更简单
    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 Integer, 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, Integer
    Returns:
    Integer
    Method signature:
    Public Function getPosition(keystrokes As String, N As Integer) As Integer
    (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: 33)    
    "RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
    19
    Returns: 12This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
    这次可以编译了,但是提示Len函数和Mid函数未声明,而我在代码放到VB里运行正常,不知道需要怎么写才能让TopCoder的编译器支持这两个函数编译错误信息:
    Your code did not compile:(6) : error BC30451: Name 'Len' is not declared.            For i = 0 To Len(keystrokes) - 1
                             ~~~                
    (7) : error BC30451: Name 'Mid' is not declared.                CurrentPosition = CursorMove(Mid(keystrokes, i + 1, 1), CurrentPosition, N)
    我的源代码如下:    Public Function getPosition(keystrokes As String, N As Integer) As Integer
            Dim CurrentPosition As Integer
            Dim i As Integer
            CurrentPosition = 0
            For i = 0 To Len(keystrokes) - 1
                CurrentPosition = CursorMove(Mid(keystrokes, i + 1, 1), CurrentPosition, N)
            Next
            getPosition = CurrentPosition
        End Function
        
        Private Function CursorMove(Action As String, C As Integer, N As Integer) As Integer
            Select Case Action
                Case "E"
                    CursorMove = N
                Case "H"
                    CursorMove = 0
                Case "L"
                    If C - 1 < 0 Then
                        CursorMove = 0
                    Else
                        CursorMove = C - 1
                    End If
                Case "R"
                    If C + 1 > N Then
                        CursorMove = N
                    Else
                        CursorMove = C + 1
                    End If
            End Select
        End Function
      

  3.   

    总算明天了,是VB.NetImports Microsoft.VisualBasic
    Public Class CursorPosition
    Public Function getPosition(keystrokes As String, N As Integer) As Integer
            Dim CurrentPosition As Integer
             Dim i As Integer
            CurrentPosition = 0
             For i = 0 To Len(keystrokes) - 1
                CurrentPosition = CursorMove(Mid(keystrokes, i + 1, 1), CurrentPosition, N)
            Next
            getPosition = CurrentPosition
        End Function
        Private Function CursorMove(Action As String, C As Integer, N As Integer) As Integer
             Select Case Action
                Case "E"
                     CursorMove = N
                Case "H"
                    CursorMove = 0
                Case "L"
                    If C - 1 < 0 Then
                 CursorMove = 0
                    Else
                        CursorMove = C - 1
                    End If
                Case "R"
                    If C + 1 > N Then
                        CursorMove = N
                    Else
                        CursorMove = C + 1
                    End If
            End Select
        End Function
    End Class
      

  4.   

    http://www.google.com/chinacodejam
    http://www.topcoder.com/pl/?module=Static&d1=gccj05&d2=ZH_instructions都打不开