[알고리즘] Java / 프로그래머스 / 거리두기 확인하기

정현명·2022년 6월 13일
0

Programmers

목록 보기
3/3
post-thumbnail

[알고리즘] Java / 프로그래머스 / 거리두기 확인하기

문제


문제 링크


접근 방식


응시자 찾으면 응시자로부터 한 칸 검색하여 다른 응시자 있으면 0 리턴

응시자로부터 두 칸 검색하여 응시자 있으면

  • 가로 혹은 세로로 두칸 옆이면 그 사이 한칸이 파티션인지 확인
  • 대각선이면 위쪽 과 아래쪽 사이가 파티션인지 확인

코드


import java.util.Arrays;
class Solution {
    static int R;
	static int C;
	static boolean[][] visited;
	public static int[] solution(String[][] places) {
		int[] answer = new int[places.length];
		Arrays.fill(answer, 1);
		loop : for(int tc=0;tc<places.length;tc++) {
			R = places[tc].length;
			C = places[tc][0].length();
			visited = new boolean[R][C];
			
			for(int i=0;i<R;i++) {
	        	for(int j=0;j<C;j++) {
	        		if(places[tc][i].charAt(j) == 'P') {
	        			if(check(i,j,places[tc])) continue;
	        			answer[tc] = 0;
	        			continue loop;
	        		}
	        	}
	        }
		}
		
		return answer;
        
        
    }

	static int[] dr1 = {0,1,0,-1};
	static int[] dc1 = {1,0,-1,0};
	static int[] dr2VerHor = {0,2,0,-2};
	static int[] dc2VerHor = {2,0,-2,0};
	static int[] dr2Diag = {1,1,-1,-1};
	static int[] dc2Diag = {1,-1,-1,1};
	public static boolean check(int r, int c, String[] place) {
		
		visited[r][c] = true;
		
		// 한 칸 검색
		for(int i=0;i<4;i++) {
			int nextR = r + dr1[i];
			int nextC = c + dc1[i];
			
			if(nextR < 0 || nextR >= R || nextC < 0 || nextC >= C || visited[nextR][nextC] ) continue;
			if(place[nextR].charAt(nextC) == 'P') return false;
		}
		
		// 두 칸 가로세로 검색
		for(int i=0;i<4;i++) {
			int nextR = r + dr2VerHor[i];
			int nextC = c + dc2VerHor[i];
			
			if(nextR < 0 || nextR >= R || nextC < 0 || nextC >= C || visited[nextR][nextC] ) continue;
			if(place[nextR].charAt(nextC) == 'P') {
				if(place[r + dr2VerHor[i]/2].charAt(c + dc2VerHor[i]/2) == 'O') return false;
			}
			
		}
		
		// 두 칸 대각선 검색
		for(int i=0;i<4;i++) {
			int nextR = r + dr2Diag[i];
			int nextC = c + dc2Diag[i];
			
			if(nextR < 0 || nextR >= R || nextC < 0 || nextC >= C || visited[nextR][nextC] ) continue;
			if(place[nextR].charAt(nextC) == 'P') {
				if(place[r].charAt(nextC) == 'O' || place[nextR].charAt(c) == 'O') return false;
			}
		}
		
		return true;
	
	}
}
profile
꾸준함, 책임감

0개의 댓글