슈콩

[BOJ] 백준 2478 자물쇠 본문

Algorithms/Baekjoon

[BOJ] 백준 2478 자물쇠

shukong 2025. 8. 17. 14:59
회전 범위 확인 + 쉽게 생각하기 !

[문제]

https://www.acmicpc.net/problem/2478

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	int n = Integer.parseInt(st.nextToken());
    	int[] lock = new int[n];
    	st = new StringTokenizer(br.readLine());
    	for(int i=0;i<n;i++) {
    		lock[i] = Integer.parseInt(st.nextToken());
    	}
    	for(int t2=1;t2<n;t2++) {
    		for(int t1=1;t1<n;t1++) {
    			int p = 0;
    			while(p<n) {
        			int lVal = (p+t1) % n + 1;
        			int rVal = lock[((p-t2) % n + n) % n];
        			if(lVal==rVal) p++;
        			else break;
    			}
    			if(p>=n-1) continue;
    			
    			int q = n-1;
    			while(q>=0) {
    				int lVal = (q+t1) % n + 1;
    				int rVal = lock[((q-t2) % n + n) % n];
    				if(lVal==rVal) q--;
    				else break;
    			}
    			if(p>=q) continue;
    			
    			boolean find = true;
    			for(int k=0;k<=(q-p);k++) {
    				int left = (p+t1+k) % n + 1;
    				int right = lock[((q-t2-k) % n + n) % n];
    				if(left!=right) {
    					find = false;
    					break;
    				}
    			}
    			if(find) {
    				System.out.println(t1);
    				System.out.println((p+1)+" "+(q+1));
    				System.out.println(t2);
    				return;
    			}
    		}
    	}
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 4991 로봇청소기  (2) 2025.08.17
[BOJ] 백준 3190 뱀  (2) 2025.08.17
[BOJ] 백준 23291 어항 정리  (2) 2025.08.13
[BOJ] 백준 23290 마법사 상어와 복제  (2) 2025.08.12
[BOJ] 백준 23289 온풍기 안녕!  (4) 2025.08.11