From 9d77cff863f674e8ccec4b517a023dbd85525745 Mon Sep 17 00:00:00 2001 From: rooki Date: Fri, 4 Jul 2025 15:30:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB136=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B136.java | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 rooki/B136.java diff --git a/rooki/B136.java b/rooki/B136.java new file mode 100644 index 0000000..7a84af7 --- /dev/null +++ b/rooki/B136.java @@ -0,0 +1,122 @@ +import java.util.Scanner; + +/** + * チョコレートをもらった順番と個数を記録. + * + * @author rooki + * @version 1.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 seat = new SeatInfo(verticalSeatNumber, horizontalSeatNumber, sc); + + // 「あなた」の現在位置を保存 + final You yourPosition = new You(countFromFront, countFromLeft, seat); + + // 現在位置から移動 + yourPosition.moveSeat(movingRoute); + + sc.close(); + } + + /** + * 席の情報を持つクラス. + * + */ + + static class SeatInfo { + private int[][] seat; + + public SeatInfo(final int verticalCount, final int horizontalCount, final Scanner sc) { + this.seat = new int[verticalCount][horizontalCount]; + + for (int vertical = 0; vertical < verticalCount; vertical++) { + for (int horizontal = 0; horizontal < horizontalCount; horizontal++) { + this.seat[vertical][horizontal] = sc.nextInt(); + } + } + } + + /** + * その座席のチョコの数を返す. + * + * @param lineCount 何行目の席か + * @param ColumnCount 何列目の席か + * @return 概要する座席のチョコの数を返す + */ + public int getValue(final int lineCount, final int colmnCount) { + return seat[lineCount][colmnCount]; + } + } + + /** + * 座席を動く「あなた」の情報を持つクラス. + * + */ + static class You { + + private int linePosition; + private int columnPosition; + private SeatInfo seat; + + public You(final int lineIndexNumber, final int columnIndexNumber, final SeatInfo seat) { + //配列のインデックスと「あなた」の位置を対応させる + this.linePosition = lineIndexNumber - 1; + this.columnPosition = columnIndexNumber - 1; + this.seat = seat; + } + + /** + * 該当する方向に移動する. + * + * @param movingRoute 移動経路を表す文字列 + */ + public void moveSeat(final String movingRoute) { + // 移動経路の文字列を1文字ずつ切り出す + final String[] movingDirection = movingRoute.split(""); + for (final String singleString : movingDirection) { + switch (singleString) { + case "F": + linePosition -= 1; + outputResult(); + break; + case "B": + linePosition += 1; + outputResult(); + break; + case "L": + columnPosition -= 1; + outputResult(); + break; + case "R": + columnPosition += 1; + outputResult(); + break; + default: + break; + } + } + } + + public void outputResult() { + System.out.println(seat.getValue(linePosition, columnPosition)); + } + } +} -- GitLab From c01d46997de536770ba9733b154caffe4211bc97 Mon Sep 17 00:00:00 2001 From: rooki Date: Mon, 7 Jul 2025 15:25:51 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB136=E3=81=AE?= =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B136.java | 78 +++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/rooki/B136.java b/rooki/B136.java index 7a84af7..3c127fd 100644 --- a/rooki/B136.java +++ b/rooki/B136.java @@ -1,10 +1,11 @@ +import java.util.ArrayList; import java.util.Scanner; /** * チョコレートをもらった順番と個数を記録. * * @author rooki - * @version 1.0 + * @version 2.0 */ public class B136 { @@ -25,31 +26,47 @@ public class B136 { final String movingRoute = sc.next(); // 移動経路を表す文字列 // 各席のチョコの個数を保存 - final SeatInfo seat = new SeatInfo(verticalSeatNumber, horizontalSeatNumber, sc); + final SeatInfo seatMap = new SeatInfo(verticalSeatNumber, horizontalSeatNumber, sc); // 「あなた」の現在位置を保存 - final You yourPosition = new You(countFromFront, countFromLeft, seat); + final You yourPosition = new You(countFromFront, countFromLeft, seatMap); - // 現在位置から移動 - yourPosition.moveSeat(movingRoute); + // 現在位置から移動した経路を保存 + 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[][] seat; + private int[][] seatMap; public SeatInfo(final int verticalCount, final int horizontalCount, final Scanner sc) { - this.seat = new int[verticalCount][horizontalCount]; + this.seatMap = new int[verticalCount][horizontalCount]; for (int vertical = 0; vertical < verticalCount; vertical++) { for (int horizontal = 0; horizontal < horizontalCount; horizontal++) { - this.seat[vertical][horizontal] = sc.nextInt(); + this.seatMap[vertical][horizontal] = sc.nextInt(); } } } @@ -57,12 +74,12 @@ public class B136 { /** * その座席のチョコの数を返す. * - * @param lineCount 何行目の席か - * @param ColumnCount 何列目の席か + * @param rowCount 何行目の席か + * @param columnCount 何列目の席か * @return 概要する座席のチョコの数を返す */ - public int getValue(final int lineCount, final int colmnCount) { - return seat[lineCount][colmnCount]; + public int getChocolateCountAt(final int rowCount, final int columnCount) { + return seatMap[rowCount][columnCount]; } } @@ -72,51 +89,54 @@ public class B136 { */ static class You { - private int linePosition; + private int rowPosition; private int columnPosition; - private SeatInfo seat; + private SeatInfo seatMap; - public You(final int lineIndexNumber, final int columnIndexNumber, final SeatInfo seat) { - //配列のインデックスと「あなた」の位置を対応させる - this.linePosition = lineIndexNumber - 1; + public You(final int rowIndexNumber, final int columnIndexNumber, final SeatInfo seatMap) { + // 配列のインデックスと「あなた」の位置を対応させる + this.rowPosition = rowIndexNumber - 1; this.columnPosition = columnIndexNumber - 1; - this.seat = seat; + this.seatMap = seatMap; } /** * 該当する方向に移動する. * * @param movingRoute 移動経路を表す文字列 + * @return 移動経路の座標を持つ二次元リスト */ - public void moveSeat(final String movingRoute) { + 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": - linePosition -= 1; - outputResult(); + rowPosition -= 1; break; case "B": - linePosition += 1; - outputResult(); + rowPosition += 1; break; case "L": columnPosition -= 1; - outputResult(); break; case "R": columnPosition += 1; - outputResult(); break; default: break; } - } - } - public void outputResult() { - System.out.println(seat.getValue(linePosition, columnPosition)); + // 移動経路の座標を持つ二次元リストを作成 + ArrayList position = new ArrayList<>(); + position.add(rowPosition); + position.add(columnPosition); + + movingResult.add(position); + } + return (movingResult); } } } -- GitLab