From 2c3dfc5943b73239bcb01c3ef4394630d666c6be Mon Sep 17 00:00:00 2001 From: kurisu Date: Thu, 24 Jul 2025 13:33:46 +0900 Subject: [PATCH] =?UTF-8?q?paizaB164=E3=81=AE=E5=9B=9E=E7=AD=94=E3=81=A8?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kurisu/src/B164.java | 137 +++++++++++++++++++++++++++++++++++++ kurisu/src/B164Test.java | 144 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 kurisu/src/B164.java create mode 100644 kurisu/src/B164Test.java diff --git a/kurisu/src/B164.java b/kurisu/src/B164.java new file mode 100644 index 0000000..c58edc8 --- /dev/null +++ b/kurisu/src/B164.java @@ -0,0 +1,137 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +enum Direction { + NORTHDIRECTION(Arrays.asList(-1, 0)), + SOUTHDIRECTION(Arrays.asList(1, 0)), + EASTDIRECTION(Arrays.asList(0, -1)), + WESTDIRECTION(Arrays.asList(0, 1)); + + private final List directions; + + Direction(List directions) { + this.directions = directions; + } + + public List getDirections() { + return directions; + } + +} + + +public class B164 { + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + final int vertical = sc.nextInt(); + final int horizontal = sc.nextInt(); + final int digTree = sc.nextInt(); + + List> areaSizes = new ArrayList<>(); + + for (int row = 0; row < vertical; row++) { + List rowData = new ArrayList<>(); + for (int column = 0; column < horizontal; column++) { + rowData.add(sc.nextInt()); + } + areaSizes.add(rowData); + } + + Forest forest = new Forest(vertical, horizontal, areaSizes); + Squirrel squirrel = new Squirrel(digTree); + + final int nuts = squirrel.findDiggingMaxNuts(forest); + + System.out.println(nuts); + + sc.close(); + } +} + + +class Forest { + private final List> areaSizes; + private final int vertical; + private final int horizontal; + + Forest(int vertical, int horizontal, List> areaSizes) { + this.vertical = vertical; + this.horizontal = horizontal; + this.areaSizes = areaSizes; + } + + public List> getAreaSizes() { + return areaSizes; + } + + public int getVertical() { + return vertical; + } + + public int getHorizontal() { + return horizontal; + } + + //有効なエリアなのか判別 + public boolean validForestArea(int row, int column) { + return row >= 0 && row < vertical && column >= 0 && column < horizontal; + } + + //場所ごとの木の実の個数を取得 + public int getNuts(int row, int column) { + if (row >= 0 && row < vertical && column >= 0 && column < horizontal) { + return areaSizes.get(row).get(column); + } + return -1; + } +} + + +class Squirrel { + private final int digTree; + + Squirrel(int digTree) { + this.digTree = digTree; + } + + // 木の実を最大個数を求める + public int findDiggingMaxNuts(Forest forest) { + int maxNuts = 0; + for (int startRow = 0; startRow < forest.getVertical(); startRow++) { + for (int startColumn = 0; startColumn < forest.getHorizontal(); startColumn++) { + maxNuts = Math.max(maxNuts, findMaxNutsFromStartPoint(forest, startRow, startColumn)); + } + } + return maxNuts; + } + + // 指定された開始地点からの木の実最大個数を求める + private int findMaxNutsFromStartPoint(Forest forest, int startRow, int startColumn) { + int maxNuts = 0; + for (Direction direction : Direction.values()) { + int numNuts = diggingNutsFromStartPoint(forest, startRow, startColumn, direction + .getDirections() + .get(0), + direction.getDirections().get(1)); + maxNuts = Math.max(maxNuts, numNuts); + } + return maxNuts; + } + + // 開始地点から掘り出した木の実の個数の合計 + private int diggingNutsFromStartPoint(Forest forest, int startRow, int startColumn, + int directionColumn, int directionRow) { + int totalNuts = 0; + for (int i = 0; i < digTree; i++) { + int currentRow = startRow + directionRow * i; + int currentColumn = startColumn + directionColumn * i; + if (!forest.validForestArea(currentRow, currentColumn)) { + return 0; + } + totalNuts += forest.getNuts(currentRow, currentColumn); + } + return totalNuts; + } +} diff --git a/kurisu/src/B164Test.java b/kurisu/src/B164Test.java new file mode 100644 index 0000000..16c6207 --- /dev/null +++ b/kurisu/src/B164Test.java @@ -0,0 +1,144 @@ +import static org.junit.Assert.*; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +public class B164Test { + final int vertical = 5; + final int horizontal = 5; + final int digTree = 3; + + public List> template() { + List> areaSizes = Arrays + .asList( + Arrays.asList(8, 6, 9, 1, 2), + Arrays.asList(7, 3, 9, 5, 8), + Arrays.asList(8, 3, 7, 5, 9), + Arrays.asList(9, 7, 7, 3, 3), + Arrays.asList(9, 7, 2, 6, 1)); + + return areaSizes; + } + + + public class DirectionTest { + @Test + public void directionTest() { + Map> expectedMap = Map + .of( + Direction.NORTHDIRECTION, Arrays.asList(-1, 0), + Direction.SOUTHDIRECTION, Arrays.asList(1, 0), + Direction.EASTDIRECTION, Arrays.asList(0, -1), + Direction.WESTDIRECTION, Arrays.asList(0, 1)); + + for (Direction direction : Direction.values()) { + List expected = expectedMap.get(direction); + assertEquals(expected, direction.getDirections()); + } + } + } + + @Test + public void forestTest() { + List> areaSizes = template(); + List> expectedAreaSizes = Arrays + .asList( + Arrays.asList(8, 6, 9, 1, 2), + Arrays.asList(7, 3, 9, 5, 8), + Arrays.asList(8, 3, 7, 5, 9), + Arrays.asList(9, 7, 7, 3, 3), + Arrays.asList(9, 7, 2, 6, 1)); + + assertEquals(vertical, areaSizes.size()); + assertEquals(horizontal, areaSizes.get(0).size()); + assertEquals(expectedAreaSizes, areaSizes); + } + + @Test + public void getAreaSizesTest() { + List> areaSizes = template(); + List> expectedAreaSizes = Arrays + .asList( + Arrays.asList(8, 6, 9, 1, 2), + Arrays.asList(7, 3, 9, 5, 8), + Arrays.asList(8, 3, 7, 5, 9), + Arrays.asList(9, 7, 7, 3, 3), + Arrays.asList(9, 7, 2, 6, 1)); + Forest forest = new Forest(vertical, horizontal, areaSizes); + assertEquals(expectedAreaSizes, forest.getAreaSizes()); + } + + @Test + public void getVerticalTest() { + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + assertEquals(vertical, forest.getVertical()); + } + + @Test + public void getHorizontalTest() { + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + assertEquals(horizontal, forest.getHorizontal()); + } + + @Test + public void validForestAreaTrueTest() { + final int expectedVertical = 4; + final int expectedHorizontal = 4; + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + + assertEquals(true, forest.validForestArea(expectedVertical, expectedHorizontal)); + } + + @Test + public void validForestAreaFalseTest() { + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + + assertEquals(false, forest.validForestArea(vertical, horizontal)); + } + + @Test + public void getNuts関数が木の実の個数を返す処理() { + final int expectedVertical = 4; + final int expectedHorizontal = 4; + final int expectedNum = 1; + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + assertEquals(expectedNum, forest.getNuts(expectedVertical, expectedHorizontal)); + } + + @Test + public void getNuts関数が負の値を返す処理() { + final int expectedNum = -1; + List> areaSizes = template(); + Forest forest = new Forest(vertical, horizontal, areaSizes); + assertEquals(expectedNum, forest.getNuts(vertical, horizontal)); + } + + @Test + public void findDiggingMaxNutsTestがtotalNutsを返す処理() { + final int expectedMaxNuts = 26; + List> areaSizes = template(); + + Forest forest = new Forest(vertical, horizontal, areaSizes); + Squirrel squirrel = new Squirrel(digTree); + + assertEquals(expectedMaxNuts, squirrel.findDiggingMaxNuts(forest)); + } + + @Test + public void findDiggingMaxNutsTestが0を返す処理() { + final int expectedMaxNuts = 0; + final int expectedDigTree = 6; + List> areaSizes = template(); + + Forest forest = new Forest(vertical, horizontal, areaSizes); + Squirrel squirrel = new Squirrel(expectedDigTree); + + assertEquals(expectedMaxNuts, squirrel.findDiggingMaxNuts(forest)); + } +} -- GitLab