티스토리 뷰
https://programmers.co.kr/learn/courses/30/lessons/1844
코딩테스트 연습 - 게임 맵 최단거리
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11 [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1
programmers.co.kr
BFS로 풀이하였습니다.
C++ 소스 코드
#include <vector>
#include <queue>
using namespace std;
const int dx[] = {0,0,-1,1}, dy[]={-1,1,0,0};
int dist[101][101];
int solution(vector<vector<int> > maps){
dist[0][0]= 1;
int n = maps.size()-1;
int m = maps[0].size()-1;
queue<pair<int, int> > q;
q.push(make_pair(0,0));
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x == n && y ==m){
return dist[x][y];
}
for(int i=0;i<4;i++){
int nx = x+dx[i], ny = y+dy[i];
if (nx <0 || nx >n || ny <0 || ny > m) continue;
if (maps[nx][ny]==0 || dist[nx][ny]!=0) continue;
dist[nx][ny]= dist[x][y]+1;
q.push(make_pair(nx,ny));
}
}
return -1;
}
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
SWEA) 1216 - 회문2 (0) | 2020.08.14 |
---|---|
BOJ) 5427 - 불 (0) | 2020.08.09 |
BOJ) 1966 - 프린터 큐 (0) | 2020.08.02 |
BOJ) 4949 - 균형잡힌 세상 (0) | 2020.07.28 |
BOJ) 9375 - 패션왕 신해빈 (0) | 2020.07.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- blendshape
- 코코아팟
- rxswift
- Reactivex
- Lottie
- 백준온라인저지
- 안드로이드
- ARKit
- Kotlin
- 프로그래머스
- blendshapes
- GraphDB
- infallible
- SwiftUI
- coreml
- C++
- Swift unowned
- Swift weak
- UIHostingController
- cocoapods
- 카카오인턴십
- DispatchQueue
- disposeBag
- rxswift6
- ios
- 백준
- SWEA
- boj
- Swift
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함