From b549145308afef3b041d7a2b6b287e3742115869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Wed, 16 Oct 2024 17:59:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E5=95=8F=E9=A1=8CB096?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Bomb.java | 25 ++++++++++++++++++++ yitou/src/Field.java | 50 ++++++++++++++++++++++++++++++++++++++++ yitou/test/BombTest.java | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 yitou/src/Bomb.java create mode 100644 yitou/src/Field.java create mode 100644 yitou/test/BombTest.java diff --git a/yitou/src/Bomb.java b/yitou/src/Bomb.java new file mode 100644 index 0000000..6301858 --- /dev/null +++ b/yitou/src/Bomb.java @@ -0,0 +1,25 @@ +package src; + +import java.util.Scanner; + +public class Bomb { + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + + // フィールドの行数 line と列数 column を取得 + final int line = scanner.nextInt(); + final int column = scanner.nextInt(); + scanner.nextLine(); // 次の行に移動 + + // フィールドのインスタンスを作成 + final Field field = new Field(line, column); + + // フィールドの情報を格納 + for (int i = 0; i < line; i++) { + field.setRow(i, scanner.nextLine()); + } + + // 最終的な影響を受けるマスの数を出力 + System.out.println(field.calculateAffectedCells()); + } +} \ No newline at end of file diff --git a/yitou/src/Field.java b/yitou/src/Field.java new file mode 100644 index 0000000..3c4b931 --- /dev/null +++ b/yitou/src/Field.java @@ -0,0 +1,50 @@ +package src; + +import java.util.HashSet; +import java.util.Set; + +// フィールドを管理するクラス +public class Field { + private final char[][] grid; + private final int height; + private final int width; + + public Field(final int height, final int width) { + this.height = height; + this.width = width; + this.grid = new char[height][width]; + } + + public void setRow(final int rowIndex, final String row) { + if (row.length() != width) { + throw new IllegalArgumentException("入力数が間違ってます"); + } + this.grid[rowIndex] = row.toCharArray(); + } + +//テスト用のgetGridメソッドを追加 + public char[][] getGrid() { + return grid; + } + + public int calculateAffectedCells() { + final Set affectedCells = new HashSet<>(); + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + if (grid[i][j] == '#') { + // 縦の行を記録 + for (int k = 0; k < width; k++) { + affectedCells.add(i + "," + k); + } + // 横の列を記録 + for (int k = 0; k < height; k++) { + affectedCells.add(k + "," + j); + } + } + } + } + + return affectedCells.size(); + } +} \ No newline at end of file diff --git a/yitou/test/BombTest.java b/yitou/test/BombTest.java new file mode 100644 index 0000000..527e4cb --- /dev/null +++ b/yitou/test/BombTest.java @@ -0,0 +1,44 @@ +package test; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import src.Field; + +class BombTest { + + @Test + public void シャープが0行目0列2列および2行目2列にある時12を返す() { + final Field field = new Field(4, 4); + field.setRow(0, "#.#."); + field.setRow(1, "...."); + field.setRow(2, "..#."); + field.setRow(3, "...."); + + // 影響を受けるマスの数を確認 + final int affectedCells = field.calculateAffectedCells(); + assertEquals(12, affectedCells); + + // 期待されるグリッドの確認 + assertArrayEquals(new char[][]{ + {'#', '.', '#', '.'}, + {'.', '.', '.', '.'}, + {'.', '.', '#', '.'}, + {'.', '.', '.', '.'} + }, field.getGrid()); + } + + @Test + public void シャープが0行目0列および1行目1列にある時() { + final Field field = new Field(2, 2); + field.setRow(0, "#."); + field.setRow(1, ".#"); + + // 影響を受けるマスの数を確認 + final int affectedCells = field.calculateAffectedCells(); + assertEquals(4, affectedCells); + + // 期待されるグリッドの確認 + assertArrayEquals(new char[][]{{'#', '.'}, {'.', '#'}}, field.getGrid()); + } +} -- GitLab From 23b37618a802cb43ac459a949280c350dc70f1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Thu, 17 Oct 2024 11:35:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?B096=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Bomb.java | 8 ++++---- yitou/src/Field.java | 34 +++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/yitou/src/Bomb.java b/yitou/src/Bomb.java index 6301858..f407675 100644 --- a/yitou/src/Bomb.java +++ b/yitou/src/Bomb.java @@ -7,15 +7,15 @@ public class Bomb { final Scanner scanner = new Scanner(System.in); // フィールドの行数 line と列数 column を取得 - final int line = scanner.nextInt(); - final int column = scanner.nextInt(); + final int height = scanner.nextInt(); + final int weight = scanner.nextInt(); scanner.nextLine(); // 次の行に移動 // フィールドのインスタンスを作成 - final Field field = new Field(line, column); + final Field field = new Field(height,weight ); // フィールドの情報を格納 - for (int i = 0; i < line; i++) { + for (int i = 0; i < height; i++) { field.setRow(i, scanner.nextLine()); } diff --git a/yitou/src/Field.java b/yitou/src/Field.java index 3c4b931..b90ae51 100644 --- a/yitou/src/Field.java +++ b/yitou/src/Field.java @@ -17,34 +17,38 @@ public class Field { public void setRow(final int rowIndex, final String row) { if (row.length() != width) { - throw new IllegalArgumentException("入力数が間違ってます"); + throw new IllegalArgumentException( (rowIndex + 1) + " 行目の入力数が間違っています。期待される長さ: " + width + ", 実際の長さ: " + row.length()); } this.grid[rowIndex] = row.toCharArray(); } -//テスト用のgetGridメソッドを追加 + //テスト用のgetGridメソッドを追加 public char[][] getGrid() { - return grid; + return grid; } public int calculateAffectedCells() { final Set affectedCells = new HashSet<>(); - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - if (grid[i][j] == '#') { - // 縦の行を記録 - for (int k = 0; k < width; k++) { - affectedCells.add(i + "," + k); - } - // 横の列を記録 - for (int k = 0; k < height; k++) { - affectedCells.add(k + "," + j); - } + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + if (grid[row][col] != '#') { + continue; // '#' でない場合は次のループへ } + recordAffectedCells(affectedCells, row, col); } } - return affectedCells.size(); } + + private void recordAffectedCells(final Set affectedCells, final int row, final int col) { + // 縦の行を記録 + for (int affectedCol = 0; affectedCol < width; affectedCol++) { + affectedCells.add(row + "," + affectedCol); + } + // 横の列を記録 + for (int affectedRow = 0; affectedRow < height; affectedRow++) { + affectedCells.add(affectedRow + "," + col); + } + } } \ No newline at end of file -- GitLab