0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza 勉強記録【ロボットの暴走】【Java】

Posted at

paizaでの勉強記録です。
今回は下記のレベルアップ問題集のAランク問題の「ロボットの暴走」を実施しました。
一応テストケースはすべてOKでしたが、模範解答と比べるとレベル毎の移動マス数の処理など冗長なところが多いなと感じました。。。

ただ一つ文句が言いたい、標準入力で与えられるロボットの初期位置の最大範囲ってのは何なんだったんだ、、使わないやんけ、、

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			
//			入力はされるが使わない変数
			int x_max = sc.nextInt();
			int y_max = sc.nextInt();
			
			int robotCount = sc.nextInt();
			int moveCount = sc.nextInt();
			
			ToolBox[] toolPlaceList = new ToolBox[10];
			Robot[] robotList = new Robot[robotCount];
			
//			工具箱の位置の取得処理
			for (int i = 0; i < 10; i++) {
				int tool_x = sc.nextInt();
				int tool_y = sc.nextInt();
				toolPlaceList[i] = new ToolBox(tool_x,tool_y);
			}
			
//			ロボットの情報取得処理
			for (int i = 0; i < robotCount; i++) {
				int robot_x = sc.nextInt();
				int robot_y = sc.nextInt();
				int robot_level = sc.nextInt();
				
//				取得した情報の格納
				robotList[i]= new Robot(robot_x,robot_y,robot_level); 
			}
			
//			移動処理
			for (int i = 0; i < moveCount; i++) {
//				移動するロボットの情報取得
				int robotNumber = sc.nextInt();
				String compass = sc.next();

				Robot robot = robotList[robotNumber-1];
				robot.move(compass);
				
//				移動先に工具箱があればレベルアップ
				for (int j = 0; j < 10; j++) {
					ToolBox toolBox = toolPlaceList[j];
					if (robot.place[0]==toolBox.place[0]&&robot.place[1]==toolBox.place[1]) {
						robot.levelup();
					}
				}
			}
			
//			標準出力処理
			for (int i = 0; i < robotCount; i++) {
				Robot robot = robotList[i];
				System.out.println(String.format("%d %d %d", robot.place[0],robot.place[1],robot.level));
			}
		}
	}
}

class Robot {
	int[] place = new int[] {0,0};
	int level = 0;
	int moveDistance = 1;
	
//	コンストラクタ
	public Robot(int robot_x, int robot_y, int robot_level) {
		place[0] = robot_x;
		place[1] = robot_y;
		level = robot_level;
		
	    if (1==level) {
	    	moveDistance = 1;
	    } else if (2==level) {
			moveDistance = 2;
		} else if (3==level){
			moveDistance = 5;
		} else if (4==level) {
			moveDistance = 10;
		}
	}
			
//	移動処理
	public void move(String compass) {
		switch (compass) {
		case "N":
			place[1] -= moveDistance;
			break;
		case "S":
			place[1] += moveDistance;
			break;
		case "E":
			place[0] += moveDistance;
			break;
		case "W":
			place[0] -= moveDistance;
			break;
		default:
			break;
		}
	}
	
//	レベルアップ処理
	public void levelup() {
//		レベルが4未満の時レベルアップ
		if (4>level) {
			level++;
		} else {
			return;
		}
		
//		上がったレベルに応じて移動距離を増加させる
		if (2==level) {
			moveDistance = 2;
		} else if (3==level){
			moveDistance = 5;
		} else if (4==level) {
			moveDistance = 10;
		}
	}
}

class ToolBox {
	int [] place = new int[] {0,0};
	
//	コンストラクタ
	public ToolBox(int tool_x,int tool_y) {
		this.place[0] = tool_x;
		this.place[1] = tool_y;
	}
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?