diff --git a/ksugawara/src/DonutB138.java b/ksugawara/src/DonutB138.java new file mode 100644 index 0000000000000000000000000000000000000000..b3659fec837595fa9c8d7caa0b443bc4a8e95d9c --- /dev/null +++ b/ksugawara/src/DonutB138.java @@ -0,0 +1,17 @@ +package ksugawara; + +/** + * ksugawara 「B138:ドーナツ」の回答プログラムのクラス. + */ +public class DonutB138 { + /** + * 与えられた条件を基に、ドーナッツ型の数をカウントするプログラム. + */ + public static void main(final String[] args) { + final DonutShapeFinder findDonutShape = new DonutShapeFinder(); + findDonutShape.storeInputValues(); + final int donutShapeNum = findDonutShape.countDonutShape(); + System.out.println(donutShapeNum); + } + +} diff --git a/ksugawara/src/DonutShapeFinder.java b/ksugawara/src/DonutShapeFinder.java new file mode 100644 index 0000000000000000000000000000000000000000..909018e9c98b29a9a222e8e46989f542bf452f29 --- /dev/null +++ b/ksugawara/src/DonutShapeFinder.java @@ -0,0 +1,71 @@ +package ksugawara; + +import java.util.Scanner; + +/** + * ドーナッツの形を見つけるクラス. + */ +class DonutShapeFinder { + private int height; // マス目の縦の大きさ + private int width; // マス目の横の大きさ + private char[][] matrix; // マス目に書かれた記号を格納する二次元配列 + + /** + * 標準入力された値を変数や配列に格納する関数. + */ + public void storeInputValues() { + final Scanner sc = new Scanner(System.in); + this.height = sc.nextInt(); + this.width = sc.nextInt(); + final String[] stringArray = new String[height]; + this.matrix = new char[height][width]; + + // 標準入力された二次元配列を一度文字列として一次元配列に格納する + for (int j = 0; j < height; j++) { + stringArray[j] = sc.next(); + } + + sc.close(); + + // 先ほどの文字列型の一次元配列を文字型で二次元配列に格納する + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + matrix[row][col] = stringArray[row].charAt(col); + } + } + } + + // 二次元配列の中に、「.」の周りが「#」だけの数をカウントする + int countDonutShape() { + int donutShapeSum = 0; + for (int row = 1; row < height - 1; row++) { + for (int col = 1; col < width - 1; col++) { + + if (isDonutShape(row, col)) { + donutShapeSum += 1; + } + } + } + return donutShapeSum; + } + + // matrix[row][col]の周りの記号がすべて「#」であり、中心が「.」の場合tureを返す。 + boolean isDonutShape(final int row, final int col) { + + for (int rowCheck = -1; rowCheck <= 1; rowCheck++) { + for (int colCheck = -1; colCheck <= 1; colCheck++) { + final int newRow = row + rowCheck; + final int newCol = col + colCheck; + if (rowCheck == 0 && colCheck == 0) { + if (matrix[newRow][newCol] != '.') { + return false; + } + } else if (matrix[newRow][newCol] != '#') { + return false; + } + } + } + return true; + } + +} diff --git a/ksugawara/src/FindDounutShape.java b/ksugawara/src/FindDounutShape.java new file mode 100644 index 0000000000000000000000000000000000000000..909018e9c98b29a9a222e8e46989f542bf452f29 --- /dev/null +++ b/ksugawara/src/FindDounutShape.java @@ -0,0 +1,71 @@ +package ksugawara; + +import java.util.Scanner; + +/** + * ドーナッツの形を見つけるクラス. + */ +class DonutShapeFinder { + private int height; // マス目の縦の大きさ + private int width; // マス目の横の大きさ + private char[][] matrix; // マス目に書かれた記号を格納する二次元配列 + + /** + * 標準入力された値を変数や配列に格納する関数. + */ + public void storeInputValues() { + final Scanner sc = new Scanner(System.in); + this.height = sc.nextInt(); + this.width = sc.nextInt(); + final String[] stringArray = new String[height]; + this.matrix = new char[height][width]; + + // 標準入力された二次元配列を一度文字列として一次元配列に格納する + for (int j = 0; j < height; j++) { + stringArray[j] = sc.next(); + } + + sc.close(); + + // 先ほどの文字列型の一次元配列を文字型で二次元配列に格納する + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + matrix[row][col] = stringArray[row].charAt(col); + } + } + } + + // 二次元配列の中に、「.」の周りが「#」だけの数をカウントする + int countDonutShape() { + int donutShapeSum = 0; + for (int row = 1; row < height - 1; row++) { + for (int col = 1; col < width - 1; col++) { + + if (isDonutShape(row, col)) { + donutShapeSum += 1; + } + } + } + return donutShapeSum; + } + + // matrix[row][col]の周りの記号がすべて「#」であり、中心が「.」の場合tureを返す。 + boolean isDonutShape(final int row, final int col) { + + for (int rowCheck = -1; rowCheck <= 1; rowCheck++) { + for (int colCheck = -1; colCheck <= 1; colCheck++) { + final int newRow = row + rowCheck; + final int newCol = col + colCheck; + if (rowCheck == 0 && colCheck == 0) { + if (matrix[newRow][newCol] != '.') { + return false; + } + } else if (matrix[newRow][newCol] != '#') { + return false; + } + } + } + return true; + } + +} diff --git a/ksugawara/src/FindDounutShapeB138.java b/ksugawara/src/FindDounutShapeB138.java new file mode 100644 index 0000000000000000000000000000000000000000..f3ed46870ef69a910151e11a4755de8b07ace6fb --- /dev/null +++ b/ksugawara/src/FindDounutShapeB138.java @@ -0,0 +1,17 @@ +package ksugawara; + +/** + * ksugawara 「B138:ドーナツ」の回答プログラムのクラス. + */ +public class FindDounutShapeB138 { + /** + * 与えられた条件を基に、ドーナッツ型の数をカウントするプログラム. + */ + public static void main(final String[] args) { + final DonutShapeFinder findDonutShape = new DonutShapeFinder(); + findDonutShape.storeInputValues(); + final int donutShapeNum = findDonutShape.countDonutShape(); + System.out.println(donutShapeNum); + } + +} diff --git a/ksugawara/test/DonutB138Test.java b/ksugawara/test/DonutB138Test.java new file mode 100644 index 0000000000000000000000000000000000000000..f86049e4a66b44f7d908d9cc398151868619c249 --- /dev/null +++ b/ksugawara/test/DonutB138Test.java @@ -0,0 +1,67 @@ +package ksugawara; + +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import org.junit.Before; +import org.junit.Test; + +/** + * B138のテストケース. + */ +public class DonutB138Test { + private DonutShapeFinder findDonutShape; + + @Before // 各テストの実行前に呼ばれる + public void setUp() { + findDonutShape = new DonutShapeFinder(); + } + + @Test + public void test34() { + final String input = "3 4\n####\n#.#.#\n####\n#...#\n####\n"; // 入力文字列の作成 + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); // 入力ストリム作成文字列をバイト配列 + System.setIn(inputStream); // 標準入力として設定 + + findDonutShape.storeInputValues(); // 標準入力された値を基に変数や配列に値を格納する + final int actual = findDonutShape.countDonutShape(); // ドーナッツ型の形を数える関数を動かし実際の挙動の値を格納する + final int expected = 1; // 正しく実行された場合の値を格納する + assertEquals(expected, actual); // 実際の値と予測した値が同じ場合、テスト合格 + } + + @Test + public void test44() { + final String input = "4 3\n###\n#.#\n###\n#.#"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDonutShape.storeInputValues(); + final int actual = findDonutShape.countDonutShape(); + final int expected = 1; + assertEquals(expected, actual); + } + + @Test + public void test55() { + final String input = "5 5\n#####\n#...#\n#.#.#\n#...#\n#####\n"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDonutShape.storeInputValues(); + final int actual = findDonutShape.countDonutShape(); + final int expected = 0; + assertEquals(expected, actual); + } + + @Test + public void test77() { + final String input = "7 7\n#######\n#.#.#.#\n#######\n#.#.#.#\n#######\n#.#.#.#\n#######"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDonutShape.storeInputValues(); + final int actual = findDonutShape.countDonutShape(); + final int expected = 9; + assertEquals(expected, actual); + } +} diff --git a/ksugawara/test/FindDounutShapeB138Test.java b/ksugawara/test/FindDounutShapeB138Test.java new file mode 100644 index 0000000000000000000000000000000000000000..2193d9f3cabf37e3af89a8b0ff65edea0fa49cd8 --- /dev/null +++ b/ksugawara/test/FindDounutShapeB138Test.java @@ -0,0 +1,68 @@ +package ksugawara; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import org.junit.Before; +import org.junit.Test; + +/** + * B138のテストケース. + */ +public class FindDounutShapeB138Test { + private FindDounutShape findDounutShape; + + @Before // 各テストの実行前に呼ばれる + public void setUp() { + findDounutShape = new FindDounutShape(); + } + + @Test + public void test34() { + final String input = "3 4\n####\n#.#.#\n####\n#...#\n####\n"; // 入力文字列の作成 + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); // 入力ストリム作成文字列をバイト配列 + System.setIn(inputStream); // 標準入力として設定 + + findDounutShape.storeInputValues(); // 標準入力された値を基に変数や配列に値を格納する + final int actual = findDounutShape.getDounutShapeCount(); // ドーナッツ型の形を数える関数を動かし実際の挙動の値を格納する + final int expected = 1; // 正しく実行された場合の値を格納する + assertEquals(expected, actual); // 実際の値と予測した値が同じ場合、テスト合格 + } + + @Test + public void test44() { + final String input = "4 3\n###\n#.#\n###\n#.#"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDounutShape.storeInputValues(); + final int actual = findDounutShape.getDounutShapeCount(); + final int expected = 1; + assertEquals(expected, actual); + } + + @Test + public void test55() { + final String input = "5 5\n#####\n#...#\n#.#.#\n#...#\n#####\n"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDounutShape.storeInputValues(); + final int actual = findDounutShape.getDounutShapeCount(); + final int expected = 0; + assertEquals(expected, actual); + } + + @Test + public void test77() { + final String input = "7 7\n#######\n#.#.#.#\n#######\n#.#.#.#\n#######\n#.#.#.#\n#######"; + final InputStream inputStream = new ByteArrayInputStream(input.getBytes()); + System.setIn(inputStream); + + findDounutShape.storeInputValues(); + final int actual = findDounutShape.getDounutShapeCount(); + final int expected = 9; + assertEquals(expected, actual); + } +}