Skip to content

Commit

Permalink
11048 이동하기 / BFS, 동적계획법
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored Mar 28, 2021
1 parent 59c776c commit b2741a6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions 2021_03/BJ_11048.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 다이나믹 프로그래밍 -> bora 코드 참고

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int dx[3] = { 0, 1, 1 };
int dy[3] = { 1, 0, 1 };

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n, m;
cin >> n >> m;

vector<vector<int>> map(n, vector<int>(m));
vector<vector<int>> score(n, vector<int>(m));
bool check[1001][1001];

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
score[i][j] = 0;
check[i][j] = false;
}
}

score[0][0] = map[0][0];
check[0][0] = true;
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();

for (int i = 0; i < 3; i++) {
int rx = x + dx[i];
int ry = y + dy[i];

if (rx >= 0 && ry >= 0 && rx < n && ry < m) {
if (check[rx][ry] == false) {
q.push(make_pair(rx, ry));
check[rx][ry] = true;
score[rx][ry] = score[x][y] + map[rx][ry];
}

else if (score[rx][ry] < score[x][y] + map[rx][ry]) {
q.push(make_pair(rx, ry));
score[rx][ry] = score[x][y] + map[rx][ry];
}
}
}
}

cout << score[n - 1][m - 1] << endl;
return 0;
}

0 comments on commit b2741a6

Please sign in to comment.