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

原创 2006年06月15日 16:01:00

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
}

相关文章推荐

2013年ACM多校练习赛题目与标程

  • 2015年11月26日 17:29
  • 369KB
  • 下载

[Offer收割]编程练习赛6 题目1 : Playfair密码表 (密码学——模拟)

传送门时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho经常用Playfair密码表加密自己的代码。 密码表是按以下步骤生成的。 随机选择一个只包含大...

2006年百度之星程序设计大赛试题初赛题目-题5-座位调整

题目描述:  百度办公区里到处摆放着各种各样的零食。百度人力资源部的调研发现,员工如果可以在自己喜欢的美食旁边工作,工作效率会大大提高。因此,百度决定进行一次员工座位的大调整。  调整的方法如下:...

2006年百度之星程序设计大赛试题初赛题目-题4-剪刀石头布

N 个小孩正在和你玩一种剪刀石头布游戏。 N 个小孩中有一个是裁判,其余小孩分成三组(不排除某些组没有任何成员的可能性),但是你不知道谁是裁判,也不知道小孩们的分组情况。然后,小孩们开始玩剪刀石头布游...

2006年百度之星程序设计大赛试题初赛题目-题3-变态的比赛规则

变态的比赛规则  为了促进各部门员工的交流,百度 (baidu) 举办了一场全公司范围内的 " 拳皇友谊赛 " ,负责组织这场比赛的是百度的超级 " 拳皇 " 迷 W.Z. W.Z 不想用传统的淘汰...

题目1017:还是畅通工程(2006年浙江大学计算机及软件工程研究生机试真题)

题目1017:还是畅通工程 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2998 解决:1498 题目描述:     某省调查乡村交...

暑期9_15队内练习赛 B题 大数取模+矩阵快速幂

题目大意:求sin(n!*ln(n)*f[n]%2012)的结果,n 思路:哎~~菜菜菜啊~~却不是我的菜~~n!至少到2012都不用求后面的啦~~因为取余都为0了~~我还在傻乎乎的用矩阵求快速幂·...

[Offer收割]编程练习赛2 hihocoder 1272 买零食 (DFS 或 dp 水题)

[Offer收割]编程练习赛2 hihocoder 1272 买零食 (DFS 或 dp 水题)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TopCoder--2006年GOOGLE算法竞赛练习赛的题目第一题
举报原因:
原因补充:

(最多只允许输入30个字)