Created
April 14, 2018 08:00
-
-
Save NaniteFactory/14f973aff863b3fb41509685e2c37986 to your computer and use it in GitHub Desktop.
4013. [모의 SW 역량테스트] 특이한 자석
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class Solution { | |
public static int S = 1, N = 0, RIGHT = 1, LEFT = -1; | |
public static void rotateLeft(int[] gear) { | |
int tmp = gear[0]; | |
for (int i = 0; i < gear.length - 1; ++i) { | |
gear[i] = gear[i+1]; | |
} | |
gear[gear.length - 1] = tmp; | |
} | |
public static void rotateRight(int[] gear) { | |
int tmp = gear[gear.length - 1]; | |
for (int i = gear.length - 1; i > 0; --i) { | |
gear[i] = gear[i-1]; | |
} | |
gear[0] = tmp; | |
} | |
public static int getPts(int[][] gears) { | |
return ((gears[0][0]==S)?1:0) | |
+ ((gears[1][0]==S)?2:0) | |
+ ((gears[2][0]==S)?4:0) | |
+ ((gears[3][0]==S)?8:0); | |
} | |
public static void check(int[][] gears, int index) { | |
checked[index] = true; | |
if (index < 3 && !checked[index+1] && | |
gears[index][2] != gears[index+1][6]) { | |
toRotate[index + 1] = true; | |
check(gears, index + 1); | |
} | |
if (index > 0 && !checked[index-1] && | |
gears[index][6] != gears[index-1][2]) { | |
toRotate[index - 1] = true; | |
check(gears, index - 1); | |
} | |
} | |
public static void rotate(int[][] gears, int index, int dir) { | |
//printGears(gears); // debug | |
checked[index] = true; | |
if (dir == LEFT) { | |
//System.out.println("rotating " + index + " left"); // debug | |
rotateLeft(gears[index]); | |
//printGears(gears); // debug | |
} else if (dir == RIGHT) { | |
//System.out.println("rotating " + index + " right"); // debug | |
rotateRight(gears[index]); | |
//printGears(gears); // debug | |
} | |
if (index < 3 && !checked[index+1] && toRotate[index + 1]) { | |
rotate(gears, index + 1, -1 * dir); | |
} | |
if (index > 0 && !checked[index-1] && toRotate[index - 1]) { | |
rotate(gears, index - 1, -1 * dir); | |
} | |
} | |
@Deprecated | |
public static void printArr(boolean[] arr) { | |
for (int i = 0; i < arr.length; ++i) { | |
System.out.print(arr[i]?i:"_"); | |
} | |
System.out.println(); | |
} | |
@Deprecated | |
public static void printGears(int[][] gears) { | |
for (int i = 0; i < 4; ++i) { | |
for (int j = 0; j < 8; ++j) { | |
System.out.print(gears[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
} | |
public static boolean[] checked = new boolean[4]; | |
public static boolean[] toRotate = new boolean[4]; | |
// left: 6, right: 2 | |
public static int go(int[][] gears, int index, int dir) { | |
//1 check if it effects others | |
Arrays.fill(checked, false); | |
Arrays.fill(toRotate, false); | |
check(gears, index); | |
//printArr(checked); | |
//printArr(toRotate); // debug | |
//2 rotate | |
Arrays.fill(checked, false); | |
rotate(gears, index, dir); | |
//printArr(checked); // debug | |
//3 calc pts, return | |
//System.out.println("pts: " + getPts(gears)); // debug | |
return getPts(gears); | |
} | |
public static void main(String args[]) throws NumberFormatException, IOException { | |
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
final int nTestcases = Integer.parseInt(br.readLine().trim()); | |
final StringBuilder ret = new StringBuilder(); | |
for (int testcase = 1; testcase <= nTestcases; ++testcase) { | |
// input | |
int nMoves = Integer.parseInt(br.readLine().trim()); | |
int[][] gears = new int[4][8]; // ! | |
for (int gear = 0; gear < 4; ++gear) { | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
for (int tooth = 0; tooth < 8; ++tooth) { | |
gears[gear][tooth] = Integer.parseInt(st.nextToken()); | |
} | |
} | |
// solve | |
int answer = 0; | |
for (int move = 0; move < nMoves; ++move) { | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
int indexGearToRotate = Integer.parseInt(st.nextToken()) - 1; | |
int dir = Integer.parseInt(st.nextToken()); // RIGHT or LEFT | |
answer = go(gears, indexGearToRotate, dir); | |
} | |
// ret | |
ret.append('#'); | |
ret.append(testcase); | |
ret.append(' '); | |
ret.append(answer); | |
ret.append('\n'); | |
} // for testcase | |
System.out.println(ret); | |
} // main() | |
} // public class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment