diff --git a/yitou/src/Bomb.java b/yitou/src/Bomb.java new file mode 100644 index 0000000000000000000000000000000000000000..f4076752ffa8b9e2d5500384371e8b0440e350bb --- /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 height = scanner.nextInt(); + final int weight = scanner.nextInt(); + scanner.nextLine(); // 次の行に移動 + + // フィールドのインスタンスを作成 + final Field field = new Field(height,weight ); + + // フィールドの情報を格納 + for (int i = 0; i < height; 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 0000000000000000000000000000000000000000..b90ae514cee39df2c3bcb8f029425981de40f2a3 --- /dev/null +++ b/yitou/src/Field.java @@ -0,0 +1,54 @@ +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( (rowIndex + 1) + " 行目の入力数が間違っています。期待される長さ: " + width + ", 実際の長さ: " + row.length()); + } + this.grid[rowIndex] = row.toCharArray(); + } + + //テスト用のgetGridメソッドを追加 + public char[][] getGrid() { + return grid; + } + + public int calculateAffectedCells() { + final Set affectedCells = new HashSet<>(); + + 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 diff --git a/yitou/test/BombTest.java b/yitou/test/BombTest.java new file mode 100644 index 0000000000000000000000000000000000000000..527e4cb1afd767846bbd9c626b500a1b66e89531 --- /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()); + } +}