링크
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl
활용가능 장치
구조체, 새로운 맵(2차원배열)
import java.util.Scanner;
public class Solution {
public static int T, N, M, K;// N 크기 M 시간, K 개수
public static int[] dx = new int[] { 0, -1, 1, 0, 0 };
public static int[] dy = new int[] { 0, 0, 0, -1, 1 };
public static Node[][] map = new Node[1020][1020];
public static Node[][] newMap = new Node[1020][1020];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
T = scan.nextInt();
for (int i = 1; i <= T; i++) {
input(scan);
for (int j = 0; j < M; j++) {
move();
}
int x = getAnswer();
System.out.println("#" + i + " " + x);
}
}
private static int getAnswer() {
int sum = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += map[i][j].cnt;
}
}
return sum;
}
private static void move() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// 개수 1개 이상이면 이동(nextmap에 기입)
if (map[i][j].cnt > 0) {
int dir = map[i][j].dir;
int nextx = i + dx[dir];
int nexty = j + dy[dir];
if (nextx == 0 || nextx == N - 1 || nexty == 0 || nexty == N - 1) {
newMap[nextx][nexty].cnt = map[i][j].cnt / 2;
if (dir == 1) {
newMap[nextx][nexty].dir = 2;
} else if (dir == 2) {
newMap[nextx][nexty].dir = 1;
} else if (dir == 3) {
newMap[nextx][nexty].dir = 4;
} else if (dir == 4) {
newMap[nextx][nexty].dir = 3;
}
} else {
if (newMap[nextx][nexty].cnt >0) {
newMap[nextx][nexty].cnt += map[i][j].cnt;
if (newMap[nextx][nexty].maxCnt < map[i][j].cnt) {
newMap[nextx][nexty].maxCnt = map[i][j].cnt;
newMap[nextx][nexty].dir = map[i][j].dir;
}
} else {
newMap[nextx][nexty].cnt = map[i][j].cnt;
newMap[nextx][nexty].maxCnt = map[i][j].cnt;
newMap[nextx][nexty].dir = map[i][j].dir;
}
}
}
}
}
// map초기화시켜
// nextmap의 정보를 다시 map으로 옮겨
// nextmap초기화시켜
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// map[i][j].reset();
map[i][j].set(newMap[i][j]);
newMap[i][j].reset();
}
}
}
private static void input(Scanner scan) {
N = scan.nextInt();
M = scan.nextInt();
K = scan.nextInt();
// 초기화
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
map[i][j] = new Node();
newMap[i][j] = new Node();
map[i][j].reset();
newMap[i][j].reset();
}
}
for (int i = 0; i < K; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
int val = scan.nextInt();
int dir = scan.nextInt();
map[x][y].set(val, val, dir);
}
}
private static void printArr() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j].cnt != 0) {
System.out.println(i+" "+j+" "+map[i][j].toString());
}
}
}
}
private static void printNewArr() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (newMap[i][j].cnt != 0) {
System.out.println(i+" "+j+" "+newMap[i][j].toString());
}
}
}
}
}
class Node {
@Override
public String toString() {
return "Node [cnt=" + cnt + ", maxCnt=" + maxCnt + ", dir=" + dir + "]";
}
int cnt;
int maxCnt;
int dir;
public void set(int _cnt, int _maxCnt, int _dir) {
cnt = _cnt;
maxCnt = _maxCnt;
dir = _dir;
}
public void set(Node t) {
cnt = t.cnt;
maxCnt = t.maxCnt;
dir = t.dir;
}
public void reset() {
cnt = 0;
maxCnt = 0;
dir = 0;
}
}
'알고리즘' 카테고리의 다른 글
디저트카페[swexpert academy] (0) | 2019.10.01 |
---|---|
탈주범 검거[swexpertacademy] (0) | 2019.09.30 |
무선 충전[swexpert academy] (0) | 2019.09.29 |
DAM (0) | 2019.09.26 |
SAFETYZONE (0) | 2019.09.26 |