diff --git a/ykoiso/src/B091_Peak.java b/ykoiso/src/B091_Peak.java new file mode 100644 index 0000000000000000000000000000000000000000..8a7706b102c997e92b0bf9515203aba9fc07e759 --- /dev/null +++ b/ykoiso/src/B091_Peak.java @@ -0,0 +1,77 @@ +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 (x < 0 || x >= range || y < 0 || y >= range) { + throw new IllegalArgumentException("入力値に異常があります。"); + } + 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/src/B096_Bomb.java b/ykoiso/src/B096_Bomb.java new file mode 100644 index 0000000000000000000000000000000000000000..d214f18d858ef0feb245eb6a7f533dabff69409b --- /dev/null +++ b/ykoiso/src/B096_Bomb.java @@ -0,0 +1,83 @@ +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(); + 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.explosion(); + System.out.println(explosionMap.countBombArea()); + 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 explosion() { + 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 countBombArea() { + 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/B091_PeakTest.java b/ykoiso/test/B091_PeakTest.java new file mode 100644 index 0000000000000000000000000000000000000000..888f80a706080a093db7912900df786511e138a5 --- /dev/null +++ b/ykoiso/test/B091_PeakTest.java @@ -0,0 +1,84 @@ +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(91); + 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(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); + 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); } + */ +} diff --git a/ykoiso/test/B096_BombTest.java b/ykoiso/test/B096_BombTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0c40deb646e4980e9cc5dd3a38f3711b1b4b419f --- /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.explosion(); + final int actual = sut.countBombArea(); + final int expected = 5 + 5 - 1; + assertThat(actual, is(expected)); + } + + @Test + public void つながった2つの十字の爆発を計算できる() { + String[][] map = + {{".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",}, {".", "#", ".", "#", ".",}, + {".", ".", ".", ".", ".",}, {".", ".", ".", ".", ".",},}; + sut = bomb.new ExplosionMap(map, 5, 5); + sut.explosion(); + final int actual = sut.countBombArea(); + 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.explosion(); + final int actual = sut.countBombArea(); + final int expected = 0; + assertThat(actual, is(expected)); + } +}