From a4c4f8991aa75d51c7bde675b0f5db9d6556d180 Mon Sep 17 00:00:00 2001 From: kshiraishi Date: Wed, 2 Jul 2025 14:02:46 +0900 Subject: [PATCH 1/2] =?UTF-8?q?B161=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kshiraishi/src/B161.java | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 kshiraishi/src/B161.java diff --git a/kshiraishi/src/B161.java b/kshiraishi/src/B161.java new file mode 100644 index 0000000..98c2ca5 --- /dev/null +++ b/kshiraishi/src/B161.java @@ -0,0 +1,93 @@ +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 i = targetLine - 1; i <= targetLine + 1; i++) { + for (int j = targetColumn - 1; j <= targetColumn + 1; j++) { + // 配列の範囲内かチェック + if (i >= 0 && i < maxLine && j >= 0 && j < maxColumn) { // 境界チェック + targetAll[i][j] = surroundingScore; + } + } + } + targetAll[targetLine][targetColumn] = targetScore; + } + + // 命中した位置の得点を追加 + public static int scoreAdd(int[][] targetAll, int hitLine, int hitColumn) { + int score = targetAll[hitLine][hitColumn]; + return score; + } +} -- GitLab From 505889806e78fea866c2c9ac48d1a98c3a27f7af Mon Sep 17 00:00:00 2001 From: kshiraishi Date: Thu, 3 Jul 2025 13:29:52 +0900 Subject: [PATCH 2/2] =?UTF-8?q?B161=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kshiraishi/src/B161.java | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/kshiraishi/src/B161.java b/kshiraishi/src/B161.java index 98c2ca5..612e664 100644 --- a/kshiraishi/src/B161.java +++ b/kshiraishi/src/B161.java @@ -14,41 +14,37 @@ public class B161 { // 変数宣言 int line, column; /** 的全体の行と列 (問題のH,W) */ /** target的の行と列 (問題のr,c) */ - List targetLine = new ArrayList<>(); + List targetLine = new ArrayList<>(); List targetColumn = new ArrayList<>(); /** target的点数, 的1マス周辺の点数 (問題のp,q) */ - List targetScore = new ArrayList<>(); + List targetScore = new ArrayList<>(); List surroundingScore = new ArrayList<>(); int targetCount; /** 的の数 (問題のN) */ - int shootingCount; /** 射撃回数 (問題のM) */ /** 命中した行と列 (問題のa,b) */ - List hitLine = new ArrayList<>(); + List hitLine = new ArrayList<>(); List hitColumn = new ArrayList<>(); int score; /** スコア合計 */ - // 変数初期化 - Scanner sc = new Scanner(System.in); - line = sc.nextInt(); - column = sc.nextInt(); + 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的の数分 点数設定+周辺の点数設定 @@ -74,11 +70,11 @@ public class B161 { public static void setTargetScore(int[][] targetAll, int targetLine, int targetColumn, int targetScore, int surroundingScore, int maxLine, int maxColumn) { - for (int i = targetLine - 1; i <= targetLine + 1; i++) { - for (int j = targetColumn - 1; j <= targetColumn + 1; j++) { + for (int row = targetLine - 1; row <= targetLine + 1; row++) { + for (int col = targetColumn - 1; col <= targetColumn + 1; col++) { // 配列の範囲内かチェック - if (i >= 0 && i < maxLine && j >= 0 && j < maxColumn) { // 境界チェック - targetAll[i][j] = surroundingScore; + if (row >= 0 && row < maxLine && col >= 0 && col < maxColumn) { // 境界チェック + targetAll[row][col] = surroundingScore; } } } -- GitLab