-
[SWEA] 특이한 자석Algorithm/SWEA 2020. 11. 30. 18:58
+) 이전 풀이
소스코드
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main {static String[] magnets;static boolean[] flag;public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int k;int T = stoi(br.readLine());for (int tc = 1; tc <= T; tc++) {k = stoi(br.readLine());flag = new boolean[5];magnets = new String[5];StringTokenizer st;StringBuilder sb;for (int i = 1; i < magnets.length; i++) {st = new StringTokenizer(br.readLine().trim());sb = new StringBuilder();while (st.hasMoreTokens()) {sb.append(st.nextToken());}magnets[i] = sb.toString();}int n, dir;for (int i = 0; i < k; i++) {st = new StringTokenizer(br.readLine());n = stoi(st.nextToken());dir = stoi(st.nextToken());Arrays.fill(flag, false);simulation(n, dir);}int answer = 0;for (int i = 1; i < magnets.length; i++) {if (magnets[i].charAt(0) == '1') {answer += Math.pow(2, i-1);}}System.out.println("#" + tc + " " + answer);}}private static void simulation(int n, int dir) {if (flag[n]) return;flag[n] = true;char left = magnets[n].charAt(6);char right = magnets[n].charAt(2);rotate(n, dir);dir = (dir == 1) ? -1 : 1;if (n > 1 && magnets[n - 1].charAt(2) != left) {simulation(n - 1, dir);}if (n < 4 && magnets[n + 1].charAt(6) != right) {simulation(n + 1, dir);}}private static void rotate(int n, int dir) {StringBuilder sb;if (dir == 1) {sb = new StringBuilder();sb.append(magnets[n].charAt(7));sb.append(magnets[n].substring(0, 7));} else {sb = new StringBuilder(magnets[n].substring(1));sb.append(magnets[n].charAt(0));}magnets[n] = sb.toString();}private static int stoi(String s) {return Integer.parseInt(s);}}cs 'Algorithm > SWEA' 카테고리의 다른 글
[SWEA] 차량 정비소 (0) 2020.09.01 [SWEA] 입국심사 (0) 2020.04.25 [SWEA] S/W 문제해결 기본(4) - 길찾기 (0) 2020.04.18 [SWEA] S/W 문제해결 기본(4) - 괄호 짝짓기 (0) 2020.04.18 [SWEA] S/W 문제해결 기본(4) - 거듭제곱 (0) 2020.04.18