Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1938069
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2006-08-14 15:44:20

package topcoder;

// 这个程序是2006年TOPCODER的GOOGLE算法竞赛练习赛的题目第一题。
// 详情请看以下网址
/*
 * 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
}

阅读(2809) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~