diff --git a/htabuchi/src/B131_htabuchi.java b/htabuchi/src/B131_htabuchi.java new file mode 100644 index 0000000000000000000000000000000000000000..741835f33a5a14db7f5e597bd83f9e0519b08a67 --- /dev/null +++ b/htabuchi/src/B131_htabuchi.java @@ -0,0 +1,160 @@ +/** + * Paiza問題B131:運賃計算
+ * create 2025/07/02 + * + * @author 田渕日奈子 + * @version 1.0 + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B131_htabuchi { + final static int MatrixColumn = 2; + + public TrainMapData scanningRequiredValue() { + /** 路線や駅、その金額を読み取り */ + Scanner sc = new Scanner(System.in); + int lineTrain = sc.nextInt(); /** 路線 */ + int numStation = sc.nextInt(); /** 駅数 */ + List allPriceList = new ArrayList<>(); + + /** 路線ごとの金額一覧(注意:最初の駅1から任意の駅nまでの値段) */ + for (int line = 0; line < lineTrain; line++) { + List fee = new ArrayList<>(); + for (int column = 0; column < numStation; column++) { + fee.add(sc.nextInt()); + } + /** 路線コード、各駅の運賃を格納 */ + allPriceList.add(new AllLineInfo(line, fee)); + } + + int requiredRoute = sc.nextInt(); + List requiredRouteList = new ArrayList<>(); + + /** 通る路線と駅を格納 */ + for (int route = 0; route < requiredRoute; route++) { + int nextLine = sc.nextInt(); + int nextStation = sc.nextInt(); + requiredRouteList.add(new int[] { + nextLine, nextStation + }); + } + + TrainMapData dto = new TrainMapData(allPriceList, requiredRouteList, requiredRoute); + + sc.close(); + return dto; + } + + /** 路線ごとの情報を格納する */ + private class AllLineInfo { + private int code; + private List fee; + + public AllLineInfo(int code, List fee) { + this.code = code; + this.fee = fee; + } + + /** 目的の区間の金額を算出 */ + public int getSectionPrice(int forwardStation, int backStation) { + return fee.get(forwardStation) - fee.get(backStation); + } + } + + /** AllLineInfo(路線ごとの情報)含めた経路や駅、金額の情報を格納 */ + private class TrainMapData { + /** すべての金額表 */ + private List allPriceList; + /** 通る路線(row)と駅(column) */ + private List requiredRouteList; + /** 通るべき駅の数 */ + private int requiredRoute; + + public TrainMapData(final List allPriceList, final List requiredRouteList, + final int requiredRoute) { + this.allPriceList = allPriceList; + this.requiredRouteList = requiredRouteList; + this.requiredRoute = requiredRoute; + } + + /** 以下ゲッターとセッター */ + public int getRequiredRoute() { + return requiredRoute; + } + + public int[] getRequiredRouteList(int num) { + return requiredRouteList.get(num); + } + + public AllLineInfo getAllPriceList(int line) { + return allPriceList.get(line); + } + + } + + /** 目的の経路までにかかる金額の算出 */ + public static int judgePrice(TrainMapData dto) { + int curLine = 0; /** 現在の路線 */ + int curStation = 0; /** 現在の駅 */ + int sumPrice = 0; + + for (int i = 0; i < dto.getRequiredRoute(); i++) { + /** 目指すべき駅の場所をallPrice配列用に置き換える */ + int[] nextInfo = dto.getRequiredRouteList(i); + int nextLine = nextInfo[0] - 1; + int nextStation = nextInfo[1] - 1; + + /** 今の路線が次の目的の路線とあっているか確認 */ + if (nextLine == curLine) { + /** 変更なし */ + } else { + /** 同じ駅で路線変更 */ + curLine = nextLine; + } + /** 今の駅の位置と目的の駅の位置から金額算出 */ + if (curStation == nextStation) { + /** 同じ駅は金額0 */ + } else if (curStation < nextStation) { + /** 今の駅よりも目的の駅が先 */ + sumPrice += culcSectionFoward(dto, curLine, curStation, nextLine, nextStation); + curStation = nextStation; + } else { + /** 今の駅よりも目的の駅が後ろ */ + sumPrice += culcSectionBack(dto, curLine, curStation, nextLine, nextStation); + curStation = nextStation; + } + + } + + return sumPrice; + } + + /** 区間ごとの金額を算出する */ + /** パターン1:目的駅が現在駅よりも前 パターン2:目的駅が現在駅よりも後ろ */ + public static int culcSectionFoward(TrainMapData dto, int curLine, int curStation, int nextLine, + int nextStation) { + AllLineInfo LineInfo = dto.getAllPriceList(curLine); /** 今の路線の金額情報獲得 */ + return LineInfo.getSectionPrice(nextStation, curStation); + } + + public static int culcSectionBack(TrainMapData dto, int curLine, int curStation, int nextLine, + int nextStation) { + AllLineInfo LineInfo = dto.getAllPriceList(curLine); /** 今の路線の金額情報獲得 */ + return LineInfo.getSectionPrice(curStation, nextStation); + } + + public static void displayResultPrice(int sumPrice) { + /** 表示 */ + System.out.println(sumPrice); + } + + public static void main(String[] args) { + B131_htabuchi program = new B131_htabuchi(); + TrainMapData dto = program.scanningRequiredValue(); + int sumPrice = judgePrice(dto); + displayResultPrice(sumPrice); + } +}