[SWEA] 1240. 단순 2진 암호코드 D3 - Simulation

제출일 : 2019-11-16

문제 풀이 시간 : 1H

난이도 : ★★★

Problem

link : https://swexpertacademy.com/main/code/problem/problemSolver.do?contestProbId=AV15FZuqAL4CFAYD

Input

가장 첫줄은 전체 테스트 케이스의 수이다.

각 테스트 케이스의 첫 줄에 두 자연수가 주어지는데 각각 배열의 세로 크기 N, 배열의 가로크기 M이다 (1≤N<50, 1≤M<100).

그 다음 N개의 줄에는 M개의 배열의 값이 주어진다.

Output

각 테스트 케이스의 답을 순서대로 표준출력으로 출력하며, 각 케이스마다 줄의 시작에 “#C”를 출력하여야 한다.

이때 C는 케이스의 번호이다. 같은 줄에 빈칸을 하나 두고, 입력에 주어진 배열에서 정상적인 암호코드들에 포함된 숫자들의 합을 출력한다.

Example

input

2
16 80
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000011101101100010111011011000101100010001101001001101110110000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
11 70
00000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000001100101000110100011010111101101110010011001001101110110000000000
00000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000

output

#1 38 
#2 0 

Solution & Inpression

인덱스 조작문제

입력에서 코드 값을 찾아 8개의 값을 구하기가 까다로운 문제였다.

전부 배열로 입력을 받았지만 문자열로 입력을 받아 비교하면 더 시간이 빠르지 않았을까 하는 생각도 든다.

Code

언어 : JAVA

메모리 : 28,036 kb

실행시간 : 156 ms

import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Solution {
    static int N, M;
    static int[][] input;
    static int ans;
    static final int[][] code = { { 0, 0, 0, 1, 1, 0, 1 }// 0
            , { 0, 0, 1, 1, 0, 0, 1 }// 1
            , { 0, 0, 1, 0, 0, 1, 1 }// 2
            , { 0, 1, 1, 1, 1, 0, 1 }// 3
            , { 0, 1, 0, 0, 0, 1, 1 }// 4
            , { 0, 1, 1, 0, 0, 0, 1 }// 5
            , { 0, 1, 0, 1, 1, 1, 1 }// 6
            , { 0, 1, 1, 1, 0, 1, 1 }// 7
            , { 0, 1, 1, 0, 1, 1, 1 }// 8
            , { 0, 0, 0, 1, 0, 1, 1 }// 9
    };
    static int[] res;

    public static void main(String[] args) throws Exception {
        System.setIn(new FileInputStream("res/input.txt"));
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int tc = 1; tc <= T; tc++) {
            N = sc.nextInt();
            M = sc.nextInt();

            input = new int[8][7];
            boolean flag = false;
            for (int i = 0; i < N; i++) {
                String str = sc.next();
                for (int j = 0; j < M; j++) {
                    if (flag) // 이미 코드를 구한경우
                        break;

                    int tmp = str.charAt(j) - '0';
                    if (tmp == 1) {
                        flag = true;
                        j--;
                        for (int r = 0; r < 8; r++) {
                            for (int c = 0; c < 7; c++) {
                                input[r][c] = str.charAt(j++) - '0';
                            }
                        }
                    }
                }
            }

            getCode();
            System.out.println("#" + tc + " " + calc());

        } // end of TC
        sc.close();
    }// end of main

    private static int calc() {
        int result = (res[0] + res[2] + res[4] + res[6]) * 3 + (res[1] + res[3] + res[5]);
        if ((result + res[7]) % 10 == 0) {
            int sum = 0;
            for (int is : res) {
                sum += is;
            }
            return sum;
        } else
            return 0;
    }

    private static void getCode() {
        res = new int[8];
        Arrays.fill(res, -1);
        for (int k = 0; k < 10; k++) {
            for (int i = 0; i < 8; i++) {
                int flag = 0;
                for (int j = 0; j < 7; j++) {
                    if (input[i][j] == code[k][j])
                        flag++;
                }
                if (flag == 7)
                    res[i] = k;
            }
        }
        for (int i = 0; i < 8; i++) {
            if (res[i] == -1) {
                rotate();
                getCode();
                return;
            }
        }
    }

    private static void rotate() {
        int first = 0;
        for (int i = 0; i < 8; i++) {
            int[] tmp = input[i].clone();
            input[i][0] = first;
            for (int j = 1; j <= 6; j++) {
                input[i][j] = tmp[j - 1];
            }
            first = tmp[6];
        }
    }
}