ABOUT ME

Today
Yesterday
Total
  • 1485 정사각형
    백준/기하학 2022. 6. 9. 21:08

     

    1. 풀이

     

    정사각형을 만들 수 있는지 구하라고 해서,

     

    이런 경우만 생각했다.

     

     

    하지만, 

     

     

     

     

    이런 경우도 정사각형이다.

    회전 된 상태라고 생각하면 된다.

     

     

    그래서 정사각형을 구할 때, 

     

    1) 네 변의 길이가 같은지

    2) 대각선의 길이가 같은지

     

    두 가지 경우를 확인해주면 된다.

    즉, 네 변의 길이가 모두 같고,

     

     

     

     

     

    2개의 대각선이 길이가 같다면 정사각형이다.

     

     

     

     

    2. 최종 코드

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.StringTokenizer;
    
    class Position{
    	int x;
    	int y;
    	
    	Position(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    }
    
    public class Main {
    	
    	public static void main(String[] args) throws IOException {
    				
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		int T = Integer.parseInt(br.readLine());
    	
    		for(int i=0; i<T; i++) {
    			ArrayList<Position> input = new ArrayList<>();
    			
    			for(int j=0; j<4;j++) {
    				StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    				input.add(new Position(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
    			}
    			
    			ArrayList<Double> length = new ArrayList<>();
    			
    			for(int a=0; a<4; a++) {
    				for(int b=a+1; b<4; b++) {
    					
    					double one =  Math.pow(input.get(a).x - input.get(b).x, 2);
    					double two = Math.pow(input.get(a).y - input.get(b).y, 2);
    					double result =  Math.sqrt(one + two);
    					
    					length.add(result);
    					
    				}
    			}
    			
    			Collections.sort(length);
    			
    			double value = length.get(0);
    			double value1 = 0;
    			int j = 0;
    			int answer = 1;
    			
    			while(j<4) {
    				
    				value1 = length.get(j);
    				
    				if(value!= value1) {
    					answer = 0;
    					break;
    				}
    				
    				j++;
    			}
    			
    			
    			value = length.get(4);
    			 
    			while(j>=4 && j<6) {
    				
    				value1 = length.get(j);
    				
    				if(value != value1) {
    				
    					answer = 0;
    					break;
    				}
    				
    				j++;
    			}
    				
    			System.out.println(answer);
    		}	
    	}
    }

     

     

    참고 블로그 :

     

    https://nahwasa.com/66

     

    백준 1485 자바 - 정사각형 (BOJ 1485 JAVA)

    [ 문제 보기 ] [ 코드 보기(깃헙) ] [ 틀린 경우 ] 처음엔 아래와 같이 축에 평핸한 정사각형을 찾는줄 알았다. 그래서 예를들어 입력이 1 1 1 1 2 2 1 2 2 와 같다면, 사실 좌측이 x축인지 우측이 x축인

    nahwasa.com

     

    댓글

Designed by Tistory.