google中国编程挑战赛-练习题之三(250分题)

qwerttyy 2005-12-08 01:56:23
哎,看来我得先去学几年E再说了~TT


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.
...全文
290 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
onlyxu 2005-12-11
  • 打赏
  • 举报
回复
我的,得了243.58分
import java.util.Arrays;

public class DrawLines {
public String[] execute(String[] commands) {
char[][] res = init();
if (commands == null) {
return getResult(res);
}
int currentX = 0, currentY = 0, direction = 0;
int comms = commands.length;
String command = null;
for (int i = 0; i < comms; i++) {
command = commands[i];
if (command.equalsIgnoreCase("LEFT")) {
direction = (direction + 1) % 4;
}
else if (command.toUpperCase().startsWith("FORWARD")) {
String siz = command.substring(8);
int size = 0;
try {
size = Integer.parseInt(siz);
}
catch (Exception ex) {
continue;
}
int[] xy=forward(res, currentX, currentY, direction, size);
currentX=xy[0];
currentY=xy[1];
}
}
return getResult(res);
}

private String[] getResult(char[][] res) {
String[] result = new String[20];
for (int i = 0; i < 20; i++) {
result[i] = new String(res[i]);
}
return result;
}

private char[][] init() {
char[][] res = new char[20][20];
for (int i = 0; i < 20; i++) {
Arrays.fill(res[i], '.');
}
return res;
}

private int[] forward(char[][] res, int currentX, int currentY,
int direction, int size) {
if (direction == 1) {
if (currentX < 19) {
int realsize = currentX + size > 19 ? 19 : currentX + size;
for (int i = currentX; i <= realsize; i++) {
res[currentY][i] = 'X';
}
currentX = realsize;
}
}
else if (direction == 3) {
if (currentX > 0) {
int realsize = currentX - size < 0 ? 0 : currentX - size;
for (int i = currentX; i >= realsize; i--) {
res[currentY][i] = 'X';
}
currentX = realsize;
}
}
else if (direction == 0) {
if (currentX < 19) {
int realsize = currentY + size > 19 ? 19 : currentY + size;
for (int i = currentY; i <= realsize; i++) {
res[i][currentX] = 'X';
}
currentY = realsize;
}
}
else if (direction == 2) {
if (currentX > 0) {
int realsize = currentY - size < 0 ? 0 : currentY - size;
for (int i = currentY; i >= realsize; i--) {
res[i][currentX] = 'X';
}
currentY = realsize;
}
}
int[] xy = new int[2];
xy[0] = currentX;
xy[1] = currentY;
return xy;
}
}
qwerttyy 2005-12-10
  • 打赏
  • 举报
回复
up
qwerttyy 2005-12-09
  • 打赏
  • 举报
回复
up
qwerttyy 2005-12-08
  • 打赏
  • 举报
回复
ice226(ice226):

有正解版本了吗?:)
daimi01171 2005-12-08
  • 打赏
  • 举报
回复
看看
ice226 2005-12-08
  • 打赏
  • 举报
回复
对不起各位,发上来了才发现看错题目了,
ice226 2005-12-08
  • 打赏
  • 举报
回复
写完了,房上来,大家批评一下:P
public class Class1
{
#region var
int cx;
int cy;
enum DIRECTION{right=0,down,left,up};
DIRECTION direct;
char [,] map;
#endregion
#region Move
public Class1()
{
map=new char[20,20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
map[i,j]='.';
}
}
cx=0;
cy=0;
direct=DIRECTION.right;
}
public void Act(ArrayList al)
{
for(int i=0;i<al.Count;i++)
{
ExCmd((Cmd)al[i]);
}
}
public void ExCmd(Cmd step)
{
switch(step.ccmd)
{
case CmdType.Move:
Move(step.step);
break;
case CmdType.Turn:
TurnDirect();
break;
default:
break;
}
}
public void Move(int step)
{
if(step>19)
{
throw new Exception("UnAvaliable Input of Steps");
}
switch(direct)
{
case DIRECTION.up:
DoMove(step,0,-1);
break;
case DIRECTION.down:
DoMove(step,0,1);
break;
case DIRECTION.left:
DoMove(step,-1,0);
break;
case DIRECTION.right:
DoMove(step,1,0);
break;
}


}
public void DoMove(int step,int xfactor,int yfactor)
{
for(int i=0;i<step;i++)
{
map[cx,cy]='X';
cx+=xfactor;
cy+=yfactor;
if(cx>19||cx<0||cy>19||cy<0)
{
throw new Exception("Out Of map on Moving:cx is"+cx.ToString()+":cy is "+cy.ToString());
}
map[cx,cy]='X';
}
}
public void TurnDirect()
{
int temp=(int)direct;
temp++;
if(temp>3)
temp-=4;
direct=(DIRECTION)temp;
}
#endregion
#region CMD analyzer
public ArrayList AnalyzeCmd(string cmd)
{
ArrayList al=new ArrayList();
string temp=@"[\w\d,]+";
Regex r=new Regex(temp);
System.Text.RegularExpressions.MatchCollection mc =r.Matches(cmd);
string strtmp="";
foreach(Match m in mc)
{
strtmp+=m.Value;
}
string[] tmp=strtmp.Split(',');
Regex regx=new Regex(@"[\d]+");

for(int i=0;i<tmp.Length;i++)
{
if(tmp[i]=="LEFT")
{
al.Add(new Cmd(CmdType.Turn,0));
}
else
{
if(regx.IsMatch(tmp[i]))
{
al.Add(new Cmd(CmdType.Move,Int32.Parse(regx.Match(tmp[i]).Value)));
}
else
{
continue;
}
}
}
return al;
}
#endregion
#region Print
public void Print()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
Console.Write(map[i,j]);
}
Console.WriteLine("");
}
}
#endregion
[STAThread]
public static void Main()
{
string[] s=new string[4];
s[0]=@"{'FORWARD 19', 'LEFT', 'FORWARD 19', 'LEFT', 'FORWARD 19', 'LEFT', 'FORWARD 19'}";
s[1]=@"{'LEFT', 'LEFT', 'LEFT', 'LEFT', 'LEFT', 'LEFT', 'LEFT', 'LEFT'}";
s[2]=@"{'FORWARD 5'}";
s[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'}";
Class1 c1=null;
try
{
for(int i=0;i<4;i++)
{
c1=new Class1();
//c1.Print();
ArrayList al=c1.AnalyzeCmd(s[i]);
c1.Act(al);
c1.Print();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());

}
}


}
public enum CmdType {Move,Turn};
public class Cmd
{
public CmdType ccmd;
public int step;
public Cmd(CmdType cmdtp,int stp)
{
ccmd=cmdtp;
step=stp;
}
}

110,539

社区成员

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

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

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