뚱땅뚱땅

[문제]SWEA 8382 방향전환 본문

알고리즘/swea

[문제]SWEA 8382 방향전환

양순이 2021. 3. 5. 15:48
728x90

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWyNQrCahHcDFAVP&categoryId=AWyNQrCahHcDFAVP&categoryType=CODE&problemTitle=8382&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1#

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

풀이

 

좀만 생각하면 풀린다

public class SWEA_8382 {
	static int startX, startY, endX, endY;
	static int min;
	static int[] dx= {1,0,-1,0};	// 짝수: 가로, 홀수: 세로
	static int[] dy = {0,1,0,-1};
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(in.readLine());
		StringBuilder sb = new StringBuilder();
		
		for(int tc=1;tc<=T;tc++) {
			sb.append("#").append(tc).append(' ');
			StringTokenizer st = new StringTokenizer(in.readLine(), " ");
			
			startX = Integer.parseInt(st.nextToken());
			startY = Integer.parseInt(st.nextToken());
			endX = Integer.parseInt(st.nextToken());
			endY = Integer.parseInt(st.nextToken());
			
			int diffX = Math.abs(startX-endX);
			int diffY = Math.abs(startY-endY);
			int small =Math.min(diffX, diffY);
			int big =Math.max(diffX, diffY);
			if(small==0 && big==1) {
				sb.append(1).append('\n');
			}else {
				int ans = 2 *small;
				int left = big-small;
				if(left>0) {
					if(left%2==0) ans += 2*left;
					else
						ans += 2 * left-1;
				}
				sb.append(ans).append('\n');
			}
			
		}
		System.out.println(sb);
	}
	
}
728x90
Comments