728x90
2178๋ฒ: ๋ฏธ๋ก ํ์
์ฒซ์งธ ์ค์ ๋ ์ ์ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ M๊ฐ์ ์ ์๋ก ๋ฏธ๋ก๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ๊ฐ์ ์๋ค์ ๋ถ์ด์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
www.acmicpc.net
โจ ๋ด ์์ค ์ฝ๋
#include<iostream>
#include <string>
#include<vector>
#include<queue>
using namespace std;
int N, M;
int MAP[101][101];
string temp;
int Route[4][2] = { {0,-1},{1,0},{0,1},{-1,0} };
void bfs() {
queue<pair<int,int>> q;
q.push(make_pair(0, 0));
int nx, ny;
pair<int, int> current;
while (!q.empty()) {
current = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
nx = current.first + Route[i][0];
ny = current.second + Route[i][1];
if (0 <= nx && nx < N && 0 <= ny && ny < M && MAP[nx][ny]==1) {
q.push(make_pair(nx, ny));
MAP[nx][ny] = MAP[current.first][current.second] + 1;
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N;i++) {
cin >> temp;
for (int j = 0; j < M; j++) {
MAP[i][j] = temp[j]-'0';
}
}
bfs();
cout << MAP[N - 1][M - 1];
return 0;
}
๐ ๋ฌธ์ ํ์ด
๋ฏธ๋ก๋ฅผ ํ์ํ๋ ๊ฒ์ BFS๋ฅผ ์ด์ฉํ์ฌ ์ด๋ ค์ ์์ด ํ์์ง๋ง
์ด๋ํ ์นธ ์๋ฅผ ๊ตฌํ๋ ๋ฐ์์ ๊ณจ๋จธ๋ฆฌ๋ฅผ ์์๋ค
๊ฒฐ๊ตญ DP๋ฅผ ์ด์ง ์๋ ๊ฑธ๋ก ํด๊ฒฐํ์๋ค

โญ ๋ฌธ์ ํ์ด ๊ฒฐ๊ณผ

728x90