알고리즘 - 톱니바퀴

HoJeong Im·2021년 10월 15일
0

Break_Algo

목록 보기
40/46

문제

코드

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

public class BOJ_14891 {
	static char[][] arr = new char[4][8];
	
	static void rotateClockwise(int target) {
		char one12 = arr[target][0];
		for(int j = 7; j >= 1 ; j--) {
			arr[target][(j+1)%8] = arr[target][(j)%8];
		}
		arr[target][1] = one12;
	}
	static void rotateAntiClockwise(int target) {
		char one12 = arr[target][0];
		for(int j = 1; j <= 7 ; j++) {
			arr[target][(j-1)%8] = arr[target][(j)%8];
		}
		arr[target][7] = one12;
	}
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		for(int i = 0; i < 4; i++) {
			char[] array = br.readLine().toCharArray();
			for(int j = 0; j < 8; j++) {
				arr[i] = array;
			}
		}
		
		int k = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < k ; i++) {
			StringTokenizer stk = new StringTokenizer(br.readLine());
			int num = Integer.parseInt(stk.nextToken());
			int direction = Integer.parseInt(stk.nextToken());
			
			if(direction == 1) {
				
				if(num == 1) {
					
					char target = arr[0][2];
					if(target != arr[1][6]) {
						rotateClockwise(0);
						if(arr[1][2] != arr[2][6]) {
							rotateAntiClockwise(1);
							if(arr[2][2] != arr[3][6]) {
								rotateClockwise(2);
								rotateAntiClockwise(3);								
							}
							else {
								rotateClockwise(2);								
							}
						}
						else {							
							rotateAntiClockwise(1);
						}
					}
					else {
						rotateClockwise(0);						
					}
					
				}
				else if(num == 2) {
					
					
					if(arr[1][6] != arr[0][2] && arr[1][2] == arr[2][6]) {
						rotateAntiClockwise(0);
						rotateClockwise(1);
					}
					else if(arr[1][6] == arr[0][2] && arr[1][2] != arr[2][6]) {
						if(arr[2][2] != arr[3][6]) {
							rotateClockwise(3);							
						}
						rotateClockwise(1);					
						rotateAntiClockwise(2);
						
						
					}
					else if(arr[1][6] != arr[0][2] && arr[1][2] != arr[2][6]) {
						rotateAntiClockwise(0);
						
						if(arr[2][2] != arr[3][6]) {
							rotateClockwise(3);							
						}
						rotateAntiClockwise(2);
						rotateClockwise(1);						
						
					}
					else {
						rotateClockwise(1);						
					}
					
					
				}
				else if(num == 3) {
					
					if(arr[2][2] != arr[3][6] && arr[2][6] == arr[1][2]) {
						rotateAntiClockwise(3);
						rotateClockwise(2);		
					}
					else if(arr[2][2] == arr[3][6] && arr[2][6] != arr[1][2]) {
						if(arr[1][6] != arr[0][2]) {
							rotateClockwise(0);							
						}
						rotateAntiClockwise(1);
						rotateClockwise(2);		
					
					}
					else if(arr[2][2] != arr[3][6] && arr[2][6] != arr[1][2]) {
						rotateAntiClockwise(3);
						if(arr[1][6] != arr[0][2]) {
							rotateClockwise(0);							
						}
						rotateAntiClockwise(1);
						rotateClockwise(2);		
					
					}
					else {
						rotateClockwise(2);		
						
					}
					
				}
				else if(num == 4) {
					
					char target = arr[3][6];
				
					if(target != arr[2][2]) {
						rotateClockwise(3);
						if(arr[2][6] != arr[1][2]) {
							rotateAntiClockwise(2);
							if(arr[1][6] != arr[0][2]) {
								rotateClockwise(1);
								rotateAntiClockwise(0);								
							}
							else {
								rotateClockwise(1);								
							}
						}
						else {
							rotateAntiClockwise(2);
						}
					}
					else {		
						rotateClockwise(num-1);
					}
					

					
				}
				
			}	
			else if(direction == -1) {
				
				if(num == 1) {
					
					char target = arr[0][2];
					if(target != arr[1][6]) {
						rotateAntiClockwise(num-1);
						if(arr[1][2] != arr[2][6]) {
							rotateClockwise(1);
							if(arr[2][2] != arr[3][6]) {
								rotateAntiClockwise(2);
								rotateClockwise(3);								
							}
							else {
								rotateAntiClockwise(2);								
							}
						}
						else {
							rotateClockwise(1);
							
						}
					}
					else {
						rotateAntiClockwise(num-1);
					}
				}
				else if(num == 2) {
					
					
					if(arr[1][6] != arr[0][2] && arr[1][2] == arr[2][6]) {
						rotateClockwise(0);
						rotateAntiClockwise(num-1);
					}
					else if(arr[1][6] == arr[0][2] && arr[1][2] != arr[2][6]) {

						if(arr[2][2] != arr[3][6]) {
							rotateAntiClockwise(3);							
						}
						rotateClockwise(2);
						rotateAntiClockwise(num-1);					
						
						
					}
					else if(arr[1][6] != arr[0][2] && arr[1][2] != arr[2][6]) {
						rotateClockwise(0);
						
						if(arr[2][2] != arr[3][6]) {
							rotateAntiClockwise(3);							
						}

						rotateClockwise(2);
						rotateAntiClockwise(num-1);						
						
						
					}
					else {
						rotateAntiClockwise(num-1);						
						
					}
					
					
				}
				else if(num == 3) {
					if(arr[2][2] != arr[3][6] && arr[2][6] == arr[1][2]) {
						rotateClockwise(3);
						rotateAntiClockwise(num-1);		
					}
					else if(arr[2][2] == arr[3][6] && arr[2][6] != arr[1][2]) {
						
						if(arr[1][6] != arr[0][2]) {
							rotateAntiClockwise(0);							
						}

						rotateClockwise(1);
						rotateAntiClockwise(num-1);		
					
					}
					else if(arr[2][2] != arr[3][6] && arr[2][6] != arr[1][2]) {
						rotateClockwise(3);
						
						if(arr[1][6] != arr[0][2]) {
							rotateAntiClockwise(0);							
						}
						rotateClockwise(1);
						rotateAntiClockwise(num-1);		
					
					}
					else {
						rotateAntiClockwise(num-1);		
						
					}
					
					
				}
				else if(num == 4) {
					
					char target = arr[3][6];
					
					if(target != arr[2][2]) {
						
						rotateAntiClockwise(num-1);
						if(arr[2][6] != arr[1][2]) {
							rotateClockwise(2);
							if(arr[1][6] != arr[0][2]) {
								rotateAntiClockwise(1);
								rotateClockwise(0);								
							}
							else {
								rotateAntiClockwise(1);								
							}
						}
						else {
							rotateClockwise(2);							
						}
						
					}
					else {
						
						rotateAntiClockwise(num-1);
					}
					
				}
			}
			
			
//			
			
			
//		System.out.println(Arrays.toString(arr[0]));
//		System.out.println(Arrays.toString(arr[1]));
//		System.out.println(Arrays.toString(arr[2]));
//		System.out.println(Arrays.toString(arr[3]));
//		System.out.println("--------------");	
		}
		
		int num1 = (arr[0][0] -'0') == 0 ? 0: 1; 
		int num2 = (arr[1][0] -'0') == 0 ? 0: 2; 
		int num3 = (arr[2][0] -'0') == 0 ? 0: 4; 
		int num4 = (arr[3][0] -'0') == 0 ? 0: 8; 
		
		System.out.println(num1+num2+num3+num4);
	}
}

회고

  • 반례를 통과하지 못한다는 부분 => 정확한 개발, 엣지 케이스를 접근하지 못함

  • 문제를 제대로 이해하지 못한 채로 코드를 작성했다는 부분이 존재

  • 링크 : 이 링크의 반례를 통과하지 못함

    • sysout를 찍어보고 나서 알게 된다는 자체가 제대로 문제를 이해하지 않고 콘솔로 보려는 행동을 하려고 함
  • 바로 키보드에 손을 대려는 노력 대신, 문제를 더 꼼꼼히 읽고 푸는 노력을 하자

profile
꾸준함이 제일 빠른 길이었다

0개의 댓글