Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 스프링
- 2638
- 알고리즘
- 서블릿
- mvc
- 쓰레드 풀
- 완도산회
- 1로 만들기
- 프로그래머스
- 포두부 보쌈
- 스프링 MVC
- 고모네 콩탕
- 양꼬치
- 2839
- Spring
- Servlet
- 2020 KAKAO BLIND
- dp
- 문자열 압축
- 맛집
- 2589
- 설탕 배달
- 투어
- 다이나믹 프로그래밍
- 맛집 투어
- 백준
- HTTP API
- 호유동
- BFS
- 동적 프로그래밍
Archives
- Today
- Total
프로그래밍 공방
[프로그래머스] 블록 이동하기 본문
문제
programmers.co.kr/learn/courses/30/lessons/60063
코딩테스트 연습 - 블록 이동하기
[[0, 0, 0, 1, 1],[0, 0, 0, 1, 0],[0, 1, 0, 1, 1],[1, 1, 0, 0, 1],[0, 0, 0, 0, 0]] 7
programmers.co.kr
문제해결방법
로봇이 가로로 있는 경우와 세로로 있는 경우를 다른 배열로 관리해서 문제를 풀었다.
1. 기존 방향을 유지하면서 이동하면 같은 방향의 배열에 기록해주고, 회전을 하면 다른 방향의 배열에 기록해준다.
2. 로봇을 이동할 때, 두 칸 중 하나라도 0이면 이동한다.
3. 위 두 가지 규칙을 지키면서 BFS를 돌며 N, N 칸에 도착하면 그 값을 출력해줬다.
코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package Programmers; import java.util.ArrayDeque; import java.util.Queue; public class Solution_블록이동하기 { public static int n; public static int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static int[][] map; public static int[][][] count; public static boolean isMove(int x, int y) { if(x>=0 && x<n && y>=0 && y<n && map[x][y]==0) return true; return false; } public static int bfs(int i1, int j1, int i2, int j2, int k) { int second = 1; count[k][i1][j1] = second; count[k][i2][j2] = second; second++; Queue<int[]> q = new ArrayDeque<int[]>(); q.add(new int[] {i1, j1, i2, j2, k}); while(!q.isEmpty()) { int size = q.size(); for(int s = 0; s<size; s++) { int[] cur = q.poll(); int x1 = cur[0]; int y1 = cur[1]; int x2 = cur[2]; int y2 = cur[3]; for(int d=0; d<dir.length; d++) { int xx1 = x1+dir[d][0]; int yy1 = y1+dir[d][1]; int xx2 = x2+dir[d][0]; int yy2 = y2+dir[d][1]; if(isMove(xx1, yy1) && isMove(xx2, yy2)) { if(count[cur[4]][xx1][yy1]==0 || count[cur[4]][xx2][yy2]==0) { count[cur[4]][xx1][yy1] = second; count[cur[4]][xx2][yy2] = second; q.add(new int[] {xx1, yy1, xx2, yy2, cur[4]}); } } } if(cur[4]==0) { if(isMove(x1-1, y1) && isMove(x1-1, y2)) { if(count[1][x1-1][y1]==0) { count[1][x1][y1]=second; count[1][x1-1][y1]=second; q.add(new int[] {x1, y1, x1-1, y1, 1}); } if(count[1][x1-1][y2]==0) { count[1][x1][y2]=second; count[1][x1-1][y2]=second; q.add(new int[] {x1, y2, x1-1, y2, 1}); } } if(isMove(x1+1, y1) && isMove(x1+1, y2)) { if(count[1][x1+1][y1]==0) { count[1][x1][y1]=second; count[1][x1+1][y1]=second; q.add(new int[] {x1, y1, x1+1, y1, 1}); } if(count[1][x1+1][y2]==0) { count[1][x1][y2]=second; count[1][x1+1][y2]=second; q.add(new int[] {x1, y2, x1+1, y2, 1}); } } } else { // 세로 if(isMove(x1, y1-1) && isMove(x2, y1-1)) { if(count[0][x1][y1-1]==0) { count[0][x1][y1]=second; count[0][x1][y1-1]=second; q.add(new int[] {x1, y1, x1, y1-1, 0}); } if(count[0][x2][y1-1]==0) { count[0][x2][y1]=second; count[0][x2][y1-1]=second; q.add(new int[] {x2, y1, x2, y1-1, 0}); } } if(isMove(x1, y1+1) && isMove(x2, y1+1)) { if(count[0][x1][y1+1]==0) { count[0][x1][y1]=second; count[0][x1][y1+1]=second; q.add(new int[] {x1, y1, x1, y1+1, 0}); } if(count[0][x2][y1+1]==0) { count[0][x2][y1]=second; count[0][x2][y1+1]=second; q.add(new int[] {x2, y1, x2, y1+1, 0}); } } } if(count[0][n-1][n-1]!=0 || count[1][n-1][n-1]!=0) return second-1; } second++; } return 0; } public static int solution(int[][] board) { int answer = 0; map = board; n = board.length; count = new int[2][n][n]; answer = bfs(0, 0, 0, 1, 0); return answer; } public static void main(String[] args) { int[][] board = {{0, 0, 0, 1, 1},{0, 0, 0, 1, 0},{0, 1, 0, 1, 1},{1, 1, 0, 0, 1},{0, 0, 0, 0, 0}}; System.out.println(solution(board)); } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[프로그래머스] 괄호 변환 (0) | 2021.01.20 |
---|---|
[백준] 1194번 : 달이 차오른다, 가자. (0) | 2021.01.20 |
[프로그래머스] 기둥과 보 설치 (0) | 2021.01.14 |
[백준] 1654번 : 랜선 자르기 (0) | 2021.01.12 |
[백준] 2252번 : 줄 세우기 (0) | 2021.01.12 |