今天遇到一個有點好玩的算法,就是通過一系列的指令來控制機器人的行走,最後要求定位機器人的準確位置。第一次寫還寫錯了,改了一次才正確,覺得很有意思,就記下來了。
機器人有 4種指令:
- forward x,前進 x 米。
- back x,先向後轉,然後前進 x 米。
- left x,先向左轉,然後前進 x 米。
- right x,先向右轉,然後前進 x 米。
現在把機器人放在座標軸原點,起始朝向爲 x軸正方向。經過一系列指令以後,你能告訴蒜頭君機器人的座標位置嗎。座標軸上一個單位長度表示 1 米。
輸入格式
第一行輸入一個整數 n(1≤n≤100) 表示指令的個數。
接下里 n 行,每行輸入形如上面的指令,其中 −1000≤x≤1000。
輸出格式
輸出兩個整數 x,y表示機器人最後座標。用空格隔開。
樣例輸入
10
back -9
left 3
left 8
back 15
right 10
right -7
right -3
left 11
right 17
left 3
樣例輸出
9 -7
分析:拿到這樣一個問題,首先想到的事不難,應該很簡單,座標有四個方向,直接寫出關於四個方向的方法定義就行了,雖然確實是如此,但是我們不得不考慮一個問題,機器人是有正面的,每一次的轉向都是相對於他自己,而不是座標軸。而我們需要的答案卻是座標軸值。所以我們光簡單的規定座標的方向是不行的,我們還要定義機器人的方向(機器人面對的方向),並在兩個方向上建立一定的聯繫。
就比如題目所說,機器人首先是面對x軸正方向,機器人可以向任何一個方向移動,但是移動之後不僅僅是座標軸值改變了,對於機器人本身而言,相對於機器人的方向也變了。比如機器人第一步要向x軸負方向走 -8米,我們知道,朝負方向走 -8米,其實相當於正方向走了 8米,但是此時的機器人朝向已經改變,從面相x正半軸變爲面向x負半軸。而對於下一次的移動,機器人還是針對於自身的前後左右進行移動,並且這種變化每一次移動都會改變。
經過上述分析(如果有疑惑可以自己畫個圖,很簡單就能理解),我們知道,需要定義一個值來表示機器人此時的朝向(機器人相對於座標軸)。在接下來的代碼中,用headTo表示,其值是 1,2,3,4.分別代表朝x+,y-,x-,y+ 方向。對於代碼中的其他方法基本上就能見名知意了,此時還有一個需要注意的細節:
在機器人每一次移動後,我們需要更新機器人的朝向信息,也就是headTo的值,不然不能得到正確結果。整體代碼如下:
import java.util.Scanner;
public class RobotWalking
{
private int x;
private int y;
private int headTo;
public RobotWalking()
{
this.x=0;
this.y=0;
//default head to x+; 2 stand for y-;3 stand for x-;4 stand for y+;
this.headTo=1;
}
public void direction(int dir)
{
this.headTo=dir;
}
/*The fallowing 4 methods:4 kinds of actions of the robot
Every time you move the robot,you had to change the direction info of the robot
*/
public void UP(int steps)
{
switch(this.headTo)
{
case 1:this.x+=steps;this.headTo=1;break;
case 2:this.y-=steps;this.headTo=2;break;
case 3:this.x-=steps;this.headTo=3;break;
case 4:this.y+=steps;this.headTo=4;break;
}
}
public void DOWN(int steps)
{
switch(this.headTo)
{
case 1:this.x-=steps;this.headTo=3;break;
case 2:this.y+=steps;this.headTo=4;break;
case 3:this.x+=steps;this.headTo=1;break;
case 4:this.y-=steps;this.headTo=2;break;
}
}
public void RIGHT(int steps)
{
switch(this.headTo)
{
case 1:this.y-=steps;this.headTo=2;break;
case 2:this.x-=steps;this.headTo=3;break;
case 3:this.y+=steps;this.headTo=4;break;
case 4:this.x+=steps;this.headTo=1;break;
}
}
public void LEFT(int steps)
{
switch(this.headTo)
{
case 1:this.y+=steps;this.headTo=4;break;
case 2:this.x+=steps;this.headTo=1;break;
case 3:this.y-=steps;this.headTo=2;break;
case 4:this.x-=steps;this.headTo=3;break;
}
}
//Method to display the robot locatipn!
public void GetLocation()
{
System.out.println(this.x+" "+this.y);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int numbers=sc.nextInt();
//Save the code which can control the moving directions of the robot
String[] method=new String[numbers];
//Save the steps number of every move
int steps[]=new int[numbers];
for(int i=0;i<numbers;i++)
{
method[i]=sc.next();
steps[i]=sc.nextInt();
}
RobotWalking test=new RobotWalking();
//Start to move the robot and calculate the final location!
for(int i=0;i<numbers;i++)
{
switch(method[i])
{
case "forward":test.UP(steps[i]);break;
case "back":test.DOWN(steps[i]);break;
case "left":test.LEFT(steps[i]);break;
case "right":test.RIGHT(steps[i]);break;
}
}
test.GetLocation();
}
}
運行結果如圖:
submit and test!:
PERFECT & AMUSIVE!!!