diff --git a/rsuzuki/README.md b/rsuzuki/README.md new file mode 100644 index 0000000000000000000000000000000000000000..796c95d353f961f632c60d81ef2bdf62fa8ea5ff --- /dev/null +++ b/rsuzuki/README.md @@ -0,0 +1,2 @@ +# Paiza +knoda用 diff --git a/rsuzuki/src/B136_chocolate.java b/rsuzuki/src/B136_chocolate.java new file mode 100644 index 0000000000000000000000000000000000000000..df80074f088be9976ee7f2c0ced8b3f4cdba9af0 --- /dev/null +++ b/rsuzuki/src/B136_chocolate.java @@ -0,0 +1,190 @@ +package paiza_c; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** データを保持するクラス */ +final class ChocoData { + /** 移動回数 */ + private final int movingTime; + /** 縦の席数 */ + private final int ySeat; + /** 横の席数 */ + private final int xSeat; + /** 自分の座席がどこか */ + private int yPlayer; + private int xPlayer; + /** 移動経路FBLR */ + private final String movingCommand; + /** 座席でもらえるチョコの数を格納する配列 */ + final int[][] chocoNumber; + + /** + * ChocoDataの新しいインスタンスを生成 + * + * @param movingTime 移動回数 + * @param ySeat 縦の席数 + * @param xSeat 横の席数 + * @param yPlayer プレイヤーの初期縦位置 + * @param xPlayer プレイヤーの初期横位置 + * @param movingCommand 移動コマンド文字列 + * @param chocoNumber 各座席のチョコの数を格納する二次元配列 + */ + public ChocoData(int movingTime, int ySeat, int xSeat, int yPlayer, int xPlayer, String movingCommand, + int[][] chocoNumber) { + this.movingTime = movingTime; + this.ySeat = ySeat; + this.xSeat = xSeat; + this.yPlayer = yPlayer; + this.xPlayer = xPlayer; + this.movingCommand = movingCommand; + this.chocoNumber = chocoNumber; + } + + public int getMovingTime() { + return movingTime; + } + + public int getYSear() { + return ySeat; + } + + public int getXSeat() { + return xSeat; + } + + public int getYPlayer() { + return yPlayer; + } + + public int getXPlayer() { + return xPlayer; + } + + public String getMovingCommand() { + return movingCommand; + } + + public int getChocoNumber(int y, int x) { + return chocoNumber[y][x]; + } + + public void setSy(int sy) { + this.yPlayer = sy; + } + + public void setSx(int sx) { + this.xPlayer = sx; + } +} + +/** + * ロジック + */ +final class ChocoLogic { + private final ChocoData data; + /** 集計用リスト */ + private final List chocoValues; + + public ChocoLogic(ChocoData data) { + this.data = data; + this.chocoValues = new ArrayList<>(); + } + + /** + * 移動コマンドに基づいてプレイヤーを移動させ、通過した座席のチョコの数を集計 + */ + public void Commands() { + for (int i = 0; i < data.getMovingTime(); i++) { + char command = data.getMovingCommand().charAt(i); + movePlayer(command); + chocoValues.add(data.getChocoNumber(data.getYPlayer(), data.getXPlayer())); + } + } + + /** + * 指定されたコマンドに基づいてプレイヤーを移動 + */ + private void movePlayer(char command) { + int yPlayer = data.getYPlayer(); + int xPlayer = data.getXPlayer(); + int ySeat = data.getYSear(); + int xSeat = data.getXSeat(); + + if (command == 'F') { // 前進 (上) + if (yPlayer > 0) { + data.setSy(yPlayer - 1); + } + } else if (command == 'B') { // 後退 (下) + if (yPlayer < ySeat - 1) { + data.setSy(yPlayer + 1); + } + } else if (command == 'L') { // 左 + if (xPlayer > 0) { + data.setSx(xPlayer - 1); + } + } else if (command == 'R') { // 右 + if (xPlayer < xSeat - 1) { + data.setSx(xPlayer + 1); + } + } + } + + /** + * 集計されたチョコの数のリストを取得 + * + * @return チョコの数のリスト + */ + public List getChocoValues() { + return chocoValues; + } +} + +/** 結果を出力するクラス */ +final class ChocoResult { + public void printResult(List chocoValues) { + for (int value : chocoValues) { + System.out.println(value); + } + } + +} + +public class B136_chocolate { + + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + final int movingTime = sc.nextInt(); + final int ySeat = sc.nextInt(); + final int xSeat = sc.nextInt(); + int yPlayer = sc.nextInt(); + int xPlayer = sc.nextInt(); + + sc.nextLine();// 残った改行文字を消費 + + final String movingCommand = sc.nextLine(); + + // HxW のグリッドを宣言し、読み込む + final int[][] chocoNumber = new int[ySeat][xSeat]; + for (int i = 1; i < ySeat; i++) { + for (int j = 1; j < xSeat; j++) { + chocoNumber[i][j] = sc.nextInt(); + } + } + + // チョコデータオブジェクトを生成 + ChocoData data = new ChocoData(movingTime, ySeat, xSeat, yPlayer, xPlayer, movingCommand, chocoNumber); + + // チョコロジックオブジェクトを生成し、コマンドを実行 + ChocoLogic chocoLogic = new ChocoLogic(data); + chocoLogic.Commands(); + + // チョコ結果オブジェクトを生成し、結果を出力 + ChocoResult chocoResult = new ChocoResult(); + chocoResult.printResult(chocoLogic.getChocoValues()); + + sc.close(); + + } +}