1. 程式人生 > >874. Walking Robot Simulation

874. Walking Robot Simulation

題目描述:

A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:

  • -2: turn left 90 degrees
  • -1: turn right 90 degrees
  • 1 <= x <= 9: move forward x units

Some of the grid squares are obstacles. 

The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])

If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

Return the square of the maximum Euclidean distance that the robot will be from the origin.

  • Example 1:

Input: commands = [4,-1,3], obstacles = []

Output: 25

Explanation: robot will go to (3, 4)

  • Example 2:

Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]

Output: 65

Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)

根據example1,可以想到找做一個沒有障礙物的:

package rt;

import java.util.ArrayList;
import java.util.Scanner;

public class rt {

	public static void main(String[] args) {
		/*
		 * 當前方向狀態
		 * a:朝北
		 * b:朝東
		 * c:朝南
		 * d:朝西
		 * 切記不能用數字,因為容易與輸入的數字混淆
		 */
		String[] flag={"a","b","c","d"};
		int index=0;
		String f=flag[index];
		//機器人座標,初始座標為原點
		int x=0;
		int y=0;
		ArrayList<Integer> s=new ArrayList();
		Scanner sc=new Scanner(System.in);
		//準備資料
			while (true) {
				//退出迴圈條件
				String input = sc.next();
				if (input.equals("exit")) {
					break;
				}else {
					try {
						s.add(Integer.parseInt(input));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			}
			sc.close();
			
//		System.out.println(s);
	   //障礙位置
//		int x1=sc.nextInt();
//		int y1=sc.nextInt();
		for (int i = 0; i < s.size(); i++) {
			if (s.get(i)==-1) {
				index++;
				index=(index>3)?0:index;
				f=flag[index];
			} else if(s.get(i)==-2){
				index--;
				index=(index<0)?3:index;
				f=flag[index];
			}else {
				if (s.get(i)<1||s.get(i)>9) {
					System.out.println("輸入錯誤");
					break;
				}
				if ((s.get(i)!=-2)&&(s.get(i)!=-1)) {
					switch (f) {
					case "a":y+=s.get(i);
					     break;
					case "b":x+=s.get(i);
					     break;
					case "c":y-=s.get(i);
					     break;
					case "d":x-=s.get(i);
						break;
					}
				}
				
				
			}
			
		}
		System.out.println(x*x+y*y);
		
	}

}

結果測試如下:

4

-1

3

exit

25

有障礙物的程式碼:

package rt;

import java.util.ArrayList;
import java.util.Scanner;

public class rt {

	public static void main(String[] args) {
		/*
		 * 當前方向狀態
		 * a:朝北
		 * b:朝東
		 * c:朝南
		 * d:朝西
		 * 切記不能用數字,因為容易與輸入的數字混淆
		 */
		String[] flag={"a","b","c","d"};
		int index=0;
		String f=flag[index];
		//機器人座標,初始座標為原點
		int x=0;
		int y=0;
		ArrayList<Integer> s=new ArrayList();
		Scanner sc=new Scanner(System.in);
		//準備資料
			while (true) {
				//退出迴圈條件
				String input = sc.next();
				if (input.equals("exit")) {
					break;
				}else {
					try {
						s.add(Integer.parseInt(input));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			}
//			sc.close();
			
//		System.out.println(s);
	   //障礙位置
		int x1=sc.nextInt();
		int y1=sc.nextInt();
		for (int i = 0; i < s.size(); i++) {
			if (s.get(i)==-1) {
				index++;
				index=(index>3)?0:index;
				f=flag[index];
			} else if(s.get(i)==-2){
				index--;
				index=(index<0)?3:index;
				f=flag[index];
			}else {
				if (s.get(i)<1||s.get(i)>9) {
					System.out.println("輸入錯誤");
					break;
				}
				if ((s.get(i)!=-2)&&(s.get(i)!=-1)) {
					switch (f) {
					case "a":
						if ((x==x1)&&(y<y1&&y1<(y+s.get(i)))) {
							y=y1-1;
						}else {
							y+=s.get(i);
						}
						
					     break;
					case "b":
					if ((y==y1)&&(x<x1&&x1<(x+s.get(i)))) {
						x=x1-1;
					}else {
						x+=s.get(i);
					}
					
					     break;
					case "c":
						if ((x==x1)&&((y-s.get(i))<y1&&y1<y)) {
							y=y1+1;
						}else {
							y-=s.get(i);
						}
						
					     break;
					case "d":
						if ((y==y1)&&((x-s.get(i))<x1&&x1<x)) {
							x=x1+1;
						}else {
							x-=s.get(i);
						}
						
						break;
					}
				}
				
				
			}
			
		}
		System.out.println(x+","+y);
		System.out.println(x*x+y*y);
		
	}

}

測試結果:

4

-2

4

exit

2

4

1,8

65

   但是這道題特坑,特麼是求最大值,不是最終值!!!

   雖然說只用再改幾行程式碼,但是也得改:

package rt;

import java.util.ArrayList;
import java.util.Scanner;

public class rt {

	public static void main(String[] args) {
		/*
		 * 當前方向狀態
		 * a:朝北
		 * b:朝東
		 * c:朝南
		 * d:朝西
		 * 切記不能用數字,因為容易與輸入的數字混淆
		 */
		String[] flag={"a","b","c","d"};
		int index=0;
		String f=flag[index];
		//機器人座標,初始座標為原點
		int x=0;
		int y=0;
		ArrayList<Integer> s=new ArrayList();
        ArrayList<Integer> h=new ArrayList();
		Scanner sc=new Scanner(System.in);
		//準備資料
			while (true) {
				//退出迴圈條件
				String input = sc.next();
				if (input.equals("exit")) {
					break;
				}else {
					try {
						s.add(Integer.parseInt(input));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			}
//			sc.close();
			
//		System.out.println(s);
	   //障礙位置
		int x1=sc.nextInt();
		int y1=sc.nextInt();
		for (int i = 0; i < s.size(); i++) {
			if (s.get(i)==-1) {
				index++;
				index=(index>3)?0:index;
				f=flag[index];
			} else if(s.get(i)==-2){
				index--;
				index=(index<0)?3:index;
				f=flag[index];
			}else {
				if (s.get(i)<1||s.get(i)>9) {
					System.out.println("輸入錯誤");
					break;
				}
				if ((s.get(i)!=-2)&&(s.get(i)!=-1)) {
					switch (f) {
					case "a":
						if ((x==x1)&&(y<y1&&y1<(y+s.get(i)))) {
							y=y1-1;
						}else {
							y+=s.get(i);
						}
						
					     break;
					case "b":
					if ((y==y1)&&(x<x1&&x1<(x+s.get(i)))) {
						x=x1-1;
					}else {
						x+=s.get(i);
					}
					
					     break;
					case "c":
						if ((x==x1)&&((y-s.get(i))<y1&&y1<y)) {
							y=y1+1;
						}else {
							y-=s.get(i);
						}
						
					     break;
					case "d":
						if ((y==y1)&&((x-s.get(i))<x1&&x1<x)) {
							x=x1+1;
						}else {
							x-=s.get(i);
						}
						
						break;
					}
				}
				
				
			}
			h.add(x*x+y*y);
		}
		collections.sort(h);
		System.out.println(h.get(h.size()-1));
		
	}

}