[λ°±μ€] π₯ 20056 λ§λ²μ¬ μμ΄μ νμ΄μ΄λ³Ό
π λμ΄λ
π₯ Gold 4
π λ¬Έμ
https://www.acmicpc.net/problem/20056
π νμ΄
μβ¦ 2λ²μ§Έλ‘ λμ νλ 골λ4 λ¬Έμ μ λμμ μ²μμΌλ‘ νΌ κ³¨λ 4 λ¬Έμ μ΄λ€.
λ°₯λ¨Ήλ μκ° μ μΈνκ³ , 8μκ°μ κΌ¬λ° μ΄ λ¬Έμ λ§ νμ κ² κ°λ€.
κ·Έλλ§ μ½κ² νλ¦° μ΄μ λ λ©λͺ¨λ¦¬ μ νμ΄ λλνκ³ , μΉΈμ λμ΄κ° 50
λ°μ μλμ 50^4 = 6250000
μ΄λΌλ o(n^4)
κ° λμλ κ±±μ μλ λ¬Έμ λΌμ λ©λͺ¨λ¦¬ κ±±μ μνκ³ νμ΄μ μ½κ² ν΄κ²°λ λ¬Έμ μΈκ² κ°λ€.
μ²μμλ N x N
ν¬κΈ°μ pos
λΌλ ν΄λμ€λ₯Ό μΈμλ‘ κ°μ§λ 2μ°¨μ λ°°μ΄μ μ¬μ©νμ¬ κ΅¬μμ νμλ€.
νμ΄μ΄λ³Όμ΄λΌλ κ°μ²΄λ₯Ό μ¬λ¬κ° ꡬννλ νμμ΄ μλ, κ° μ£Όμκ°μ νμ¬ νμ΄μ΄λ³Όμ μ 보λ₯Ό μ μ₯μν€λ λ°©μμΌλ‘ ꡬμμ νμλ€.
μ΄μ€ for
λ¬Έμ μ¬μ©νμ¬, BFS
μ²λΌ 첫칸λΆν° λ§μ§λ§ μΉΈκΉμ§ μμλλ‘ μ΄λμν€λ μμΌλ‘ ꡬνμ νλ €κ³ νμλ€.
μ΄λ₯Ό μνμ¬ μλ * DR[i] + νμ¬μμΉ
λ₯Ό μ΄μ©νμ¬ λ€μ μΉΈμ μμΉλ₯Ό ꡬνλ €κ³ νλλ°, μ΄ μμΉκ° N
λ³΄λ€ μμμλ, ν΄μλ μλ€λ λ¬Έμ μ μ΄ μμλ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μνμ¬, cur[0] + ((ODR[j] * curs+ N) % N
μ²λΌ μΌλΆλ¬ N
μ λν΄μ μμλ‘ λ§λ€κ³ N
μΌλ‘ λλ μ μΈλ±μ€ λ¬Έμ λ₯Ό ν΄κ²°νλ €κ³ νμλ€.
νμ§λ§ λ§λ€κ³ 보λ μλκ° λ무컀μ Nμ λν΄μ€κ°λ³΄λ€ λ ν΄ μλ μλ€λ μκ°μ΄ λ€μλ€.
κ·Έλμ μλλ₯Ό N
μΌλ‘ λλ λλ¨Έμ§μ N
κ°μ λν΄μ£Όκ³ λ€μ %N
μ°μ°μ ν΄μ£Όλ μ΄μ§μ λ²κ±°λ‘μ΄ λ€μκ³Ό κ°μ μ°μ°μ μ΄μ©νμ¬ κ΅¬ννμλ€.
(cur[0] + ((ODR[j] * curs) % N + N)) % N
μ΄ν μ½λλ₯Ό μμ±νλ€κ° visited
λ°°μ΄μ μ¬μ©νλλΌλ ν΄κ²°μ΄ μλλ λ¬Έμ κ° νλ μμλλ° λ°λ‘, νμ΄μ΄λ³Όμ΄ λ€μκ³Ό κ°μ΄ νμ΄μ΄λ³Ό1
μ΄ [2,2]μ μμΉλ‘ μ΄λνλ €κ³ ν λ νν κ·Έμ리μ λ€λ₯Έ νμ΄μ΄λ³Όμ΄ μμΌλ©΄, νμ΄μ΄λ³Ό1
μ λλ² μ΄λνκ² λκ² λλ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μνμ¬ νμ΄μ΄λ³Ό1
μ΄ μ΄λν ν, [2,2]μ μ리λ₯Ό visited λ°°μ΄μ λ£μ΄μ, κ²μ¬λ₯Ό μνλ©΄ νμ΄μ΄λ³Ό2
λ μμ§μ΄μ§ μκ² λλ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μνμ¬ κ²°κ΅ λ€μμνμ νμ¬μνλ₯Ό ꡬλ³ν΄μΌ λλ€λ κ²μ λλΌκ³ , μ½λλ₯Ό μ²μλΆν° λ€μ μ§κΈ° μμνλ€.
λ©λͺ¨λ¦¬μμ΄ λλνλ€λ κ²μ μ΄μ©ν΄ κ·Έλ₯ currentfield
μ field
λ₯Ό λ§λ€μ΄μ νμ¬ μνμ λ€μ μνλ₯Ό ꡬλ³νμλ€.
κ·Έλ¦¬κ³ , νλ°μ μ΄λ»κ² ꡬνν΄μΌ νλ μμλ₯Ό κ³ λ―Όνλ€κ°, νμ νλ° -> μ΄λ
λ°©μμΌλ‘ isExplode
λΌλ λ³μλ₯Ό μΆκ°νμ¬, μ΄λνκΈ°μ μ isExplode
κ° κ±°μ§μ΄λ©΄ κ·Έλλ‘, μ°Έμ΄λ©΄ κ°κ° EDR
, ECR
, ODR
, OCR
μ΄λΌλ μμ μΈνΈ λ°°μ΄μ λ§λ€μ΄μ dir % 8
κ°μ΄ μ§μλ©΄ μμ λκ°, νμλ©΄ λ€μ λκ°λ₯Ό μ¬μ©νμ¬ νλ°μ ꡬννμλ€.
νμ§λ§ μ¬κΈ°μ ν¬λν° μ€μ°¨κ° νλ μκΈ°λλ° λ€μμ μ€λͺ
νκ² λ€.
μ΄ν, ν©μΉλ κ²μ κ³μ ꡬννλ€κ° μλ λΆλΆμμ λ¬Έμ κ° μκ²Όλ€.
μλ κΈ°μ‘΄ λ€μ΄μ¨ κ°μ μκ°μ λν΄μ 2λ₯Ό λλλ μμΌλ‘ ꡬνμ νλ €λ€κ°, μμΈν κ³μ°μ ν΄λ³΄λ, count
λΌλ λ³μκ°μ΄ 무쑰건 νμνλ€λ κ²μ μκ²λμλ€.
κ·Έλμ λ€λ¦κ² pos
μ λ£μ΄μ£Όμλ€.
νμ§λ§ μ΄ λͺ¨λ μ€λ₯λ₯Ό κ³ μΉκ³ λμλ κ³μ 1%μμ ν리λ μν©μ΄ λ°λ³΅λμλ€.
λ무λλ νκ°λ¬μ§λ§ μ°¨λΆνκ² λλ²κΉ
μ μ°μΌλ©΄μ νκ°μ§ λ¬Έμ μ μ μκ² λμλ€.
λ¬Έμ μμ λͺ¨λ μ§μμ΄κ±°λ, νμμΌλ 쑰건μμ μ§ + μ§ % 2 == ν + ν % 2 == 0
μ κ°λ
μΌλ‘ μ κ·Όμ νλλ°, μ§ μ§ ν ν
κ°μκ²½μ° μ²λ¦¬κ° μ λλ‘ λμ§ μλ κ²μ΄μλ€.
λ€λ¦κ² μ΄ μ¬μ€μ κΉ¨λ«κ³ κ³ μΉκΈ° μν΄μ isSame
μ΄λΌλ λ³μλ₯Ό λ£κ³ final
μμλ₯Ό μ¬μ©νμ¬ μ΄κ²μ class λ΄λΆμμ ꡬλ³μ νκ² κ΅¬ννμλ€.
λ무 μ΄λ €μ κ³ , μκ°μ΄ μ€λκ±Έλ Έμ§λ§, νμ΄λ΄λκΉ ν볡νλ€. π
π Code
package BOJ.Implements;
import java.io.*;
import java.util.*;
public class N20056 {
static class pos {
int m;
int s;
int d;
boolean isExplode;
int count;
int isSame;
public pos(int m, int s, int d, int count, int isSame, boolean isExplode) {
this.m = m;
this.s = s;
this.d = d;
this.count = count;
this.isSame = isSame;
this.isExplode = isExplode;
}
public boolean BigBang(int m, int s, int d) {
if (this.m != 0) {
this.isExplode = true;
this.count += 1;
if (this.isSame == notSame) {
this.isSame = notSame;
} else if ((this.isSame == empty) || (this.isSame == d % 2)) {
this.isSame = d % 2;
} else {
this.isSame = notSame;
}
} else {
this.isSame = d % 2;
this.isExplode = false;
this.count = 1;
}
this.m += m;
this.s += s;
this.d += d;
this.d %= 8;
return isExplode;
}
public void reset() {
this.m = 0;
this.s = 0;
this.d = 0;
this.count = 0;
this.isSame = empty;
this.isExplode = false;
}
}
static pos[][] field;
static pos[][] curfield;
static Queue<Integer[]> fire;
static int N;
static int M;
static int K;
static int empty = -1;
static int notSame = -2;
static final int[] DR = {-1, -1, 0, 1, 1, 1, 0, -1};
static final int[] DC = {0, 1, 1, 1, 0, -1, -1, -1};
static final int[] EDR = {-1, 0, 1, 0};
static final int[] EDC = {0, 1, 0, -1};
static final int[] ODR = {-1, 1, 1, -1};
static final int[] ODC = {1, 1, -1, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
curfield = new pos[N][N];
field = new pos[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
field[i][j] = new pos(0, 0, 0, 1, empty, false);
curfield[i][j] = new pos(0, 0, 0, 1, empty, false);
}
}
fire = new LinkedList<>();
int r;
int c;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken()) - 1;
c = Integer.parseInt(st.nextToken()) - 1;
field[r][c] = new pos(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), 1, empty, false);
fire.add(new Integer[]{r, c});
}
TotalTime(K);
}
static void TotalTime(int time) {
//ShowAll();
for (int i = 0; i < time; i++) {
MoveFire();
//ShowAll();
}
Count();
}
static void MoveFire() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
curfield[i][j] = new pos(field[i][j].m, field[i][j].s, field[i][j].d, field[i][j].count, field[i][j].isSame, field[i][j].isExplode);
field[i][j].reset();
}
}
int count = fire.size();
int nextR;
int nextC;
int curm;
int curs;
int curd;
Integer[] cur;
for (int i = 0; i < count; i++) {
cur = fire.poll();
curm = curfield[cur[0]][cur[1]].m;
curs = curfield[cur[0]][cur[1]].s;
curd = curfield[cur[0]][cur[1]].d;
if (curm <= 0) {
continue;
}
if (!curfield[cur[0]][cur[1]].isExplode) {
nextR = (cur[0] + ((DR[curd] * curs) % N + N)) % N;
nextC = (cur[1] + ((DC[curd] * curs) % N + N)) % N;
if (!field[nextR][nextC].BigBang(curm, curs, curd)) {
fire.add(new Integer[]{nextR, nextC});
}
} else {
curm /= 5;
curs /= curfield[cur[0]][cur[1]].count;
if (curm <= 0) {
continue;
}
if (curfield[cur[0]][cur[1]].isSame == notSame) {
for (int j = 0; j < ODC.length; j++) {
nextR = (cur[0] + ((ODR[j] * curs) % N + N)) % N;
nextC = (cur[1] + ((ODC[j] * curs) % N + N)) % N;
if (!field[nextR][nextC].BigBang(curm, curs, 2 * j + 1)) {
fire.add(new Integer[]{nextR, nextC});
}
}
} else {
for (int j = 0; j < EDC.length; j++) {
nextR = (cur[0] + ((EDR[j] * curs) % N + N)) % N;
nextC = (cur[1] + ((EDC[j] * curs) % N + N)) % N;
if (!field[nextR][nextC].BigBang(curm, curs, 2 * j)) {
fire.add(new Integer[]{nextR, nextC});
}
}
}
}
}
}
static void Count() {
int result = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (field[i][j].isExplode) {
result += (field[i][j].m / 5) * 4;
} else {
result += field[i][j].m;
}
}
}
System.out.println(result);
}
static void ShowAll() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (field[i][j].m == 0) {
System.out.print(". ");
} else if (field[i][j].isExplode) {
System.out.print(field[i][j].m + ")");
} else {
System.out.print(field[i][j].m + " ");
}
}
System.out.println();
}
System.out.println("==========================");
}
}
package (μ΄λ¦); λ₯Ό λκ³ , class μ΄λ¦μ Main
μΌλ‘ λ³κ²½νλ©΄ λλ€.
π μΆκ° μμ
[Input]
7 4 5
1 1 1 1 3
1 3 2 1 5
3 1 3 1 1
3 3 4 1 7
[Output]
8
[Input]
7 4 5
1 2 1 1 4
2 3 2 1 6
3 1 3 1 1
3 3 4 1 7
[Output]
8
[Input]
7 1 10
4 4 7 1 7
[Output]
7
[Input]
7 0 10
[Output]
0
κ°μΈ κ³΅λΆ κΈ°λ‘μ© λΈλ‘κ·Έμ
λλ€.
ν리거λ μ€λ₯κ° μμ κ²½μ° μ 보ν΄μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.π