문제 출처 : https://www.acmicpc.net/problem/12851
<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
|
#include<iostream>
#include<queue>
#define MAX 100001
#define endl '\n'
using namespace std;
int v[MAX];
int n,m;
queue<pair<int,int> > q;
bool finded=true;
int w;
int su;
int mi=999999999;
int cnt;
void bfs(int start)
{
v[start]=true;
q.push({start,0});
int size=0;
while(!q.empty())
{
size=q.size();
cout<<endl;
for(int i=0;i<size;i++)
{
int po=q.front().first;
int count=q.front().second;
q.pop();
v[po]=true;
//cout<<"("<<po<<","<<count<<")"<<' ';
if(po==m)
{
if(count<=mi)
{
mi=count;
cnt++;
} else
{
return ;
}
}
int next=po-1;
if(next>=0&&v[next]==0)
{
q.push({next,count+1});
}
next=po+1;
if(next<=100000&&v[next]==0)
{
q.push({next,count+1});
}
next=po*2;
if(next<=100000&&v[next]==0)
{
q.push({next,count+1});
}
}
}
}
int main(void){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
bfs(n);
cout<<mi<<endl;
cout<<cnt<<endl;
return 0;
}
|
cs |
<결과 화면 >
'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글
7562번_나이트의 이동(BFS) (0) | 2019.12.14 |
---|---|
1707번_이분그래프 (0) | 2019.12.14 |
13549번_숨바꼭질 3 (0) | 2019.12.03 |
13913번_숨바꼭질 4(Bfs) (0) | 2019.12.02 |
1697번_숨바꼭질(Bfs) (0) | 2019.12.02 |