diff --git a/dseki/paiza/src/B096.java b/dseki/paiza/src/B096.java new file mode 100644 index 0000000000000000000000000000000000000000..93fa7195e302bb4f187d7db2a53ec08595173fb9 --- /dev/null +++ b/dseki/paiza/src/B096.java @@ -0,0 +1,137 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +/** + * 爆弾の大爆発問題. + * 爆弾の位置を"#"、爆発被害を受ける位置を"!"とする + */ +public class B096 { + private static final String FIELD_STATE_IS_BOMB = "#"; // 爆弾状態 + private static final String FIELD_STATE_IS_EXPLOSION_DAMAGE = "!"; // 爆発被害を受けている状態 + private final int height; // マスの個数(縦) + private final int width; // マスの個数(横) + + private B096(final int height, final int width) { + this.height = height; + this.width = width; + } + + /** + * メインメソッド. + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + final int height = sc.nextInt(); // 縦に何マスあるか + final int width = sc.nextInt(); // 横に何マスあるか + final B096 b096 = new B096(height, width); + final ArrayList> fieldList = new ArrayList>(); + for (int i = 0; i < height; i++) { + final String fieldState = sc.next(); + fieldList.add(b096.separateString(fieldState)); + } + b096.countNumberOfExplosionField(b096.changeRowOfExplosionField(fieldList)); + sc.close(); + } + + /** + * 受け取った文字列を1文字ずつリストに格納するメソッド. + * + * @param fieldState 入力されたフィールドの情報 + * @return 1文字ずつ区切った文字のリストを返す + **/ + private ArrayList separateString(final String fieldState) { + final String[] separatedStringArray = fieldState.split(""); + final ArrayList separatedStringList = + new ArrayList(Arrays.asList(separatedStringArray)); + + return separatedStringList; + } + + /** + * 爆発被害を受ける行の文字を変換するメソッド. + * + * @param bombPositionList 横マスにおいて爆弾がある位置のリスト + **/ + private ArrayList> changeRowOfExplosionField( + final ArrayList> fieldState) { + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j++) { + if (!(fieldState.get(i).get(j).equals(FIELD_STATE_IS_BOMB)) + && fieldState.get(i).contains(FIELD_STATE_IS_BOMB)) { + fieldState.get(i).set(j, FIELD_STATE_IS_EXPLOSION_DAMAGE); + } + } + } + return fieldState; + } + + /** + * 爆発被害を受けたマスの数を数えて出力するメソッド. + * + * @param fieldList フィールドの情報 + **/ + private void countNumberOfExplosionField(final ArrayList> fieldList) { + final ArrayList rowNumberOfBombList = new ArrayList(); + int count = 0; // 爆発マスの個数 + addRowNumberOfBombToList(fieldList, rowNumberOfBombList); + for (int i = 0; i < this.height; i++) { + changeColumnOfExplosionField(fieldList, rowNumberOfBombList, i); + count += countExplosionField(fieldList, i); + } + System.out.println(count); + + } + + /* + * 爆弾がある列番号をリストに追加するメソッド. + * + * @param fieldList フィールドの情報 + * @param rowNumberOfBombList 爆弾がある列番号が格納されるリスト + **/ + private void addRowNumberOfBombToList(final ArrayList> fieldList, + final ArrayList rowNumberOfBombList) { + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j++) { + if (fieldList.get(i).get(j).equals(FIELD_STATE_IS_BOMB) + && !(rowNumberOfBombList.contains(j))) { + rowNumberOfBombList.add(j); + } + } + } + } + + /** + * 爆発被害を受ける列の文字を変換するメソッド. + * + * @param fieldList 爆弾が設置されている場所を表すリスト + * @param rowNumberOfBombList 爆弾が設置されている列番号が格納されているリスト + * @param columnNumberOfField フィールドの行番号 + **/ + private void changeColumnOfExplosionField(final ArrayList> fieldList, + final ArrayList rowNumberOfBombList, + final int columnNumberOfField) { + for (int j = 0; j < rowNumberOfBombList.size(); j++) { + fieldList.get(columnNumberOfField).set(rowNumberOfBombList.get(j), + FIELD_STATE_IS_EXPLOSION_DAMAGE); + } + } + + /** + * 行ごとに出現した"!"をカウントし、返すメソッド. + * + * @param fieldList フィールドの情報 + * @param columnNumberOfField フィールドの行番号 + * @return "!"が出現した回数を返す + **/ + private int countExplosionField(final ArrayList> fieldList, + final int columnNumberOfField) { + int count = 0; + for (int j = 0; j < this.width; j++) { + if (fieldList.get(columnNumberOfField).get(j).equals(FIELD_STATE_IS_EXPLOSION_DAMAGE)) { + count += 1; + } + } + return count; + } +}