https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeV9sKkcoDFAVH
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String args[]) throws Exception
{
// System.setIn(new FileInputStream("res/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T;
T=Integer.parseInt(br.readLine().trim());
int[][] magnets = new int[5][8];
for(int test_case = 1; test_case <= T; test_case++)
{
int K = Integer.parseInt(br.readLine().trim());
for (int magnet = 1; magnet <= 4; ++magnet) {
StringTokenizer st = new StringTokenizer(br.readLine().trim());
for (int blade = 0; blade < 8; ++blade) {
magnets[magnet][blade] = Integer.parseInt(st.nextToken());
}
}
int[] magnetTop = new int[5];
int[] magnetR = new int[5];
for (int i = 1; i < 4; ++i) {
magnetR[i] = 2;
}
int[] magnetL = new int[5];
for (int i = 2; i <= 4; ++i) {
magnetL[i] = 6;
}
// for (int i = 1; i <= 4; ++i) {
// for (int j = 0; j < 8; ++j) {
// System.out.printf("%d ", magnets[i][j]);
// }
// System.out.printf("\n");
// }
//
//simulation
for (int move = 0; move < K; ++move) {
StringTokenizer st = new StringTokenizer(br.readLine().trim());
int num = Integer.parseInt(st.nextToken());
int dir = Integer.parseInt(st.nextToken());
//we change the top;
magnetTop[num] = calcPos(magnetTop[num], dir);
//we first check the right side.
int tmpDir = dir;
int tmpNum = num;
while (tmpNum < 4) {
int L = magnets[tmpNum+1][magnetL[tmpNum+1]];
int R = magnets[tmpNum][magnetR[tmpNum]];
magnetR[tmpNum] = calcPos(magnetR[tmpNum], tmpDir);
if (L+R%2 == 1) { //different.
magnetL[tmpNum+1] = calcPos(magnetL[tmpNum+1], -tmpDir);
magnetTop[tmpNum+1] = calcPos(magnetTop[tmpNum+1], -tmpDir);
tmpDir = -tmpDir;
tmpNum++;
} else {
break;
}
}
//we then check the left side.
tmpDir = dir;
tmpNum = num;
while (tmpNum > 1) {
int L = magnets[tmpNum][magnetL[tmpNum]];
int R = magnets[tmpNum-1][magnetR[tmpNum-1]];
magnetL[tmpNum] = calcPos(magnetL[tmpNum], tmpDir);
if (L+R%2 == 1) { //different.
magnetR[tmpNum-1] = calcPos(magnetR[tmpNum-1], -tmpDir);
magnetTop[tmpNum-1] = calcPos(magnetTop[tmpNum-1], -tmpDir);
tmpDir = -tmpDir;
tmpNum--;
} else {
break;
}
}
//
// for (int i = 1; i <= 4; ++i) {
// System.out.printf("magnet %d with top %d, left %d and right %d\n", i, magnetTop[i], magnetL[i], magnetR[i]);
// }
}
//we check the top and calculate the score.
int score = 0;
for (int magnet = 1; magnet <= 4; ++magnet) {
score += magnets[magnet][magnetTop[magnet]] << (magnet - 1);
}
bw.write("#" + test_case + " " + score + "\n");
}
bw.flush();
bw.close();
}
public static int calcPos(int pos, int dir) {
pos -= dir;
if (pos >= 8) pos = 0;
else if (pos < 0) pos = 7;
return pos;
}
}
좀 요상한 시뮬레이션 문제.
구현은 다음과 같이 했다.