diff --git a/tisobe/src/B165.java b/tisobe/src/B165.java new file mode 100644 index 0000000000000000000000000000000000000000..d0a5d113d42e7eb47bc654952998af56a31efb3c --- /dev/null +++ b/tisobe/src/B165.java @@ -0,0 +1,138 @@ +package tisobe.src; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +enum Fire_situation { + TREE("#"), + TREELESS("."), + BURNED("B"), + BURING("Burning"), + ASH("A"); + + private String symbolOfMap; + + private Fire_situation(String symbolOfMap) { + this.symbolOfMap = symbolOfMap; + } + + public String getSymbol() { + return symbolOfMap; + } +} + + +public class B165 { + + public static void main(String[] args) { + // 自分の得意な言語で + // Let's チャレンジ!! + final Scanner sc = new Scanner(System.in); + + final int MAP_HEIGHT = sc.nextInt(); + final int MAP_WIDTH = sc.nextInt(); + final int FIRE_LOCATE_X = sc.nextInt(); + final int FIRE_LOCATE_Y = sc.nextInt(); + final int TIME = sc.nextInt(); + + final List> FOREST = new ArrayList<>(); + for (int i = 0; i < MAP_HEIGHT; i++) { + FOREST.add(Arrays.asList(sc.next().split(""))); + } + + Forest Forest = new Forest(MAP_HEIGHT, MAP_WIDTH, FIRE_LOCATE_X, FIRE_LOCATE_Y, FOREST); + for (int t = 0; t < TIME; t++) { + Forest.simulation_Fire(); + } + + + System.out.println(Forest.makeForestMap()); + sc.close(); + } + +} + + +class Forest { + private int map_Height; + private int map_Width; + private int fire_Locate_X; + private int fire_Locate_Y; + private List> forest; + + public Forest(final int MAP_HEIGHT, final int MAP_WIDTH, final int FIRE_LOCATE_X, + final int FIRE_LOCATE_Y, List> FOREST) { + this.fire_Locate_X = FIRE_LOCATE_X; + this.fire_Locate_Y = FIRE_LOCATE_Y; + this.map_Height = MAP_HEIGHT; + this.map_Width = MAP_WIDTH; + this.forest = FOREST; + this.forest.get(FIRE_LOCATE_X - 1).set(FIRE_LOCATE_Y - 1, Fire_situation.BURNED.getSymbol()); + } + + public List> getFOREST() { + return forest; + } + + public void simulation_Fire() { + for (int y = 0; y < map_Height; y++) { + for (int x = 0; x < map_Width; x++) { + spredFireTree(y, x); + } + } + // 燃えている箇所の確定 + for (int y = 0; y < map_Height; y++) { + Collections + .replaceAll(forest.get(y), Fire_situation.BURING.getSymbol(), Fire_situation.BURNED + .getSymbol()); + } + } + + /** + * 木("#")を燃やす TIMEに応じて炎の判定をおこなうため"Burning"で仮置き + */ + public void burningTree(final int Y, final int X) { + if (forest.get(Y).get(X).equals(Fire_situation.TREE.getSymbol())) { + forest.get(Y).set(X, Fire_situation.BURING.getSymbol()); + } + } + + /** 隣接するマスに炎を広げて、燃えている箇所を灰にする */ + public void spredFireTree(final int Y, final int X) { + if (forest.get(Y).get(X).equals(Fire_situation.BURNED.getSymbol())) { + + // 左に延焼 + if (X - 1 >= 0) { + burningTree(Y, X - 1); + } + // 上に延焼 + if (Y - 1 >= 0) { + burningTree(Y - 1, X); + } + // 右に延焼 + if (X + 1 < map_Width) { + burningTree(Y, X + 1); + } + // 下に延焼 + if (Y + 1 < map_Height) { + burningTree(Y + 1, X); + } + forest.get(Y).set(X, Fire_situation.ASH.getSymbol()); + } + } + + public String makeForestMap() { + // 結果の作成 + StringBuilder forestMap = new StringBuilder(); + for (int y = 0; y < map_Height; y++) { + for (int x = 0; x < map_Width; x++) { + forestMap.append(forest.get(y).get(x)); + } + forestMap.append("\n"); + } + return forestMap.toString(); + } +}