λ°μν
https://www.acmicpc.net/problem/17143
λ¬Έμ μ€λͺ
λμμ (-1,-1)μμ μμ
(0,0) ~ (R-1,C-1) μ μμ΄ Mλ§λ¦¬ μ‘΄μ¬
κ° μμ΄λ ν¬κΈ°, μλ κ·Έλ¦¬κ³ λ°©ν₯μ κ°μ§κ³ μμ
1μ΄ λμ
- λμμμ΄ μ€λ₯Έμͺ½μΌλ‘ ν μΉΈ μ΄λ
- κ°μ μ΄μ μμ΄ μ€ λ κ³Ό κ°μ₯ κ°κΉμ΄ μμ΄ μ‘μ
- μμ΄ μ΄λ
1) μ£Όμ΄μ§ λ°©ν₯μΌλ‘ μλλ§νΌ μ΄λ
2) λ²μκ° λμ΄κ°λ©΄, λ°©ν₯μ λ°λλ‘ ν΄μ μ΄λ
3) ν μΉΈμ μ¬λ¬λ§λ¦¬κ° μλ€λ©΄, ν¬κΈ°κ° κ°μ₯ ν° μμ΄κ° λλ¨Έμ§ μμ΄λ€μ μ‘μλ¨Ήμ - λμμμ΄ μ‘μ μμ΄μ ν¬κΈ° ν© μΆλ ₯
λ¬Έμ νμ΄
Shark Class
μμΉ μ’ν, μλ ₯, λ°©ν₯, ν¬κΈ°
dir (μ, μλ, μ€λ₯Έμͺ½, μΌμͺ½) π (0,1,2,3)
1) solution
λμκΎΌμ μ΄μ μμΉ 0 ~ C-1 λ₯Ό forλ¬ΈμΌλ‘ μ΄λ μν€λ©΄μ λμμ μμ΄ μ΄λ μ§ν
2) λμ
fishing(int col)
col
: λμκΎΌμ μ΄μ μμΉ
0 ~ R-1μ λλ©΄μ μμ΄ μ‘κΈ°
3) μμ΄ μ΄λ
Shark[][] moveSharks()
- μμμ Shark[][] tmp μμ±
- κΈ°μ‘΄μ mapμ λλ©΄μ, μμ΄κ° μλ€λ©΄ μ΄λ μμ
- β μ¬κΈ°μ κ·μΉ βΌοΈ
- μμ΄λ μ μμ΄λΌλ©΄ (C-1)2, μ μλλΌλ©΄ (R-1)2 λ§νΌ μμ§μ΄λ©΄ κ·Έ μ리 κ·Έ λ°©ν₯μ λλ¬ (-1 μ΄μ : 0 λΆν° μμμ΄λΌμ)
- π speed %= (R or C - 1) * 2 ν΄μ€λ€!!
- λ²μλ₯Ό λ²μ΄λλ€λ©΄, λ°©ν₯ λ³κ²½ν΄μ μ΄λ
- κ·Έ μ리μ ν¬κΈ°κ° λ ν° μμ΄κ° μλ€λ©΄, μ£½μ ~~!!!
μ½λ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_BOJ_17145_λμμ {
static class Shark {
int r, c, speed, dir, size;
public Shark(int r, int c, int speed, int dir, int size) {
this.r = r;
this.c = c;
this.speed = speed;
this.dir = dir;
this.size = size;
}
@Override
public String toString() {
return "Shark{" +
"r=" + r +
", c=" + c +
", speed=" + speed +
", dir=" + dir +
", size=" + size +
'}';
}
}
static int R, C, M, answer = 0;
static Shark[][] map;
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, 1, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
R = Integer.parseInt(stringTokenizer.nextToken());
C = Integer.parseInt(stringTokenizer.nextToken());
M = Integer.parseInt(stringTokenizer.nextToken());
map = new Shark[R][C];
for (int i = 0; i < M; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
int r = Integer.parseInt(stringTokenizer.nextToken());
int c = Integer.parseInt(stringTokenizer.nextToken());
int s = Integer.parseInt(stringTokenizer.nextToken());
int d = Integer.parseInt(stringTokenizer.nextToken()) - 1;
int z = Integer.parseInt(stringTokenizer.nextToken());
map[r - 1][c - 1] = new Shark(r - 1, c - 1, s, d, z);
}
solution();
System.out.println(answer);
}
private static void solution() {
for (int i = 0; i < C; i++) {
// System.out.println(i);
fishing(i);
map = moveSharks();
/* for (int k = 0; k < R; k++) {
for (int j = 0; j < C; j++) {
System.out.print(map[k][j] == null ? -1+" " : map[k][j].size+" ");
}
System.out.println();
}*/
}
}
private static Shark[][] moveSharks() {
Shark[][] tmp = new Shark[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if(map[i][j] == null) continue;
Shark now = map[i][j];
int speed = now.speed;
if(now.dir == 0 || now.dir == 1) speed %= (R - 1) * 2;
else speed %= (C - 1) * 2;
for (int s = 0; s < speed; s++) {
int nr = now.r + dr[now.dir];
int nc = now.c + dc[now.dir];
if (nr < 0 || nc < 0 || R <= nr || C <= nc) {
now.r -= dr[now.dir];
now.c -= dc[now.dir];
if(now.dir == 0) now.dir = 1;
else if(now.dir == 1) now.dir = 0;
else if(now.dir == 2) now.dir = 3;
else now.dir = 2;
continue;
}
now.r = nr;
now.c = nc;
}
if(tmp[now.r][now.c] != null && tmp[now.r][now.c].size > now.size) continue;
tmp[now.r][now.c] = now;
// System.out.println(now.toString());
}
}
/*for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (tmp[i][j] != null) {
System.out.println("i: " + i + " j: " + j);
System.out.println(tmp[i][j].toString());
}
}
}*/
return tmp;
}
private static void fishing(int col) {
for (int i = 0; i < R; i++) {
if(map[i][col] == null) continue;
answer += map[i][col].size;
// System.out.println(map[i][col].size);
map[i][col] = null;
return;
}
}
}
728x90
λ°μν
'μκ³ λ¦¬μ¦ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ λ°±μ€ / BOJ 21609 ] μμ΄ μ€νκ΅ ( μλ° / JAVA ) (0) | 2022.02.14 |
---|---|
[ BOJ / λ°±μ€ 20061 ] λͺ¨λ Έλ―Έλ Έλλ―Έλ Έ 2 ( μλ° / JAVA ) (0) | 2022.02.12 |
[ λ°±μ€ / BOJ 19236 ] μ²μλ μμ΄ ( μλ° / JAVA ) (0) | 2022.02.08 |
[ λ°±μ€ / BOJ 19237 ] μ΄λ₯Έ μμ΄ ( μλ° / JAVA ) (0) | 2022.02.08 |
[ λ°±μ€ / BOJ 17822 ] μν λ리기 ( μλ° / JAVA ) (0) | 2022.02.07 |