알고리즘/DFS, BFS, 시뮬, 백트래킹

[백준] 2578번 빙고 #Java

VIPeveloper 2022. 5. 25. 10:06
728x90
반응형

시뮬레이션.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int [][] arr;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        arr = new int[5][5];
        int [] call = new int [25];

        for (int i = 0; i < 5; i++) {
            st = new StringTokenizer(br.readLine()," ");
            for (int j = 0; j < 5; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        for (int i = 0; i < 5; i++) {
            st = new StringTokenizer(br.readLine()," ");
            for (int j = 0; j < 5; j++) {
                call[i*5+j] = Integer.parseInt(st.nextToken());
            }
        }
        int n = 25;
        int callNum = 0;
        int check;
        while (n-- > 0){
            check = 0;
            int a = call[callNum++];
            findNum(a);
            check += pandanRow();
            check += pandanCol();
            check += pandanA();
            check += pandanB();
            if(check>=3){
                break;
            }
            //System.out.println(check);
            //printaa();
        }
        System.out.println(callNum);
    }

    private static void printaa() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.printf("%d\t", arr[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    private static void findNum(int a) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if(arr[i][j]== a){
                    arr[i][j] = 0;
                    return;
                }
            }
        }
    }
    private static int pandanRow() {
        int cnt=0;
        for (int i = 0; i < 5; i++) {
            boolean check = true;
            for (int j = 0; j < 5; j++) {
                if(arr[i][j]!=0) {
                    check = false;
                    break;
                }
            }
            if(check){
                cnt++;
            }
        }
        return cnt;
    }
    private static int pandanCol() {
        int cnt=0;
        for (int i = 0; i < 5; i++) {
            boolean check = true;
            for (int j = 0; j < 5; j++) {
                if(arr[j][i]!=0) {
                    check = false;
                    break;
                }
            }
            if(check){
                cnt++;
            }
        }
        return cnt;
    }
    private static int pandanA() {
        for (int i = 0; i < 5; i++) {
            if(arr[i][i]!=0) return 0;
        }
        return 1;
    }
    private static int pandanB() {
        for (int i = 0; i < 5; i++) {
            if(arr[i][4-i]!=0) return 0;
        }
        return 1;
    }
}
728x90
반응형