欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

874. Walking Robot Simulation

程序员文章站 2024-03-19 08:19:58
...

 

题目描述:

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));
		
	}

}

 

上一篇: SSL 1861 JZOJ 1365 无限序列

下一篇: