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

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值