티스토리 뷰
[ 해결 방법 ]
기본적인 BFS문제인 것 같다. 현재 위치에서 위또는 아래 방향으로 BFS돌리면 된다.
[ 내 코드 ]
#include <iostream>#include <queue>using namespace std;struct info{int floor, cnt;};int F, S, G, U, D;int visit[1000002];int dir[2] = { 1, -1 };queue<info> q;int main(){cin >> F >> S >> G >> U >> D;dir[0] *= U;dir[1] *= D;q.push(info{ S, 1 });visit[S] = 1;info cur, next;while (!q.empty()){cur = q.front();q.pop();for (int i = 0; i <= 1; i++){next.floor = cur.floor + dir[i];next.cnt = cur.cnt + 1;if (next.floor > F || next.floor < 1)continue;if (visit[next.floor] == 0){visit[next.floor] = next.cnt;q.push(next);}}}if (visit[G])cout << visit[G] - 1 << endl;elsecout << "use the stairs" << endl;return 0;}
'알고리즘 > 너비우선탐색(BFS)' 카테고리의 다른 글
너비우선탐색 BFS (백준-바이러스) (0) | 2016.12.20 |
---|---|
백준_숨바꼭질 (0) | 2016.09.11 |
백준_트리의 지름길 (0) | 2016.09.03 |
백준_다리 만들기 (1) | 2016.07.11 |
백준_이분 그래프 (0) | 2016.07.08 |