GOOGLE挑战赛练习题1及答案(200分)

博客虽未给出具体内容,但从标签可知涉及Google、UP、J#等信息技术相关内容,推测可能围绕Google相关技术、UP应用及J#编程等方面展开探讨。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

 

  1 public   class  DrawLines
  2 {
  3  class Pos
  4  {
  5    private int X;
  6    private int Y;
  7    private String status = ".";
  8
  9    public Pos(int x,int y)
 10    {
 11      this.X = x;
 12      this.Y = y;
 13      this.status = ".";
 14    }

 15
 16    public int getX()
 17    {
 18      return X;
 19    }

 20
 21    public int getY()
 22    {
 23      return Y;
 24    }

 25
 26    public void setX(int x)
 27    {
 28      this.X = x;
 29    }

 30
 31    public void setY(int y)
 32    {
 33      this.Y = y;
 34    }

 35
 36    public void setStatus(String s)
 37    {
 38      this.status = s;
 39    }

 40
 41    public String getSatus()
 42    {
 43      return status;
 44    }

 45
 46    public void showPos()
 47    {
 48      String xStr=X+"";
 49      String yStr=Y+"";
 50
 51      if(X<10)
 52      {
 53        xStr = "0"+xStr;
 54      }

 55
 56      if(Y<10)
 57      {
 58        yStr = "0"+yStr;
 59      }

 60    }

 61  }

 62
 63  public int min = 0, max = 19;
 64  public int North = 1, South = 3, West = 2, East = 0;
 65  public String LEFT = "LEFT", FORWARD = "FORWARD";
 66  public Pos[][] map = null;
 67
 68
 69
 70  public DrawLines()
 71 {
 72   this.direction = South;
 73   pos = new Pos(0,0);
 74
 75   //走之前初始化地图.
 76   map = new Pos[max + 1][max + 1];
 77   for(int row = 0;row<=max;row++)
 78   {
 79     for(int col=0;col<=max;col++)
 80     {
 81       map[row][col] = new Pos(row,col);
 82     }

 83   }

 84 }

 85
 86  public void showMap()
 87  {for (int col = 0; col <= max; col++)
 88
 89    {
 90      for (int row = 0; row <= max; row++)
 91      {
 92        System.out.print(map[row][col].getSatus());
 93      }

 94      System.out.println(); 
 95    }

 96  }

 97
 98  private Pos pos = null;
 99  private int direction = South;
100
101  public Pos getPos()
102  {
103    return pos;
104  }

105  public void setPos(Pos pos)
106  {
107    this.pos = pos;
108  }

109  /** *//**
110   *
111   * @param cmd
112   */

113  public void execute(String cmd)
114  {
115    if (cmd.equals("LEFT"))
116    {
117      Left();
118    }

119    else if (cmd.indexOf(FORWARD) >= 0)
120    {
121      int from = cmd.indexOf(FORWARD);
122      String stepStr = cmd.substring(from + FORWARD.length());
123      stepStr = stepStr.trim();
124      int step = Integer.parseInt(stepStr);
125      forward(step);
126    }

127    else
128
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值