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
반응형
'알고리즘 > DFS, BFS, 시뮬, 백트래킹' 카테고리의 다른 글
[백준] 24479번 알고리즘 수업 - 깊이 우선 탐색 1 #Java (0) | 2022.06.17 |
---|---|
[백준] 7562번 나이트의 이동 #Java (0) | 2022.05.27 |
[백준] 2630번 색종이 만들기 (0) | 2022.05.17 |
[알고리즘] 미로탐색(DFS) with Java, 초기화 전략 (0) | 2022.04.22 |
[프로그래머스] N-Queen 문제 (0) | 2021.11.09 |