[백준 3025] 돌 던지기

[백준 3025] 돌 던지기

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static int R, C, N, command; static char[][] map; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().split(" "); R = Integer.parseInt(input[0]); C = Integer.parseInt(input[1]); map = new char[R][C]; for (int i = 0; i < R; i++) { map[i] = br.readLine().toCharArray(); } N = Integer.parseInt(br.readLine()); for (int i = 0; i < N; i++) { command = Integer.parseInt(br.readLine())-1; if (map[0][command] != '.') { continue; } falling(-1, command); // 화산탄 떨어뜨리기 수행 } for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { sb.append(map[i][j]); } sb.append("

"); } System.out.println(sb.toString()); } // 화산탄이 떨어지는 것을 수행하는 함수 static void falling(int x, int y) { while (true) { x++; // 아래로 한칸 떨어뜨리기 if (x >= R || map[x][y] == 'X') { // 땅이거나 장애물에 막혀있다면 굳힘 map[x - 1][y] = 'O'; return; } if (map[x][y] == 'O') { // 화산탄이라면 멈춤 break; } } if (0 <= y - 1 && map[x][y - 1] == '.' && map[x - 1][y - 1] == '.') { // 왼쪽 인덱스 범위 확인 & 왼쪽, 왼쪽-아래칸 비어있는지 확인 falling(x, y - 1); // 왼쪽 아래칸으로 굴러 떨어지기 수행 } else if (y + 1 < C && map[x][y + 1] == '.' && map[x - 1][y + 1] == '.') { // 오른쪽 인덱스 범위 확인 & 오른쪽, 오른쪽-아래칸 비어있는지 확인 falling(x, y + 1); // 오른쪽 아래칸으로 굴러 떨어지기 수행 } else { // 왼쪽 오른쪽으로 굴러 떨어지지 않는다면 굳힘 map[x - 1][y] = 'O'; } } }

from http://hillier.tistory.com/118 by ccl(A) rewrite - 2021-09-08 02:27:18