링크
5650
풀이
코드
package SWEA_AD;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Solution_5650_핀볼게임 {
static int[][] dir = {{-1, 0}, {0, -1}, {0, 1}, {1, 0} };
static int[][] type = {
{},
{2, 1, 0, 3},
{0, 1, 3, 2},
{0, 3, 2, 1},
{1, 0, 2, 3},
{0, 1, 2, 3}
};
private static int[][] map;
private static int ANS;
private static int N;
private static int score;
static class Pinball{
int sr, sc, r, c, dir, score = 0;
public Pinball(int sr, int sc, int r, int c, int dir) {
this.sr = sr;
this.sc = sc;
this.r = r;
this.c = c;
this.dir = dir;
}
}
static List<int[]>[] list;
static int[][][] wormHole;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T =Integer.parseInt(br.readLine().trim());
for(int tc = 1; tc <= T; tc++) {
N =Integer.parseInt(br.readLine().trim());
map = new int[N][N];
list = new ArrayList[11];
for(int i = 6; i <= 10; i++) {
list[i] = new ArrayList<>();
}
wormHole = new int[N][N][2];
for(int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if(map[i][j] >= 6) {
list[map[i][j]].add(new int[] {i,j});
}
}
}
for(int i = 6; i <=10 ; i++) {
if(list[i].size() == 0) continue;
int x1 = list[i].get(0)[0];
int y1 = list[i].get(0)[1];
int x2 = list[i].get(1)[0];
int y2 = list[i].get(1)[1];
wormHole[x1][y1][0] = x2;
wormHole[x1][y1][1] = y2;
wormHole[x2][y2][0] = x1;
wormHole[x2][y2][1] = y1;
}
ANS = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j <N ; j ++) {
if(map[i][j] == 0) {
for(int d = 0 ; d < 4 ; d ++) {
score = 0;
search(new Pinball(i, j, i, j, d));
ANS = Math.max(ANS, score);
}
}
}
}
System.out.println("#" + tc + " " + ANS);
}
}
private static void search(Pinball p) {
while(true) {
int d = p.dir;
int nr = p.r + dir[d][0];
int nc = p.c + dir[d][1];
if(isIn(nr, nc)) {
if(map[nr][nc] == 0) {
if(nr == p.sr && nc == p.sc){
return;
}
p.r = nr;
p.c = nc;
}else if(map[nr][nc] == -1) {
return;
}else if(map[nr][nc] >=1 && map[nr][nc] <= 5) {
if(map[nr][nc] == 5) {
score *= 2;
score++;
return;
}
score++;
p.r = nr;
p.c = nc;
p.dir = type[map[nr][nc]][3-p.dir];
}else {
p.r = wormHole[nr][nc][0];
p.c = wormHole[nr][nc][1];
}
}else {
score *= 2;
score++;
return;
}
}
}
static boolean isIn(int r, int c) {
return r >=0 && r < N && c>= 0 && c < N;
}
}