시뮬레이션 문제이다. 시키는대로만 구현하면 된다.
행렬이 범위만 주의하도록 하자 1 ≤ r ≤ R, 1 ≤ c ≤ C
import java.util.*;
public class Main {
public static int dr[] = { -1, 1, 0, 0 };
public static int dc[] = { 0, 0, 1, -1 };
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row = in.nextInt();
int col = in.nextInt();
int n = in.nextInt();
int mat[][] = new int[row + 1][col + 1];
Shark sharks[] = new Shark[n + 1];
for (int i = 1; i <= n; i++) {
int r = in.nextInt();
int c = in.nextInt();
int s = in.nextInt();
int d = in.nextInt();
int z = in.nextInt();
sharks[i] = new Shark(i, r, c, s, dr[d - 1], dc[d - 1], z);
mat[r][c] = i;
}
int sum = 0;
for (int i = 1; i <= col; i++) {
sum += getShark(i, mat, sharks);
moveShark(mat, sharks, row, col);
}
System.out.println(sum);
}
public static int getShark(int col, int[][] mat, Shark[] sharks) {
for (int i = 0; i < mat.length; i++) {
if (mat[i][col] != 0) {
int size = sharks[mat[i][col]].size;
sharks[mat[i][col]] = null;
mat[i][col] = 0;
return size;
}
}
return 0;
}
public static void moveShark(int[][] mat, Shark[] sharks, int maxRow, int maxCol) {
for (int i = 1; i < sharks.length; i++) {
Shark shark = sharks[i];
if (shark != null) {
mat[shark.row][shark.col] = 0;
shark.move(maxRow, maxCol, shark.speed);
}
}
for (int i = 1; i < sharks.length; i++) {
Shark shark = sharks[i];
if (shark != null) {
if (mat[shark.row][shark.col] == 0) {
mat[shark.row][shark.col] = i;
} else {
Shark beforeShark = sharks[mat[shark.row][shark.col]];
if (beforeShark.size > shark.size) {
sharks[i] = null;
} else {
sharks[mat[shark.row][shark.col]] = null;
mat[shark.row][shark.col] = i;
}
}
}
}
}
}
class Shark {
int name;
int row;
int col;
int speed;
int dr;
int dc;
int size;
public Shark(int name, int row, int col, int speed, int dr, int dc, int size) {
this.name = name;
this.row = row;
this.col = col;
this.speed = speed;
this.dr = dr;
this.dc = dc;
this.size = size;
}
public void changeDirection() {
this.dr = -dr;
this.dc = -dc;
}
public void move(int maxRow, int maxCol, int move) {
// 상어가 이동하려고 하는 칸이 격자판의 경계를 넘는 경우에는 방향을 반대로 바꿔서 속력을 유지한채로 이동한다.
if (move == 0) {
return;
}
int newRow = row + dr * move;
int newCol = col + dc * move;
if (newRow > 0 && newCol > 0 && newRow <= maxRow && newCol <= maxCol) {
row = newRow;
col = newCol;
return;
}
if (newRow <= 0) {
row = 1;
changeDirection();
move(maxRow, maxCol, 1 - newRow);
return;
}
if (newCol <= 0) {
col = 1;
changeDirection();
move(maxRow, maxCol, 1 - newCol);
return;
}
if (newRow > maxRow) {
row = maxRow;
changeDirection();
move(maxRow, maxCol, newRow - maxRow);
return;
}
if (newCol > maxCol) {
col = maxCol;
changeDirection();
move(maxRow, maxCol, newCol - maxCol);
return;
}
}
}
'알고리즘 공부 > boj' 카테고리의 다른 글
boj 1167: 트리의 지름 (0) | 2021.01.25 |
---|---|
boj 1939: 중량제한 (0) | 2021.01.20 |
boj 1005: ACM Craft (0) | 2021.01.20 |
boj 1949: 우수 마을 (0) | 2021.01.19 |
boj 1981: 배열에서 이동 (0) | 2021.01.17 |