李开复搞的Code Jam编程大赛里面的一道Demo例题!

csShooter 2005-11-29 05:20:29
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",
"...................." }

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.
...全文
263 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
windbey 2005-11-29
  • 打赏
  • 举报
回复
mark
seekg 2005-11-29
  • 打赏
  • 举报
回复
有空再来看
csShooter 2005-11-29
  • 打赏
  • 举报
回复
/*****************************
* Program: Code Jam 250分
* Code by: screen
* Create date: 2005.11.23
* **************************/


using System;

namespace CodeJam
{
/// <summary>
/// Google Code Jam 参赛题。
/// 贪吃蛇原型
/// </summary>
class DrawLines
{

/// <summary>
/// 画布二维数组
/// </summary>
private int[,] canvas;
/// <summary>
/// 运动命令
/// </summary>
private string[] m_command;
/// <summary>
/// 画布大小
/// </summary>
private const int CANVAL_LARG = 20;
/// <summary>
/// 命令语句的最大长度
/// </summary>
private const int COMMAND_LENGTH = 50;
/// <summary>
/// 运动方向0->向下,1->向右,2->向上,3->向左
/// </summary>
private int nDirection;

/// <summary>
/// 移动前的位置
/// </summary>
private Point oldPoint;
/// <summary>
/// 当前光标位置
/// </summary>
private Point nowPoint;


DrawLines()
{
canvas = new int[CANVAL_LARG,CANVAL_LARG];
m_command = new string[50];

// 初始化画布
for(int x=0; x < CANVAL_LARG; x++)
{
for(int y=0; y < CANVAL_LARG; y++)
{
canvas[x,y] = 0;
}
}

// 初始化命令
m_command = new string[]{""};

oldPoint.x = 0;
oldPoint.y = 0;
nowPoint.x = 0;
nowPoint.y = 0;

}

/// <summary>
/// 执行画线命令
/// </summary>
/// <param name="commands">命令语句</param>
/// <returns>执行结果。一个二维字符数组</returns>
public string[] execute(string[] commands)
{
foreach(string _command in commands)
{
if(_command == "LEFT")
{
nDirection ++;
nDirection = nDirection%4;
continue;
}

if( _command.Length > "FORWARD ".Length && _command.Substring(0,"FORWARD ".Length) == "FORWARD ")
{
string _step = _command.Substring("FORWARD ".Length, _command.Length-"FORWARD ".Length);
int nStep = 0;
try
{
nStep = Convert.ToInt32(_step);

}
catch(Exception ex)
{
Console.WriteLine("有误");
}

// 有效前进
if(nStep > 0 && nStep < CANVAL_LARG)
{
switch(nDirection)
{
case 0:
nowPoint.y = oldPoint.y + nStep;
if(nowPoint.y >= CANVAL_LARG)
{
nowPoint.y = CANVAL_LARG-1;
}
for(int i = oldPoint.y; i <= nowPoint.y; i++)
{
canvas[oldPoint.x, i] = 1;
}

break;
case 1:
nowPoint.x = oldPoint.x + nStep;
if(nowPoint.x >= CANVAL_LARG)
{
nowPoint.x = CANVAL_LARG-1;
}
for(int i = oldPoint.x; i <= nowPoint.x; i++)
{
canvas[i, oldPoint.y] = 1;
}
break;
case 2:
nowPoint.y = oldPoint.y - nStep;
if(nowPoint.y < 0)
{
nowPoint.y = 0;
}
for(int i = nowPoint.y; i < oldPoint.y; i++)
{
canvas[oldPoint.x, i] = 1;
}
break;
case 3:
nowPoint.x = oldPoint.x - nStep;
if(nowPoint.x < 0)
{
nowPoint.x = 0;
}
for(int i = nowPoint.x; i < oldPoint.x; i++)
{
canvas[i, oldPoint.y] = 1;
}
break;
}

// 画线


oldPoint = nowPoint;
}
}
}
return PrintCanvas();
}

/// <summary>
/// 在画页指定点画图
/// </summary>
/// <param name="Coor_x">X轴坐标</param>
/// <param name="Coor_y">Y轴坐标</param>
private void PaintPoint(int Coor_x, int Coor_y)
{
canvas[Coor_x,Coor_y] = 1;
}



/// <summary>
/// 打印出画布现状
/// </summary>
/// <returns></returns>
private string[] PrintCanvas()
{
string[] _canvas = new string[20];
for(int i=0; i < CANVAL_LARG; i++)
{
string _tmp = "";
for(int j=0; j < CANVAL_LARG; j++)
{
if(canvas[i,j] == 0)
{
_tmp += ".";
}
else
{
_tmp += "x";
}
}
_canvas[i] = _tmp;
}
return _canvas;
}


/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
//string[] comm = new string[]{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"};
//string[] comm = new string[]{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"};
string[] comm = 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 100", "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 100", "LEFT", "LEFT", "LEFT", "FORWARD 7"};
string[] canvas = new string[19];

DrawLines snake = new DrawLines();
canvas = snake.execute(comm);

for(int i=0; i < canvas.Length; i++)
{
Console.WriteLine(canvas[i]);
}
Console.Read();
}

}

/// <summary>
/// 当前光标位置
/// </summary>
struct Point
{
/// <summary>
/// x坐标
/// </summary>
public int x;
/// <summary>
/// y坐标
/// </summary>
public int y;
}
}
xldlm 2005-11-29
  • 打赏
  • 举报
回复
啊! 英文@_@
csShooter 2005-11-29
  • 打赏
  • 举报
回复
兄弟们,注意边界问题! ..

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧