알고리즘/SWEA

[Java] SWEA 4014번 활주로 건설

컴공맨 2021. 5. 23. 23:30
 

SW Expert Academy

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

swexpertacademy.com


풀이

문제에서 주어진 조건에 맞추어 해결했습니다.


코드

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

public class Solution {
	public static int N, X;
	public static int[][] map;
	public static boolean[] isSelected;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder("");
		StringTokenizer st = null;		
		
		int T = Integer.parseInt(br.readLine());
		
		for (int tc = 1; tc <= T; ++tc) {
			st = new StringTokenizer(br.readLine(), " ");
			
			N = Integer.parseInt(st.nextToken());
			X = Integer.parseInt(st.nextToken());
			
			map = new int[N][N];
			
			for (int i = 0; i < N; ++i) {
				st = new StringTokenizer(br.readLine(), " ");
				
				for (int j = 0; j < N; ++j) {
					map[i][j] = Integer.parseInt(st.nextToken());
				}
			}
			
			int answer = 0;
			for (int i = 0; i < N; ++i) {
				isSelected = new boolean[N];
				if (goX(i, 0)) {
					answer++;
				}
				
				isSelected = new boolean[N];
				if (goY(0, i)) {
					answer++;
				}
			}
			
			sb.append("#").append(tc).append(" ").append(answer).append("\n");
		}
		
		System.out.println(sb);
	}
	
	public static boolean goX(int y, int x) {
		for (int i = 0; i < N - 1; ++i) {
			int idx = x + i + 1;
			int height = map[y][idx] - map[y][x + i];
			
			if (height == 1) {
				for (int j = i; j > i - X; j--) {
					if (j < 0) {
						return false;
					}
					
					if (map[y][idx] - map[y][j] != 1) {
						return false;
					}
					
					if (isSelected[j]) {
						return false;
					}
					
					isSelected[j] = true;
				}
			}
			else if (height == -1) {
				for (int j = idx; j < idx + X; ++j) {
					if (j >= N) {
						return false;
					}
					
					if (map[y][j] - map[y][i] != -1) {
						return false;
					}
					
					if (isSelected[j]) {
						return false;
					}
					
					isSelected[j] = true;
				}
			}
			else if (height > 1 || height < -1) {
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean goY(int y, int x) {
		for (int i = 0; i < N - 1; ++i) {
			int idx = y + i + 1;
			int height = map[idx][x] - map[y + i][x];
			
			if (height == 1) {
				for (int j = i; j > i - X; j--) {
					if (j < 0) {
						return false;
					}
					
					if (map[idx][x] - map[j][x] != 1) {
						return false;
					}
					
					if (isSelected[j]) {
						return false;
					}
					
					isSelected[j] = true;
				}
			}
			else if (height == -1) {
				for (int j = idx; j < idx + X; ++j) {
					if (j >= N) {
						return false;
					}
					
					if (map[j][x] - map[i][x] != -1) {
						return false;
					}
					
					if (isSelected[j]) {
						return false;
					}
					
					isSelected[j] = true;
				}
			}
			else if (height > 1 || height < -1) {
				return false;
			}
		}
		
		return true;
	}
}

 

pyo7410/Algorithm

1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.

github.com