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![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
3
class Pos
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
private int X;
6
private int Y;
7
private String status = ".";
8![](/Images/OutliningIndicators/InBlock.gif)
9
public Pos(int x,int y)
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
this.X = x;
12
this.Y = y;
13
this.status = ".";
14
}
15![](/Images/OutliningIndicators/InBlock.gif)
16
public int getX()
17![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
return X;
19
}
20![](/Images/OutliningIndicators/InBlock.gif)
21
public int getY()
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
return Y;
24
}
25![](/Images/OutliningIndicators/InBlock.gif)
26
public void setX(int x)
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
this.X = x;
29
}
30![](/Images/OutliningIndicators/InBlock.gif)
31
public void setY(int y)
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
this.Y = y;
34
}
35![](/Images/OutliningIndicators/InBlock.gif)
36
public void setStatus(String s)
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
this.status = s;
39
}
40![](/Images/OutliningIndicators/InBlock.gif)
41
public String getSatus()
42![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
return status;
44
}
45![](/Images/OutliningIndicators/InBlock.gif)
46
public void showPos()
47![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
String xStr=X+"";
49
String yStr=Y+"";
50![](/Images/OutliningIndicators/InBlock.gif)
51
if(X<10)
52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
xStr = "0"+xStr;
54
}
55![](/Images/OutliningIndicators/InBlock.gif)
56
if(Y<10)
57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
yStr = "0"+yStr;
59
}
60
}
61
}
62![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
68![](/Images/OutliningIndicators/InBlock.gif)
69![](/Images/OutliningIndicators/InBlock.gif)
70
public DrawLines()
71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
this.direction = South;
73
pos = new Pos(0,0);
74![](/Images/OutliningIndicators/InBlock.gif)
75
//走之前初始化地图.
76
map = new Pos[max + 1][max + 1];
77
for(int row = 0;row<=max;row++)
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
for(int col=0;col<=max;col++)
80![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
map[row][col] = new Pos(row,col);
82
}
83
}
84
}
85![](/Images/OutliningIndicators/InBlock.gif)
86
public void showMap()
87![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{for (int col = 0; col <= max; col++)
88![](/Images/OutliningIndicators/InBlock.gif)
89![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
for (int row = 0; row <= max; row++)
91![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
System.out.print(map[row][col].getSatus());
93
}
94
System.out.println();
95
}
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
private Pos pos = null;
99
private int direction = South;
100![](/Images/OutliningIndicators/InBlock.gif)
101
public Pos getPos()
102![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
return pos;
104
}
105
public void setPos(Pos pos)
106![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
107
this.pos = pos;
108
}
109![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
110
*
111
* @param cmd
112
*/
113
public void execute(String cmd)
114![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
if (cmd.equals("LEFT"))
116![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
Left();
118
}
119
else if (cmd.indexOf(FORWARD) >= 0)
120![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
System.out.println("未知的命令,无法执行.");
130
}
131
}
132![](/Images/OutliningIndicators/InBlock.gif)
133
public String[] execute(String[] cmds)
134![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
if (cmds != null)
136![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
137
for (int i = 0; i < cmds.length; i++)
138![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
execute(cmds[i]);
140
}
141
}
142![](/Images/OutliningIndicators/InBlock.gif)
143
String[] rval = new String[20];
144
String tempStr = "";
145
for (int col = 0; col <= max; col++)
146![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
tempStr = "";
148
for (int row = 0; row <= max; row++)
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
tempStr = tempStr + map[row][col].getSatus();
151
}
152
rval[col]=tempStr;
153
}
154
return rval;
155![](/Images/OutliningIndicators/InBlock.gif)
156
}
157![](/Images/OutliningIndicators/InBlock.gif)
158
public void Left()
159![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
160
direction = (direction + 1) % 4;
161
}
162![](/Images/OutliningIndicators/InBlock.gif)
163
public void forward(int step)
164![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
if (direction == South) //Y++
166![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
167
while (pos.getY() < max && step>0)
168![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
if (map[pos.getX()][pos.getY()].getSatus().equals(".")) //第一步也要画
170![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
171
map[pos.getX()][pos.getY()].setStatus("*");
172
}
173
pos.setY(pos.getY() + 1);
174
map[pos.getX()][pos.getY()].setStatus("*");
175
map[pos.getX()][pos.getY()].showPos();
176
step--;
177
}
178
}
179
else if (direction == East) //X++
180![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
while (pos.getX() < max && step>0)
182![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
if (map[pos.getX()][pos.getY()].getSatus().equals(".")) //第一步也要画
184![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185
map[pos.getX()][pos.getY()].setStatus("*");
186
}
187
pos.setX(pos.getX() + 1);
188
map[pos.getX()][pos.getY()].setStatus("*");
189
map[pos.getX()][pos.getY()].showPos();
190
step--;
191
}
192![](/Images/OutliningIndicators/InBlock.gif)
193
}
194
else if (direction == North) //Y--
195![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
while (pos.getY() > min && step>0)
197![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
198
if (map[pos.getX()][pos.getY()].getSatus().equals(".")) //第一步也要画
199![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
map[pos.getX()][pos.getY()].setStatus("*");
201
}
202
pos.setY(pos.getY() - 1);
203
map[pos.getX()][pos.getY()].setStatus("*");
204
map[pos.getX()][pos.getY()].showPos();
205
step--;
206
}
207
}
208
else if (direction == West ) //X--
209![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
210
while (pos.getX() > min && step>0)
211![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
212
if (map[pos.getX()][pos.getY()].getSatus().equals(".")) //draw the first step
213![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214
map[pos.getX()][pos.getY()].setStatus("*");
215
}
216
pos.setX(pos.getX() - 1);
217
map[pos.getX()][pos.getY()].setStatus("*");
218
map[pos.getX()][pos.getY()].showPos();
219
step--;
220
}
221
}
222
}
223![](/Images/OutliningIndicators/InBlock.gif)
224
public static void main(String[] args)
225![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String[] cmds =
{ "FORWARD 19","LEFT","FORWARD 19", "LEFT","FORWARD 19","LEFT","FORWARD 17","LEFT","FORWARD 17","LEFT","FORWARD 17","LEFT","FORWARD 13","LEFT","FORWARD 13","LEFT","FORWARD 13","LEFT","FORWARD 13","LEFT","FORWARD 13","LEFT",};
227![](/Images/OutliningIndicators/InBlock.gif)
228
DrawLines dl = new DrawLines();
229![](/Images/OutliningIndicators/InBlock.gif)
230
dl.execute(cmds);
231![](/Images/OutliningIndicators/InBlock.gif)
232
dl.showMap();
233![](/Images/OutliningIndicators/InBlock.gif)
234
}
235![](/Images/OutliningIndicators/InBlock.gif)
236
}
posted on 2005-11-28 10:32
鱼上游 阅读(1070)
评论(2) 编辑 收藏 所属分类:
爪哇风景欣赏 、
爪哇世界探险