From ce095b73c2b29d18ff7fc9f5459b189fbf6c90d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 25 Jul 2022 10:37:31 +0900 Subject: [PATCH 1/5] =?UTF-8?q?B091=20=E5=B1=B1=E9=A0=82=E3=82=92=E6=8E=A2?= =?UTF-8?q?=E3=81=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B091_Peak.java | 74 ++++++++++++++++++++++++++++++++ ykoiso/test/B091_PeakTest.java | 77 ++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 ykoiso/src/B091_Peak.java create mode 100644 ykoiso/test/B091_PeakTest.java diff --git a/ykoiso/src/B091_Peak.java b/ykoiso/src/B091_Peak.java new file mode 100644 index 0000000..d04f173 --- /dev/null +++ b/ykoiso/src/B091_Peak.java @@ -0,0 +1,74 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class B091_Peak { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int range = scan.nextInt(); + // マスの数読み込み + // 配列のが良い + List> elevation = new ArrayList<>(); + for (int i = 0; i < range; i++) { + elevation.add(new ArrayList<>()); + } + // 各標高の読み込み + for (int y = 0; y < range; y++) { + for (int x = 0; x < range; x++) { + int num = scan.nextInt(); + elevation.get(y).add(num); + } + } + // 答え出力用のリスト + List peakList = new ArrayList<>(); + for (int y = 0; y < range; y++) { + for (int x = 0; x < range; x++) { + // 頂上かの判定 + if (isPeak(elevation, range, x, y)) { + peakList.add(elevation.get(y).get(x)); + } + } + } + // 降順ソート + Collections.sort(peakList, Collections.reverseOrder()); + // 答えの出力 + for (int i = 0; i < peakList.size(); i++) { + System.out.println(peakList.get(i)); + } + scan.close(); + } + + // 頂点かどうかの判定 + public static boolean isPeak(final List> elevation, final int range, final int x, + final int y) { + final int target = elevation.get(y).get(x); + // 各辺に面しているかの判定 + // 最大2辺までなので角かつ0でも問題なし + if (y != 0) { + final int targetTop = elevation.get(y - 1).get(x); + if (target <= targetTop) { + return false; + } + } + if (y != range - 1) { + final int targetBottom = elevation.get(y + 1).get(x); + if (target <= targetBottom) { + return false; + } + } + if (x != 0) { + final int targetLeft = elevation.get(y).get(x - 1); + if (target <= targetLeft) { + return false; + } + } + if (x != range - 1) { + final int targetRight = elevation.get(y).get(x + 1); + if (target <= targetRight) { + return false; + } + } + return true; + } +} diff --git a/ykoiso/test/B091_PeakTest.java b/ykoiso/test/B091_PeakTest.java new file mode 100644 index 0000000..369e3c9 --- /dev/null +++ b/ykoiso/test/B091_PeakTest.java @@ -0,0 +1,77 @@ +import static org.junit.Assert.assertThat; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import static org.hamcrest.CoreMatchers.*; + +public class B091_PeakTest { + List> elevation = new ArrayList<>(); + + @Before + public void setUp() throws Exception { + for (int i = 0; i < 3; i++) { + elevation.add(new ArrayList<>()); + } + } + + @Test + public void 真ん中が頂上であることを判断できる() { + elevation.get(0).add(0); + elevation.get(0).add(0); + elevation.get(0).add(70); + elevation.get(1).add(89); + elevation.get(1).add(90); + elevation.get(1).add(0); + elevation.get(2).add(60); + elevation.get(2).add(75); + elevation.get(2).add(0); + boolean actual = B091_Peak.isPeak(elevation, 3, 1, 1); + assertThat(actual, is(true)); + } + + @Test + public void 隣接マスが同じ高さの時頂上であることを判断されない() { + elevation.get(0).add(0); + elevation.get(0).add(90); + elevation.get(0).add(70); + elevation.get(1).add(89); + elevation.get(1).add(90); + elevation.get(1).add(0); + elevation.get(2).add(60); + elevation.get(2).add(75); + elevation.get(2).add(0); + boolean actual = B091_Peak.isPeak(elevation, 3, 1, 1); + assertThat(actual, is(false)); + } + + @Test + public void 隣接マスがより高い時頂上であることを判断されない() { + elevation.get(0).add(0); + elevation.get(0).add(250); + elevation.get(0).add(70); + elevation.get(1).add(89); + elevation.get(1).add(90); + elevation.get(1).add(0); + elevation.get(2).add(60); + elevation.get(2).add(75); + elevation.get(2).add(0); + boolean actual = B091_Peak.isPeak(elevation, 3, 1, 1); + assertThat(actual, is(false)); + } + + @Test + public void 斜めのマスがより高い時頂上であることを判断できる() { + elevation.get(0).add(999); + elevation.get(0).add(50); + elevation.get(0).add(70); + elevation.get(1).add(89); + elevation.get(1).add(90); + elevation.get(1).add(0); + elevation.get(2).add(60); + elevation.get(2).add(75); + elevation.get(2).add(0); + boolean actual = B091_Peak.isPeak(elevation, 3, 1, 1); + assertThat(actual, is(true)); + } +} -- GitLab From d967b966c682e3fccfaff9d934116e119ea3561f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 25 Jul 2022 13:15:14 +0900 Subject: [PATCH 2/5] =?UTF-8?q?B091=20=E5=B1=B1=E9=A0=82=E3=82=92=E6=8E=A2?= =?UTF-8?q?=E3=81=9B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B091_Peak.java | 4 ++++ ykoiso/test/B091_PeakTest.java | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ykoiso/src/B091_Peak.java b/ykoiso/src/B091_Peak.java index d04f173..524ec85 100644 --- a/ykoiso/src/B091_Peak.java +++ b/ykoiso/src/B091_Peak.java @@ -45,6 +45,10 @@ public class B091_Peak { final int target = elevation.get(y).get(x); // 各辺に面しているかの判定 // 最大2辺までなので角かつ0でも問題なし + if (x < 0 || x >= range || y < 0 || y >= range) { + System.out.println("入力値に異常があります。"); + throw new IllegalArgumentException(); + } if (y != 0) { final int targetTop = elevation.get(y - 1).get(x); if (target <= targetTop) { diff --git a/ykoiso/test/B091_PeakTest.java b/ykoiso/test/B091_PeakTest.java index 369e3c9..888f80a 100644 --- a/ykoiso/test/B091_PeakTest.java +++ b/ykoiso/test/B091_PeakTest.java @@ -48,7 +48,7 @@ public class B091_PeakTest { @Test public void 隣接マスがより高い時頂上であることを判断されない() { elevation.get(0).add(0); - elevation.get(0).add(250); + elevation.get(0).add(91); elevation.get(0).add(70); elevation.get(1).add(89); elevation.get(1).add(90); @@ -62,7 +62,7 @@ public class B091_PeakTest { @Test public void 斜めのマスがより高い時頂上であることを判断できる() { - elevation.get(0).add(999); + elevation.get(0).add(91); elevation.get(0).add(50); elevation.get(0).add(70); elevation.get(1).add(89); @@ -74,4 +74,11 @@ public class B091_PeakTest { boolean actual = B091_Peak.isPeak(elevation, 3, 1, 1); assertThat(actual, is(true)); } + /* + * @Test(expected = IllegalArgumentException.class) public void + * 座標の引数がマイナスの時IllegalArgumentExceptionを投げる() throws Exception { elevation.get(0).add(91); + * elevation.get(0).add(50); elevation.get(0).add(70); elevation.get(1).add(89); + * elevation.get(1).add(90); elevation.get(1).add(0); elevation.get(2).add(60); + * elevation.get(2).add(75); elevation.get(2).add(0); B091_Peak.isPeak(elevation, 3, -1, 1); } + */ } -- GitLab From a5042d478178f01e945c580783157b3be8924c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 25 Jul 2022 13:21:10 +0900 Subject: [PATCH 3/5] =?UTF-8?q?B091=20=E5=B1=B1=E9=A0=82=E3=82=92=E6=8E=A2?= =?UTF-8?q?=E3=81=9B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B091_Peak.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ykoiso/src/B091_Peak.java b/ykoiso/src/B091_Peak.java index 524ec85..8a7706b 100644 --- a/ykoiso/src/B091_Peak.java +++ b/ykoiso/src/B091_Peak.java @@ -46,8 +46,7 @@ public class B091_Peak { // 各辺に面しているかの判定 // 最大2辺までなので角かつ0でも問題なし if (x < 0 || x >= range || y < 0 || y >= range) { - System.out.println("入力値に異常があります。"); - throw new IllegalArgumentException(); + throw new IllegalArgumentException("入力値に異常があります。"); } if (y != 0) { final int targetTop = elevation.get(y - 1).get(x); -- GitLab From 47cd64c576bd07f24066561aee393cbc38104991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Tue, 26 Jul 2022 11:48:37 +0900 Subject: [PATCH 4/5] =?UTF-8?q?B096=20=E7=88=86=E5=BC=BE=E3=81=AE=E5=A4=A7?= =?UTF-8?q?=E7=88=86=E7=99=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B096_Bomb.java | 82 ++++++++++++++++++++++++++++++++++ ykoiso/test/B096_BombTest.java | 51 +++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 ykoiso/src/B096_Bomb.java create mode 100644 ykoiso/test/B096_BombTest.java diff --git a/ykoiso/src/B096_Bomb.java b/ykoiso/src/B096_Bomb.java new file mode 100644 index 0000000..a166952 --- /dev/null +++ b/ykoiso/src/B096_Bomb.java @@ -0,0 +1,82 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class B096_Bomb { + // マップ記号のマップ + public static Map STR = new HashMap<>(); + { + STR.put("BOMB", "#"); + STR.put("NON", "."); + } + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 読み込み + final int height = scan.nextInt(); + final int width = scan.nextInt(); + String[][] map = new String[height][width]; + for (int h = 0; h < height; h++) { + String str = scan.next(); + for (int w = 0; w < width; w++) { + map[h][w] = str.substring(w, w + 1); + } + } + // 爆発範囲クラスの生成 + B096_Bomb bomb = new B096_Bomb(); + ExplosionMap explosionMap = bomb.new ExplosionMap(map, height, width); + // 爆発範囲の計算 + explosionMap.calcExplosion(); + System.out.println(explosionMap.getBombArea()); + scan.close(); + } + + class ExplosionMap { + private int height; + private int width; + private String[][] map; + private boolean[][] isExplosion; + + // コンストラクタ + ExplosionMap(final String[][] map, final int height, final int width) { + this.map = map; + this.height = height; + this.width = width; + isExplosion = new boolean[height][width]; + } + + // 爆発全体の計算 + public void calcExplosion() { + for (int h = 0; h < height; h++) { + for (int w = 0; w < width; w++) { + if (map[h][w].equals(STR.get("BOMB"))) { + checkExplosion(h, w); + } + } + } + } + + // 縦横への爆発範囲の計算 + public void checkExplosion(final int targetH, final int targetW) { + for (int h = 0; h < height; h++) { + isExplosion[h][targetW] = true; + } + for (int w = 0; w < width; w++) { + isExplosion[targetH][w] = true; + } + } + + // 爆発箇所の計算 + public int getBombArea() { + int explosionNum = 0; + for (int h = 0; h < height; h++) { + for (int w = 0; w < width; w++) { + if (isExplosion[h][w]) { + explosionNum++; + } + } + } + return explosionNum; + } + } +} diff --git a/ykoiso/test/B096_BombTest.java b/ykoiso/test/B096_BombTest.java new file mode 100644 index 0000000..fa706b4 --- /dev/null +++ b/ykoiso/test/B096_BombTest.java @@ -0,0 +1,51 @@ +import static org.junit.Assert.assertThat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; + +public class B096_BombTest { + B096_Bomb bomb = new B096_Bomb(); + B096_Bomb.ExplosionMap sut; + + @Test + public void 十字の爆発を計算できる() { + String[][] map = + {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", "#", ".", ".",}, + {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; + sut = bomb.new ExplosionMap(map, 5, 5); + sut.calcExplosion(); + final int actual = sut.getBombArea(); + final int expected = 5 + 5 - 1; + assertThat(actual, is(expected)); + } + + @Test + public void つながった2つの十字の爆発を計算できる() { + String[][] map = + {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", "#", ".", "#", ".",}, + {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; + sut = bomb.new ExplosionMap(map, 5, 5); + sut.calcExplosion(); + final int actual = sut.getBombArea(); + final int expected = 5 * 2 + 5 - 2; + assertThat(actual, is(expected)); + } + + @Test + public void 爆発がない場合0を返す() { + String[][] map = + {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, + {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; + sut = bomb.new ExplosionMap(map, 5, 5); + sut.calcExplosion(); + final int actual = sut.getBombArea(); + final int expected = 0; + assertThat(actual, is(expected)); + } +} -- GitLab From e468402419436112099a7483b760821798d06f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Tue, 26 Jul 2022 14:44:59 +0900 Subject: [PATCH 5/5] =?UTF-8?q?B096=20=E7=88=86=E5=BC=BE=E3=81=AE=E5=A4=A7?= =?UTF-8?q?=E7=88=86=E7=99=BA=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B096_Bomb.java | 15 ++++++++------- ykoiso/test/B096_BombTest.java | 12 ++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ykoiso/src/B096_Bomb.java b/ykoiso/src/B096_Bomb.java index a166952..d214f18 100644 --- a/ykoiso/src/B096_Bomb.java +++ b/ykoiso/src/B096_Bomb.java @@ -18,16 +18,17 @@ public class B096_Bomb { String[][] map = new String[height][width]; for (int h = 0; h < height; h++) { String str = scan.next(); - for (int w = 0; w < width; w++) { - map[h][w] = str.substring(w, w + 1); - } + map[h] = str.split(""); + /* + * for (int w = 0; w < width; w++) { map[h][w] = str.substring(w, w + 1); } + */ } // 爆発範囲クラスの生成 B096_Bomb bomb = new B096_Bomb(); ExplosionMap explosionMap = bomb.new ExplosionMap(map, height, width); // 爆発範囲の計算 - explosionMap.calcExplosion(); - System.out.println(explosionMap.getBombArea()); + explosionMap.explosion(); + System.out.println(explosionMap.countBombArea()); scan.close(); } @@ -46,7 +47,7 @@ public class B096_Bomb { } // 爆発全体の計算 - public void calcExplosion() { + public void explosion() { for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { if (map[h][w].equals(STR.get("BOMB"))) { @@ -67,7 +68,7 @@ public class B096_Bomb { } // 爆発箇所の計算 - public int getBombArea() { + public int countBombArea() { int explosionNum = 0; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { diff --git a/ykoiso/test/B096_BombTest.java b/ykoiso/test/B096_BombTest.java index fa706b4..0c40deb 100644 --- a/ykoiso/test/B096_BombTest.java +++ b/ykoiso/test/B096_BombTest.java @@ -19,8 +19,8 @@ public class B096_BombTest { {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", "#", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; sut = bomb.new ExplosionMap(map, 5, 5); - sut.calcExplosion(); - final int actual = sut.getBombArea(); + sut.explosion(); + final int actual = sut.countBombArea(); final int expected = 5 + 5 - 1; assertThat(actual, is(expected)); } @@ -31,8 +31,8 @@ public class B096_BombTest { {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", "#", ".", "#", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; sut = bomb.new ExplosionMap(map, 5, 5); - sut.calcExplosion(); - final int actual = sut.getBombArea(); + sut.explosion(); + final int actual = sut.countBombArea(); final int expected = 5 * 2 + 5 - 2; assertThat(actual, is(expected)); } @@ -43,8 +43,8 @@ public class B096_BombTest { {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; sut = bomb.new ExplosionMap(map, 5, 5); - sut.calcExplosion(); - final int actual = sut.getBombArea(); + sut.explosion(); + final int actual = sut.countBombArea(); final int expected = 0; assertThat(actual, is(expected)); } -- GitLab