뭔가 문제가 삼성느낌이 난다...
문제 풀이 코드는 아래와 같다. 코드가 넘 긴 느낌이 있긴 하지만... 시키는 대로만 하면 풀 수 있다
import java.util.*;
public class Main {
private static int STAND = 0;
private static int FALL = 1;
private static int[][] before;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int R = in.nextInt();
int C = in.nextInt();
int round = in.nextInt();
int mat[][] = new int[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
mat[i][j] = in.nextInt();
}
}
before = new int[R][C]; // 0: 서 있음, 1: 넘어짐
int score = 0;
for (int r = 0; r < round; r++) {
int attackR = in.nextInt() - 1;
int attackC = in.nextInt() - 1;
Direction direction = Direction.valueOf(in.next());
int defendR = in.nextInt() - 1;
int defendC = in.nextInt() - 1;
score += turn(mat, attackR, attackC, direction, defendR, defendC, R, C);
}
System.out.println(score);
print(before);
}
private static int turn(int mat[][], int attackR, int attackC, Direction direction, int defendR, int defendC,
int maxRow, int maxCol) {
int[][] after = clone(before, maxRow, maxCol);
attack(mat, after, attackR, attackC, direction, maxRow, maxCol);
int score = calculateScore(after, maxRow, maxCol);
defend(after, defendR, defendC);
before = after;
return score;
}
private static void attack(int mat[][], int[][] after, int attackR, int attackC, Direction direction, int maxRow,
int maxCol) {
if (after[attackR][attackC] == FALL) {
return;
}
after[attackR][attackC] = FALL;
for (int i = 1; i < mat[attackR][attackC]; i++) {
int newRow = attackR + direction.row * i;
int newCol = attackC + direction.col * i;
// System.out.println(newRow + " " + newCol + " " + direction.toString());
if (isBoundary(newRow, newCol, maxRow, maxCol) && after[newRow][newCol] == STAND) {
attack(mat, after, newRow, newCol, direction, maxRow, maxCol);
}
}
}
private static boolean isBoundary(int row, int col, int maxRow, int maxCol) {
if (row < 0) {
return false;
}
if (col < 0) {
return false;
}
if (row >= maxRow) {
return false;
}
if (col >= maxCol) {
return false;
}
return true;
}
private static void defend(int[][] after, int defendR, int defendC) {
after[defendR][defendC] = STAND;
}
private static int calculateScore(int after[][], int row, int col) {
int sum = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (before[i][j] == STAND && after[i][j] == FALL) {
sum++;
}
}
}
return sum;
}
private static int[][] clone(int mat[][], int row, int col) {
int newMat[][] = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
newMat[i][j] = mat[i][j];
}
}
return newMat;
}
private static void print(int mat[][]) {
int row = mat.length;
int col = mat[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (mat[i][j] == STAND) {
System.out.print("S ");
} else {
System.out.print("F ");
}
}
System.out.println();
}
}
}
enum Direction {
N(-1, 0), W(0, -1), E(0, 1), S(1, 0);
int row;
int col;
Direction(int row, int col) {
this.row = row;
this.col = col;
}
public String toString() {
return this.name() + "(" + this.row + ", " + this.col + ")";
}
}
'알고리즘 공부 > boj' 카테고리의 다른 글
boj 2631: 줄세우기 (0) | 2021.04.01 |
---|---|
boj 20166: 문자열 지옥에 빠진 호석 (1) | 2021.03.26 |
boj 20164: 홀수 홀릭 호석 (0) | 2021.03.24 |
boj 11663: 선분 위의 점 (0) | 2021.03.17 |
boj 19637: IF문 좀 대신 써줘 (0) | 2021.03.17 |