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 |
Tags
- DFS
- 게임개발
- 인프런
- 알고리즘
- c++
- 시리얼라이제이션
- 개발일지
- 이득우
- binary_search
- BFS
- 이분탐색
- 유한상태기계
- fsm
- c#
- unity
- 유니티
- 너비우선탐색
- 운영체제
- UI 자동화
- 안드로이드
- 언리얼
- upper_bound
- 백준
- lower_bound
- 프로그래머스
- 재귀
- 게임개발공모전
- 웅진씽크빅
- unreal
- 구현
Archives
- Today
- Total
초고교급 희망
[프로그래머스][C++] 게임 맵 최단거리 본문
728x90
#include<vector>
#include<queue>
using namespace std;
int solution(vector<vector<int> > maps)
{
int n = maps.size();
int m = maps[0].size();
vector<vector<int> > dist(n, vector<int>(m, -1));
dist[0][0]= 1;
queue<pair<int, int>> q;
q.push({0, 0});
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
while(!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>= 0 && nx < n && ny >= 0 && ny < m && maps[nx][ny] == 1 && dist[nx][ny] == -1)
{
dist[nx][ny] = dist[x][y] + 1;
q.push({nx, ny});
}
}
}
return dist[n - 1][m - 1];
}
BFS 연습하기 좋은 간단한 문제였습니다.
728x90
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스][C++] 귤 고르기 (0) | 2023.10.19 |
---|---|
[프로그래머스][C++] 카펫 (2) | 2023.06.03 |
[프로그래머스][C++] 타겟 넘버 (2) | 2023.06.01 |
[프로그래머스][C++]삼총사 (0) | 2023.06.01 |