POJ 3682

http://acm.pku.edu.cn/JudgeOnline/problem?id=2632

一道模拟题,在一个二维数组里,每个机器人占一格。通过命令来控制机器人的运行,结果又转到墙上,与其他机器人相撞,或成功。

 

代码中定义了一个Robot,和Command的内部类。

机器人有四个方向 static String ds = "NESW";//方向

对应每个方向坐标增量数组为:static int[] XI = {0,1,0,-1};//x轴增量

  static int[] YI = {1,0,-1,0};//y轴增量

static int[][] v ;为存在标志

 

在代码中最重要的是处理好机器人的转向问题:

                //向右转
		private boolean r(int repeat) {
			repeat = repeat > 4?repeat %=4:repeat;
			w =  ds.charAt((ds.indexOf(w) + repeat)%4);
			return true;
		}
		//向左转
		private boolean L(int repeat) {
			repeat = repeat > 4?repeat %=4:repeat;
			w =  ds.charAt((ds.indexOf(w) - repeat + 4)%4);
			return true;
		}

 

源代码:

package com.woxiaoe.acm.pku.P2632;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	static int A,B;
	static String ds = "NESW";//方向
	static int[] XI = {0,1,0,-1};//x轴增量
	static int[] YI = {1,0,-1,0};//y轴增量
	static int[][] v ;
	static boolean report = false;
	static PrintWriter out = new PrintWriter(System.out);
	static class Robot{
		int id,x,y;
		char w;
		Robot(int id,int x ,int y, char w){
			this.id = id;
			this.x = x;
			this.y = y;
			this.w = w;
			v[x][y] = id;
		}
		/**
		 * 返回true继续
		 * @param command
		 * @param repeat
		 * @return
		 */
		boolean action(char command,int repeat){
			switch (command) {
			case 'F':
				return f(repeat);
			case 'L':
				return L(repeat);
			case 'R':
				return r(repeat);
			}
			return false;
		}
		//向右转
		private boolean r(int repeat) {
			repeat = repeat > 4?repeat %=4:repeat;
			w =  ds.charAt((ds.indexOf(w) + repeat)%4);
			return true;
		}
		//向左转
		private boolean L(int repeat) {
			repeat = repeat > 4?repeat %=4:repeat;
			w =  ds.charAt((ds.indexOf(w) - repeat + 4)%4);
			return true;
		}
		//前进
		private boolean f(int repeat) {
			int xi = XI[ds.indexOf(w)];
			int yi = YI[ds.indexOf(w)];
			int nx ;
			int ny;
			//System.out.println("nx:" + nx + ", ny:" + ny);
			ny = y;
			nx = x;
			v[x][y] = 0;
			for(int i = 0; i < repeat; i++){
				if(xi == 0){
					ny +=yi;
				}else{
					nx +=xi;
				}
				//注意顺序
				if((nx <= 0 || nx > A) ||(ny <= 0 || ny > B)){
					if(!report){
						out.println("Robot " + id + " crashes into the wall");
						report = true;
					}
					return false;
				}
				if(v[nx][ny] != 0){//撞到机器人
					if(!report){
						out.println("Robot " + id + " crashes into robot " + v[nx][ny]);
						report = true;
					}
					return false;
				}
			}
				
			v[nx][ny] = id;
			x = nx;
			y = ny;
			return true;
		}
	}
	static class Command{
		Robot robot;
		int repeat;
		char command;
		public Command(Robot robot,char command,int repeat) {
			this.robot = robot;
			this.command = command;
			this.repeat = repeat;
		}
		public boolean run(){
			return robot.action(command, repeat);
		}
		
	}
	public static void main(String[] args) {
		//Scanner scn = new Scanner(System.in);
		Scanner scn = new Scanner(Main.class.getResourceAsStream("in.dat"));
		int n = scn.nextInt();
		int robotNum,commandNum;//机器人数,命令数
		while(n-- != 0){
			List<Robot> robots = new ArrayList<Robot>();
			List<Command> commands = new ArrayList<Command>();
			v = new int[110][110];
			A = scn.nextInt();
			B = scn.nextInt();
			report = false;
			boolean ok = true;
			robotNum = scn.nextInt();
			commandNum = scn.nextInt();
			for(int i = 0; i < robotNum; i++){
				robots.add(new Robot(i + 1, scn.nextInt(), scn.nextInt(), scn.next().charAt(0)));
			}
			for(int i = 0; i < commandNum; i++){
				commands.add(new Command(robots.get(scn.nextInt() - 1),scn.next().charAt(0),scn.nextInt()));
			}
			for(int i = 0; i < commandNum; i++){
				if(!commands.get(i).run()){
					ok = false;
					break;
				}
			}
			if(ok){
				out.println("OK");
			}
		}
		out.flush();
	}

}
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值