자물쇠가 원형이므로 거리 계산 시 원형 구조를 고려해야 한다.
즉, 일반적인 차이 계산이 아닌 원형 거리를 적용해야 함.
두 숫자 x와 y 사이의 원형 거리는 다음과 같이 계산됨:
|x - y|N - |x - y|이 원리를 이용하여 특정 숫자가 기준 숫자에서 2 이내에 있는지를 판별할 수 있다.
isWithinRange(int x, int y, int n): 주어진 숫자 x가 기준 숫자 y에서 원형 거리 2 이내인지 확인하는 함수import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
int a2 = sc.nextInt(), b2 = sc.nextInt(), c2 = sc.nextInt();
sc.close();
int count = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
if (isWithinRange(i, a, n) && isWithinRange(j, b, n) && isWithinRange(k, c, n)
|| isWithinRange(i, a2, n) && isWithinRange(j, b2, n) && isWithinRange(k, c2, n)) {
count++;
}
}
}
}
System.out.println(count);
}
private static boolean isWithinRange(int x, int y, int n) {
int dist = Math.min(Math.abs(x - y), n - Math.abs(x - y));
return dist <= 2;
}
}
9
1 2 3
4 5 6
27