티스토리 뷰
12851번: 숨바꼭질 2
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때
www.acmicpc.net
백준 소스코드 [C++] 12851 숨바꼭질 2
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
using namespace std;
int arr[100001];
bool check[100001];
void bfs(int n, int k) {
queue<int> q, t,r;
q.push(n);
int cnt = 0;
arr[n] = 1;
check[n] = true;
while (q.empty() != true) {
while (q.empty() != true) {
t.push(q.front());
q.pop();
}
while (t.empty() != true) {
int temp = t.front();
t.pop();
if (temp == k) {
cout << cnt << '\n' << arr[temp];
return;
}
if ((temp + 1 <= 100000 && arr[temp + 1] == 0)) {
for(int i=0;i<arr[temp];i++)r.push(temp + 1);
if (check[temp + 1] == false) {
q.push(temp + 1);
check[temp + 1] = true;
}
}
if ((0 <= temp - 1 && arr[temp - 1] == 0) ) {
for (int i = 0;i < arr[temp];i++)r.push(temp - 1);
if (check[temp - 1] == false) {
q.push(temp - 1);
check[temp - 1] = true;
}
}
if ((2 * temp <= 100000 && arr[2 * temp] == 0) ) {
for (int i = 0;i < arr[temp];i++)r.push(2 * temp);
if (check[2*temp ] == false) {
q.push(2*temp );
check[2*temp ] = true;
}
}
}
while (r.empty() != true) {
arr[r.front()]++;
r.pop();
}
cnt++;
}
return;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n, k;
cin >> n >> k;
bfs(n, k);
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1708 볼록 껍질 (0) | 2020.10.24 |
---|---|
백준 소스코드 [C++] 1764 듣보잡 (0) | 2020.10.22 |
백준 소스코드 [C++] 11722 가장 긴 감소하는 부분 수열 (0) | 2020.10.10 |
백준 소스코드 [C++] 11053 가장 긴 증가하는 부분 수열 (0) | 2020.10.10 |
백준 소스코드 [C++] 9461 파도반 수열 (0) | 2020.10.07 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- mach-o
- WWDC16
- IOS
- 최단경로 문제
- observeOn
- State Restoration
- CPU와 Memory
- 네트워크 플로우
- 코딩대회
- 벨만포드 알고리즘
- 에드몬드 카프 알고리즘
- 네트워크 유량
- 최단경로문제
- 컴퓨터 추상화
- CompositionalLayout
- 포드 풀커슨 알고리즘
- 최대 매칭
- rxswift
- HIG
- WWDC19
- WWDC17
- 벨만포드 시간복잡도
- 강한 순환 참조
- 다익스트라 시간복잡도
- test coverage
- 부스트캠프 6기
- 최단경로 알고리즘
- Testable
- WWDC21
- MeTal
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함