diff --git a/anakamura/src/B136.java b/anakamura/src/B136.java new file mode 100644 index 0000000000000000000000000000000000000000..584350db96e6b7f63400e7c7a9c9233e67797ce1 --- /dev/null +++ b/anakamura/src/B136.java @@ -0,0 +1,102 @@ +package anakamura.src; + +import java.util.Scanner; + +public class B136 { + public static void main(String[] args) { + + final Scanner sc = new Scanner(System.in); + + final int numberOfMoves = sc.nextInt(); // 移動回数 + final int colsOfMatrix = sc.nextInt(); // 縦の列の数 + final int rowsOfMatrix = sc.nextInt(); // 横の列の数 + + final int startCols = sc.nextInt(); // 自分の縦の場所 + final int startRows = sc.nextInt();// 自分の横の場所 + + final String movedRoute = sc.next();// 移動経路 + + // 文字列を1文字ずつに分割する + char[] routeChars = new char[movedRoute.length()]; + for (int i = 0; i < movedRoute.length(); i++) { + char movement = (movedRoute.charAt(i)); + routeChars[i] = movement; + } + Position position = new Position(startRows, startCols); + + // 前からi列目左からj列目に座っているクラスメイトがくれるチョコレートの数 + final int[][] matrix = new int[colsOfMatrix][rowsOfMatrix]; + for (int x = 0; x < colsOfMatrix; x++) { + for (int y = 0; y < rowsOfMatrix; y++) { + matrix[x][y] = sc.nextInt(); + } + } + sc.close(); + + int chocolateCounts[] = new int[numberOfMoves]; + // 1文字ずつ条件にあったメソッドを動かす + for (int i = 0; i < numberOfMoves; i++) { + switch (routeChars[i]) { + case 'F': + position.moveFront(); + break; + case 'B': + position.moveBack(); + break; + case 'L': + position.moveLeft(); + break; + case 'R': + position.moveRight(); + break; + } + // チョコの個数を記述 + chocolateCounts[i] = matrix[position.getCols()][position.getRows()]; + } + // 出力 + for (int num : chocolateCounts) { + System.out.println(num); + } + } + +} + + +class Position { + private int rows, cols; + + public Position(int initialRows, int initialCols) { + this.cols = initialCols - 1; + this.rows = initialRows - 1; + + } + + public int getRows() { + return rows; + } + + public int getCols() { + return cols; + + } + + // 前方向に移動する場合 + public void moveFront() { + cols -= 1; + } + + // 後方向に移動する場合 + public void moveBack() { + cols += 1; + } + + // 左方向に移動する場合 + public void moveLeft() { + rows -= 1; + } + + // 右方向に移動する場合 + public void moveRight() { + rows += 1; + } +}