반응형
package boj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class boj8972 {
static int R, C;
static int map[][];
static int dy[] = { 1, 1, 1, 0, 0, 0, -1, -1, -1 };
static int dx[] = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
static int Jx, Jy;
static char[] sequence;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String in[] = br.readLine().split(" ");
R = Integer.parseInt(in[0]);
C = Integer.parseInt(in[1]);
map = new int[R][C];
for (int i = 0; i < R; i++) {
char str[] = br.readLine().toCharArray();
for (int j = 0; j < C; j++) {
char c = str[j];
if (c == 'I') {
Jx = j;
Jy = i;
} else if (c == 'R') {
map[i][j] = 1;
}
}
}
sequence = br.readLine().toCharArray();
System.out.println(Solve());
}
private static String Solve() {
StringBuilder sb = new StringBuilder();
for (int s = 0; s < sequence.length; s++) {
int d = sequence[s] - '1';
Jy += dy[d];
Jx += dx[d];
if (map[Jy][Jx] != 0)
return "kraj " + (s + 1);
Move();
if (!Process())
return "kraj " + (s + 1);
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (Jy == i && Jx == j)
sb.append('I');
else if (map[i][j] == 0)
sb.append('.');
else if (map[i][j] == 1)
sb.append('R');
}
sb.append('\n');
}
return sb.toString();
}
private static boolean Process() {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (map[i][j] == 0)
continue;
if (i == Jy && j == Jx)
return false;
if (map[i][j] > 1)
map[i][j] = 0;
}
}
return true;
}
static class YX {
int y;
int x;
public YX(int y, int x) {
super();
this.y = y;
this.x = x;
}
}
private static void Move() {
ArrayList<YX> list = new ArrayList<>();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
list.add(new YX(i, j));
}
}
}
// System.out.println(Jy +" "+Jx);
// System.out.println("----------");
for (YX cur : list) {
YX mv = getShotNextMove(cur.y, cur.x);
// System.out.println(cur.y +" "+cur.x +"- >"+(cur.y+mv.y)+ " "+(cur.x+mv.x));
map[cur.y + mv.y][cur.x + mv.x]++;
}
// System.out.println("-----");
}
private static YX getShotNextMove(int y, int x) {
int ny = y - Jy;
int nx = x - Jx;
ny = ny != 0 ? (ny > 0 ? -1 : 1) : 0;
nx = nx != 0 ? (nx > 0 ? -1 : 1) : 0;
return new YX(ny, nx);
}
}
반응형
'ProgramSoliving' 카테고리의 다른 글
프로그래머스 : N으로 표현 JavaScript (0) | 2021.06.22 |
---|---|
프로그래머스 : 메뉴 리뉴얼 (0) | 2021.06.14 |
백준 : 2632 피자판매 (0) | 2021.04.18 |
백준 : 10653 (0) | 2021.04.17 |
백준 : 18248 감시피하기 (0) | 2021.04.17 |