diff --git a/rooki/B136.java b/rooki/B136.java new file mode 100644 index 0000000000000000000000000000000000000000..3c127fda08560de03de9a8cad9f468776b133c6d --- /dev/null +++ b/rooki/B136.java @@ -0,0 +1,142 @@ +import java.util.ArrayList; +import java.util.Scanner; + +/** + * チョコレートをもらった順番と個数を記録. + * + * @author rooki + * @version 2.0 + */ + +public class B136 { + + /** + * mainメソッド. + * + * @param args 使用しない + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + + sc.nextInt(); // 移動回数の取得をスキップ + final int verticalSeatNumber = sc.nextInt(); // 縦の座席数 + final int horizontalSeatNumber = sc.nextInt(); // 横の座席数 + final int countFromFront = sc.nextInt(); // 前から何列目 + final int countFromLeft = sc.nextInt(); // 左から何列目 + final String movingRoute = sc.next(); // 移動経路を表す文字列 + + // 各席のチョコの個数を保存 + final SeatInfo seatMap = new SeatInfo(verticalSeatNumber, horizontalSeatNumber, sc); + + // 「あなた」の現在位置を保存 + final You yourPosition = new You(countFromFront, countFromLeft, seatMap); + + // 現在位置から移動した経路を保存 + final ArrayList> movingResult = yourPosition.moveSeat(movingRoute); + + // 移動結果を出力 + outputResult(seatMap, movingResult); + + sc.close(); + } + + /** + * その座席のチョコの個数を出力する. + * + * @param seatMap 席の情報を持つ + * @param movingResult 移動経路の座標を持つ二次元リスト + */ + public static void outputResult(final SeatInfo seatMap, + final ArrayList> movingResult) { + for (ArrayList position : movingResult) { + System.out.println(seatMap.getChocolateCountAt(position.get(0), position.get(1))); + } + } + + /** + * 席の情報を持つクラス. + * + */ + + static class SeatInfo { + private int[][] seatMap; + + public SeatInfo(final int verticalCount, final int horizontalCount, final Scanner sc) { + this.seatMap = new int[verticalCount][horizontalCount]; + + for (int vertical = 0; vertical < verticalCount; vertical++) { + for (int horizontal = 0; horizontal < horizontalCount; horizontal++) { + this.seatMap[vertical][horizontal] = sc.nextInt(); + } + } + } + + /** + * その座席のチョコの数を返す. + * + * @param rowCount 何行目の席か + * @param columnCount 何列目の席か + * @return 概要する座席のチョコの数を返す + */ + public int getChocolateCountAt(final int rowCount, final int columnCount) { + return seatMap[rowCount][columnCount]; + } + } + + /** + * 座席を動く「あなた」の情報を持つクラス. + * + */ + static class You { + + private int rowPosition; + private int columnPosition; + private SeatInfo seatMap; + + public You(final int rowIndexNumber, final int columnIndexNumber, final SeatInfo seatMap) { + // 配列のインデックスと「あなた」の位置を対応させる + this.rowPosition = rowIndexNumber - 1; + this.columnPosition = columnIndexNumber - 1; + this.seatMap = seatMap; + } + + /** + * 該当する方向に移動する. + * + * @param movingRoute 移動経路を表す文字列 + * @return 移動経路の座標を持つ二次元リスト + */ + public ArrayList> moveSeat(final String movingRoute) { + // 移動経路の文字列を1文字ずつ切り出す + final String[] movingDirection = movingRoute.split(""); + + ArrayList> movingResult = new ArrayList>(); + for (final String singleString : movingDirection) { + switch (singleString) { + case "F": + rowPosition -= 1; + break; + case "B": + rowPosition += 1; + break; + case "L": + columnPosition -= 1; + break; + case "R": + columnPosition += 1; + break; + default: + break; + } + + // 移動経路の座標を持つ二次元リストを作成 + ArrayList position = new ArrayList<>(); + position.add(rowPosition); + position.add(columnPosition); + + movingResult.add(position); + } + return (movingResult); + } + } +}