diff --git a/kshiraishi/src/B161.java b/kshiraishi/src/B161.java new file mode 100644 index 0000000000000000000000000000000000000000..612e664b06185ab6e84fc5d8f845c61ccdd6c915 --- /dev/null +++ b/kshiraishi/src/B161.java @@ -0,0 +1,89 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * 射撃訓練 + * + * @author kshiraishi + * @version 1 + */ +public class B161 { + + public static void main(String[] args) { + // 変数宣言 + int line, column; /** 的全体の行と列 (問題のH,W) */ + /** target的の行と列 (問題のr,c) */ + List targetLine = new ArrayList<>(); + List targetColumn = new ArrayList<>(); + /** target的点数, 的1マス周辺の点数 (問題のp,q) */ + List targetScore = new ArrayList<>(); + List surroundingScore = new ArrayList<>(); + int targetCount; /** 的の数 (問題のN) */ + int shootingCount; /** 射撃回数 (問題のM) */ + /** 命中した行と列 (問題のa,b) */ + List hitLine = new ArrayList<>(); + List hitColumn = new ArrayList<>(); + int score; /** スコア合計 */ + // 変数初期化 + Scanner sc = new Scanner(System.in); + line = sc.nextInt(); + column = sc.nextInt(); + targetCount = sc.nextInt(); + for (int i = 0; i < targetCount; i++) { + targetLine.add(sc.nextInt() - 1); + targetColumn.add(sc.nextInt() - 1); + targetScore.add(sc.nextInt()); + surroundingScore.add(sc.nextInt()); + } + shootingCount = sc.nextInt(); + for (int i = 0; i < shootingCount; i++) { + hitLine.add(sc.nextInt() - 1); + hitColumn.add(sc.nextInt() - 1); + } + score = 0; + sc.close(); + + + // 的全体作成(2次元配列ゼロで初期化) + int[][] targetAll = initializingAllTargetWithZero(line, column); + // target的の数分 点数設定+周辺の点数設定 + for (int i = 0; i < targetCount; i++) { + setTargetScore(targetAll, targetLine.get(i), targetColumn.get(i), + targetScore.get(i), surroundingScore.get(i), line, column); + } + // 射撃回数分、命中した位置の得点を追加 + for (int i = 0; i < shootingCount; i++) { + score += scoreAdd(targetAll, hitLine.get(i), hitColumn.get(i)); + } + // 結果表示 + System.out.print(score); + } + + // 的全体作成(2次元配列ゼロで初期化) + public static int[][] initializingAllTargetWithZero(int line, int column) { + int[][] targetAll = new int[line][column]; + return targetAll; + } + + // target的の点数設定 + public static void setTargetScore(int[][] targetAll, + int targetLine, int targetColumn, int targetScore, int surroundingScore, int maxLine, + int maxColumn) { + for (int row = targetLine - 1; row <= targetLine + 1; row++) { + for (int col = targetColumn - 1; col <= targetColumn + 1; col++) { + // 配列の範囲内かチェック + if (row >= 0 && row < maxLine && col >= 0 && col < maxColumn) { // 境界チェック + targetAll[row][col] = surroundingScore; + } + } + } + targetAll[targetLine][targetColumn] = targetScore; + } + + // 命中した位置の得点を追加 + public static int scoreAdd(int[][] targetAll, int hitLine, int hitColumn) { + int score = targetAll[hitLine][hitColumn]; + return score; + } +}