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 | 29 | 30 |
| 31 |
Tags
- 러닝일지
- 문제해결
- binary search
- 코딩테스트
- DP
- priorityqueue
- math
- Java
- 알고리즘
- BFS
- SWEA
- 건강
- 이분탐색
- 문제풀이
- SQL
- 투포인터
- db
- MySQL
- 코테
- 시뮬레이션
- oracle
- dfs
- BOJ
- 백준
- 운동기록
- 문자열
- 프로그래머스
- COS PRO
- 자바
- greedy
Archives
- Today
- Total
슈콩
[프로그래머스] Lv.2 게임 맵 최단 거리 본문



[문제]
https://school.programmers.co.kr/learn/courses/30/lessons/1844
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
[소스 코드]
import java.util.*;
class Solution {
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public int solution(int[][] maps) {
int answer = -1;
int n = maps.length;
int m = maps[0].length;
boolean[][] visit = new boolean[n][m];
Queue<int[]> q = new LinkedList<>();
visit[0][0] = true;
q.offer(new int[]{0,0,1});
while(!q.isEmpty()){
int[] curr = q.poll();
if(curr[0]==n-1 && curr[1]==m-1){
return curr[2];
}
for(int d=0;d<4;d++){
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr<0 || nr>=n || nc<0 || nc>=m || visit[nr][nc]) continue;
if(maps[nr][nc]==1){
visit[nr][nc] = true;
q.offer(new int[]{nr,nc,curr[2]+1});
}
}
}
return answer;
}
}'Algorithms > Programmers' 카테고리의 다른 글
| [프로그래머스] Lv.2 가장 큰 수 (0) | 2025.10.08 |
|---|---|
| [프로그래머스] Lv.1 K번째수 (0) | 2025.10.08 |
| [프로그래머스] Lv.1 비밀지도 (0) | 2025.10.07 |
| [프로그래머스] Lv.1 크레인 인형뽑기 게임 (0) | 2025.10.07 |
| [프로그래머스] Lv.1 이상한 문자 만들기 (0) | 2025.10.07 |