문제 출처 : https://www.acmicpc.net/problem/13913
<C++Code>
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string.h>
#define endl '\n'
using namespace std;
int parent[100001];
vector<int> t;
int z;
int s;
int n,m;
queue<pair<int,int> > q;
int v[100001];
int count1;
vector<int>w;
int sum=0;
int temp;
vector<vector<int> > SCC;
void bfs(int start)
{
q.push({0,start});
v[start]=1;
while(!q.empty())
{
int pox=q.front().second;
count1=q.front().first;
q.pop();
if(pox==m)
{
cout<<count1<<endl;
z=m;
t.push_back(z);
while(count1--)
{
s=parent[z];
t.push_back(s);
z=s;
}
int s=m;
break;
}
if(pox-1>=0&&v[pox-1]==0)
{
parent[pox-1]=pox;
q.push({count1+1,pox-1});
v[pox-1]=1;
}
if(pox+1<=100000&&v[pox+1]==0)
{
parent[pox+1]=pox;
q.push({count1+1,pox+1});
v[pox+1]=1;
}
if(pox*2<=100000&&v[pox*2]==0)
{
parent[pox*2]=pox;
q.push({count1+1,pox*2});
v[pox*2]=1;
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
bfs(n);
reverse(t.begin(),t.end());
for(int i=0;i<t.size();i++)
{
cout<<t[i]<<' ';
}
return 0;
}
|
cs |
'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글
12851번_숨바꼭질 2 (0) | 2019.12.03 |
---|---|
13549번_숨바꼭질 3 (0) | 2019.12.03 |
1697번_숨바꼭질(Bfs) (0) | 2019.12.02 |
7576번_토마토 (Bfs) (0) | 2019.11.27 |
11724번_연결 요소의 개수(Dfs,Bfs) (0) | 2019.11.27 |