728x90
https://www.acmicpc.net/problem/2578
문제 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
int n = 5, i, j; StringTokenizer st;
HashMap<Integer, int[]> hm = new HashMap<>();
for (i = 0; i < n; ++i) {
st = new StringTokenizer(br.readLine());
for (j = 0; j < n; ++j)
hm.put(Integer.parseInt(st.nextToken()), new int[]{i, j});
}
int[] h = new int[n], v = new int[n], c = new int[2], p; int x, y, a = 0, b = 0, k;
loop :
for (i = 0; i < n; ++i) {
st = new StringTokenizer(br.readLine());
for (j = 0; j < n; ++j) {
a++;
Integer key = Integer.parseInt(st.nextToken());
p = hm.get(key);
x = p[0]; y = p[1];
h[x]++;
v[y]++;
if (x == y) c[0]++;
if (x == n - y - 1) c[1]++;
if (h[x] == n) b++;
if (v[y] == n) b++;
for (k = 0; k < 2; ++k) {
if (c[k] == n) {
c[k] = -1;
b++;
}
}
if (b >= 3)
break loop;
}
}
System.out.println(a);
}
}
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 백준 온라인 저지 ( BOJ )' 카테고리의 다른 글
[백준(Baekjoon)][자바(java)] 1919 : 애너그램 만들기 (0) | 2021.11.06 |
---|---|
[백준(Baekjoon)][자바(java)] 6996 : 애너그램 (0) | 2021.11.06 |
[백준(Baekjoon)][자바(java)] 15666 : N과 M (12) / 백트래킹 (0) | 2021.09.02 |
[백준(Baekjoon)][자바(java)] 15665 : N과 M (11) / 백트래킹 (0) | 2021.09.02 |
[백준(Baekjoon)][자바(java)] 15664 : N과 M (10) / 백트래킹 (0) | 2021.09.02 |