직각이등변삼각형찾기_백준_2658

이동천·2021년 12월 1일
0

올림피아드 백준

목록 보기
4/4
  1. 알고리즘 문제라기보다는 빡구현 문제
  2. 예외처리및 각각 상황파악이 중요한것으로 보인다.
  3. 결국 풀지 못해 검색을 했는데 자바로 된 풀이가 없어 파이썬으로 된 풀이를 참고 했다.
  4. ==의 용도를 명확히 파악하지 못해 시간을 낭비하는 문제 발생
  5. "==" 비교 연산자는 주소값을 비교하고 equals() 메소드는 내용 자체를 비교 즉 데이터 값을 비교한다.하지만 equals를 사용해도 자체적으로 만든 class에는 적용이 안되는 것 같다.

//주석첨부예정

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

public class triangle {
	// x,y좌표를 받아줄 클래스 생성 
	static class position{
		int x;
		int y;
		public position(int x, int y) {
			this.x = x;
			this.y = y;  	
		}
	}
	
	
	static int[][] map;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		map = new int[10][10];
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		position pos;
		position row_pos = new position(0,0);
		position col_pos = new position(0,0);
		
		int row_max = 0;
		int col_max = 0;
		
		//입력을 받아 맵을 구현한다.
		for(int i=0; i<10; ++i) {
			String s = br.readLine();
			for(int j=0; j<10; ++j) {
				map[i][j] = s.charAt(j) - '0';
			}
		}
		
		// 가로세로길이의 차이
		for(int i=0; i<10; ++i) {
			for(int j=0; j<10; ++j) {
				if(map[i][j]==1) {
					pos = new position(i,j);
					int row_len = 1;
					int col_len = 1;
					for(int j2 = j+1; j2<10; ++j2) {
						if(map[i][j2] == 1) {
							row_len++;
						}
						if(row_len > row_max) {
							row_max= row_len;
							row_pos = pos;
						}
					}
					
					for(int i2 = i+1; i2<10; ++i2) {
						if(map[i2][j] == 1) {
							col_len++;
						}
						if(col_len > col_max) {
							col_max = col_len;
							col_pos = pos;
						}
					}
					}
				}
			}
		
		
		position non = new position(-1,-1);
		if(row_max == 0 || col_max == 0) {
			System.out.print(0);
		}else {
			// 각 정점의 위치와 그 정점을 기준으로 한 최대 길이를 넘겨준다. 
			position[] answers= check(row_pos,col_pos,row_max,col_max);
			for(position pos1 : answers) {
				if(pos1.x == 1 ) {
					System.out.print(0);
					break;
				}
				System.out.printf("%d %d\n" ,pos1.x + 1, pos1.y + 1);
				System.out.printf("%d %d\n" ,non.x + 1, non.y + 1);
			}
		}
	
		// check
	}
	
	static public position[] check(position row_pos, position col_pos, int row_max, int col_max) {
		  position non = new position(-1,-1);
		  position first;
		  position second;
		  position third;
//		  position[] answer= new position[3];
		  //만약 가로세로 길이가 같다면 
		if(row_max == col_max) {
			//111
			//110
			//100
			
			if(row_pos.x == col_pos.x && row_pos.y == col_pos.y) {
				for(int i=0; i<col_max; ++i) {
					for(int j =0; j<row_max -i; ++j) {
						if (map[row_pos.x + i][row_pos.y + j] != 1) {
							position[] answer = {non};
							return answer;
						}
					}
				}
				first = new position(row_pos.x,row_pos.y);
				second = new position(row_pos.x,row_pos.y + row_max-1);
				third = new position(row_pos.x + col_max -1, row_pos.y);
				position[] answer = {first,second,third};
				return answer;
			}
			
			//111
			//011
			//001
			if((row_pos.y + row_max -1) == col_pos.y && row_pos.x == col_pos.x) {
				for(int i =0; i< col_max; ++i) {
					for(int j=i; j<row_max; ++j) {
						if(map[row_pos.x +i][row_pos.y +j] != 1) {
							position[] answer = {non};
							return answer;
						}
					}
				}
				first = new position(row_pos.x,row_pos.y);
				second = new position(row_pos.x,row_pos.y + row_max-1);
				third = new position(row_pos.x + col_max -1, row_pos.y + row_max -1);
				position[] answer = {first,second,third};
				return answer;
			}
			
			//100
			//110
			//111
			if(row_pos.y == col_pos.y && row_pos.x == (col_pos.x + col_max -1)) {
				for(int i =0; i< col_max; ++i) {
					for(int j=0; j<i+1; ++j) {
						if(map[col_pos.x + i][col_pos.y +j] != 1) {
							position[] answer = {non};
							return answer;
						}
					}
				}
				first = new position(col_pos.x,col_pos.y);
				second = new position(col_pos.x + col_max -1 , col_pos.y);
				third = new position(col_pos.x + col_max -1, col_pos.y + row_max -1);
				position[] answer = {first,second,third};
				return answer;
				
			}
			
			//001
			//011
			//111
			if((row_pos.y + row_max -1) == col_pos.y && row_pos.x == (col_pos.x + col_max -1)) {
				for(int i =0; i<col_max; ++i) {
					for(int j = row_max -i -1; j<row_max; ++j) {
						if(map[col_pos.x + i][row_pos.y + j] != 1) {
							position[] answer = {non};
							return answer;
						}
					}
				}
				first = new position(col_pos.x,col_pos.y);
				second = new position(row_pos.x , row_pos.y);
				third = new position(col_pos.x + col_max -1, col_pos.y);
				position[] answer = {first,second,third};
				return answer;
			}
			
			//가로 세로 길이가 다를때
		}else{
			
			if(row_max %2 ==1 && row_max /2 +1 == col_max) {
				
				//11111
				//01110
				//00100
				if((row_pos.y + row_max /2) == col_pos.y && row_pos.x == col_pos.x) {
					for(int i=0 ; i<col_max; ++i) {
						for(int j = -col_max +i +1; j< col_max -i; ++j) {
							if(map[col_pos.x +i][col_pos.y + j] != 1) {
								position[] answer = {non};
								return answer;
							}
						}
					}
					first = new position(row_pos.x,row_pos.y);
					second = new position(row_pos.x , row_pos.y + row_max -1);
					third = new position(col_pos.x + col_max -1, col_pos.y);
					position[] answer = {first,second,third};
					return answer;
					
				}
				
				//00100
				//01110
				//11111
				if((row_pos.y + row_max /2) ==col_pos.y && row_pos.x ==(col_pos.x + col_max -1)) {
					for(int i =0 ; i< col_max; ++i) {
						for(int j= -i; j<i+1; ++j) {
							if(map[col_pos.x + i][col_pos.y + j] != 1) {
								position[] answer = {non};
								return answer;
							}
						}
  					}
					first = new position(col_pos.x,col_pos.y);
					second = new position(row_pos.x , row_pos.y);
					third = new position(row_pos.x , row_pos.y + row_max -1);
					position[] answer = {first,second,third};
					return answer;
				}
				
			}
			
			if(col_max %2 ==1 && row_max == col_max/2 +1) {
				//10000
				//11000
				//11100
				//11000
				//10000
				if((col_pos.x +col_max /2) == row_pos.x && col_pos.y == row_pos.y) {
					for(int j =0; j<row_max; ++j) {
						for(int i =-row_max +j +1; i<row_max -j; ++i) {
							if(map[row_pos.x +i][row_pos.y +j] != 1) {
								position[] answer = {non};
								return answer;
							}
						}
					}
					first = new position(col_pos.x,col_pos.y);
					second = new position(row_pos.x , row_pos.y+row_max -1);
					third = new position(col_pos.x + col_max -1 , col_pos.y);
					position[] answer = {first,second,third};
					return answer;
					
				}
				//00001
				//00011
				//00111
				//00011
				//00001
				
				if((col_pos.x + col_max/2) == row_pos.x && col_pos.y == (row_pos.y + row_max -1)) {
					for(int j =0; j<row_max; ++j) {
						for(int i =-j ; i<j+1; ++i) {
							if(map[row_pos.x + i][row_pos.y +j] != 1) {
								position[] answer = {non};
								return answer;
							}
						}
					}
					first = new position(col_pos.x,col_pos.y);
					second = new position(row_pos.x , row_pos.y);
					third = new position(col_pos.x + col_max -1 , col_pos.y);
					position[] answer = {first,second,third};
					return answer;	
				}
			}
			
		}
		position[] answer = {non};
		return answer;
	}
}
profile
안드개발자

0개의 댓글

관련 채용 정보