关闭

TopCoder--2006年GOOGLE算法竞赛练习赛的题目第一题

1255人阅读 评论(0) 收藏 举报

package topcoder;

// 这个程序是2006年TOPCODER的GOOGLE算法竞赛练习赛的题目第一题。
// 详情请看以下网址http://www.topcoder.com/gcjc_zh
/*
 * Created on 2005-11-24
 * @author Admin.zhouyuan
 */
class DrawLines
{
    public char[][] matrix;
    byte positionRow = 0;
    byte positionCol = 0;
    public byte direction;

    // 构造函数,把所有的点都置为DESERT, 方向置为DOWN.
    DrawLines()
    {
        matrix = new char[20][20];
        this.direction = 0;
        for (byte i = 0; i < 20; i++)
        {
            for (byte j = 0; j < 20; j++)
            {
                this.matrix[i][j] = '.';
            }
        }
    }

    String[] execute(String[] commands)
    {
        byte i; // 用做循环变量
        for (i = 0; i < commands.length; i++)
        {
            if (commands[i].charAt(0) == 'F') // 如果commands[i]第一个字符为'F', 则认为是FORWARD,向前移动.
            {
                Integer j;
                if (commands[i].length() == 10) // 把FORWARD后的数字转变为整数,传给j
                    j = new Integer(commands[i].substring(8, 10));
                else
                    j = new Integer(commands[i].substring(8, 9));

                switch (this.direction)
                // 判断方向,根据不同方向改变值为'GONE'
                {
                    case 0:
                        for (byte k = j.byteValue(); k > 0; k--)
                        {
                            this.positionRow += 1;
                            this.matrix[positionRow][positionCol] = 'X';
                        }
                        break;

                    case 1:
                        for (byte k = j.byteValue(); k > 0; k--)
                        {
                            this.positionCol += 1;
                            this.matrix[positionRow][positionCol] = 'X';
                        }
                        break;

                    case 2:
                        for (byte k = j.byteValue(); k > 0; k--)
                        {
                            this.positionRow -= 1;
                            this.matrix[positionRow][positionCol] = 'X';
                        }
                        break;

                    case 3:
                        for (byte k = j.byteValue(); k > 0; k--)
                        {
                            this.positionCol -= 1;
                            this.matrix[positionRow][positionCol] = 'X';
                        }
                        break;
                } // end switch
            } // end if
            else
            // 证明命令是LEFT,方向变化.
            {
                direction += 1;
                this.direction %= 4;
            }
        } // end for

        if (this.positionCol != 0 || this.positionRow != 0)
        {
            matrix[0][0] = 'X';
        }
        String[] str = new String[] { new String(matrix[0]), new String(matrix[1]), new String(matrix[2]), new String(matrix[3]),
                new String(matrix[4]), new String(matrix[5]), new String(matrix[6]), new String(matrix[7]),
                new String(matrix[8]), new String(matrix[9]), new String(matrix[10]), new String(matrix[11]),
                new String(matrix[12]), new String(matrix[13]), new String(matrix[14]), new String(matrix[15]),
                new String(matrix[16]), new String(matrix[17]), new String(matrix[18]), new String(matrix[19]) };
        return str;
    } // end execute

    public static void main(String[] args)
    {
        DrawLines dl = new DrawLines();
        // String[] str = new String[]{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"};
        // String[] str = new String[]{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"};
        // String[] str = new String[]{"FORWARD 1"};
        /*
         * String[] str = new String[]{"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"};
         */
        String[] s = dl.execute(new String[] { "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" });
        for (int i = s.length; i > 0; i--)
        {
            System.out.println(s[20 - i]);
        }
    } // end main
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10273次
    • 积分:193
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条