BOJ1261_์๊ณ ์คํ
Coding Test
2022. 9. 23. 14:29
๋ฌธ์์ด๋ก ๋ฐ์์ผ๋ ์์คํค์ฝ๋ 48๋งํผ ๋นผ์ฃผ๋ ๊ฑฐ ์์ง ๋ง๊ธฐ
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#define INF (int(1e9))
using namespace std;
struct Edge {
int pos[2];
int cost;
};
struct cmp {
bool operator() (Edge a, Edge b) {
if (a.cost <= b.cost) return false;
return true;
}
};
int N, M;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int map[101][101];
int d[101][101];
void dijkstra() {
priority_queue <Edge, vector<Edge>, cmp> pq;
pq.push({{0, 0}, 0});
d[0][0] = 0;
while(!pq.empty()) {
Edge cur = pq.top();
pq.pop();
int r = cur.pos[0];
int c = cur.pos[1];
if (cur.cost > d[cur.pos[0]][cur.pos[1]]) continue;
for(int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (nr < 0 || nc < 0 || nr >= N || nc >= M) continue;
if (d[nr][nc] <= cur.cost + map[nr][nc]) continue;
d[nr][nc] = cur.cost + map[nr][nc];
pq.push({{nr, nc}, d[nr][nc]});
}
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> M >> N;
memset(map, 0, sizeof(map));
string temp;
for (int i = 0; i < N; i++) {
cin >> temp;
for (int j = 0; j < M; j++) {
map[i][j] = temp[j] - 48;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
d[i][j] = INF;
}
}
dijkstra();
cout << d[N-1][M-1] << endl;
}'Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| BOJ1922_๋คํธ์ํฌ ์ฐ๊ฒฐ(ํฌ๋ฃจ์ค์นผ, C++) (0) | 2022.09.27 |
|---|---|
| BOJ2665_๋ฏธ๋ก๋ง๋ค๊ธฐ (1) | 2022.09.23 |
| BOJ11779_์ต์๋น์ฉ ๊ตฌํ๊ธฐ2 (0) | 2022.09.22 |
| BOJ4485_๋ น์ ์ท ์ ์ ์ ๊ฐ ์ ค๋ค์ง? (C++) (0) | 2022.09.22 |
| BOJ1504_ํน์ ํ ์ต๋จ ๊ฒฝ๋ก (0) | 2022.09.22 |