Notice
Recent Posts
Recent Comments
- Today
- Total
01-10 22:06
Tags
- ํ๋ก๊ทธ๋๋จธ์ค
- PS
- dp
- ์๋ฃ๊ตฌ์กฐ
- Graph
- database
- ์๋ฐ
- ๋ฒจ๋งํฌ๋
- tree
- OOP
- ๋ฐฑ์ค
- leetcode
- BFS
- MST
- ์์์ ๋ ฌ
- spring
- ๊ตฌํ
- ์๋ฐ์์ ์
- ์กธ์ ์ํ
- CS
- pytorch
- array
- ๋ค์ต์คํธ๋ผ
- Algorithm
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๊ทธ๋ฆฌ๋
- java
- ๋ฌธ๋ฒ
- ์ธํด
- ๋ฐฑ์๋
Link
Partially Committed
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ ๋ณธ๋ฌธ
๐ฅ Algorithm || ๋ฌธ์ ํ์ด/PS
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ
WonderJay 2022. 7. 14. 09:16728x90
๋ฐ์ํ
SMALL
https://school.programmers.co.kr/learn/courses/30/lessons/1844
BFS ๋ฅผ ์ด์ฉํ์ฌ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ฉด ๋๋๋ฐ, dist ๋ฐฐ์ด์ ํ์ฉํ์ฌ ๊ฐ ์ ์๋ ๊ฒฝ๋ก๊น์ง ์์๋๋ ์๊ฐ(?)์ ๊ธฐ๋กํ๋ค. ์ด๋ dist ๋ฐฐ์ด์ ์ด๊ธฐ์ -1 ๋ก ์ค์ ํ๋ฉด visit ๋ฐฐ์ด์ ๋ฐ๋ก ๋์ง ์๊ณ ๋ ๋ฐฉ๋ฌธ ์ฌ๋ถ ์ฒดํฌ๊ฐ ๊ฐ๋ฅํ๋ค.
[C++]
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
int dist[101][101];
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int solution(vector<vector<int> > maps)
{
int answer = 0;
// n x m , 1 ~ n,m ~ 100
// 0 : ๋ฒฝ 1 : ๊ธธ
int n = maps.size(); int m = maps[0].size();
for(int i = 0 ; i < n; i++)
fill(dist[i], dist[i]+m, -1);
queue<pair<int, int>> q;
q.push({0, 0});
dist[0][0] = 0;
while(!q.empty())
{
pair<int, int> cur = q.front();
q.pop();
for(int dir = 0 ; dir < 4 ; dir ++)
{
int nx = cur.first + dx[dir];
int ny = cur.second + dy[dir];
if(nx<0 || nx>= n || ny<0 || ny>=m) continue; // out of bounds
if(maps[nx][ny] == 0 || dist[nx][ny] > 0) continue;
dist[nx][ny] = dist[cur.first][cur.second] + 1;
q.push({nx, ny});
}
}
answer = dist[n-1][m-1];
return answer == -1 ? -1 : answer+1;
}
728x90
๋ฐ์ํ
LIST
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ ๋ ฌ] K ๋ฒ์งธ์ (JAVA) (0) | 2022.09.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ํฐ ์ ๋ง๋ค๊ธฐ (0) | 2022.07.14 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฅ (0) | 2022.07.13 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์์ฐพ๊ธฐ (0) | 2022.07.13 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฐ์ฅ ํฐ ์ (0) | 2022.07.13 |
Comments