-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
50 lines (44 loc) · 911 Bytes
/
BFS.cpp
File metadata and controls
50 lines (44 loc) · 911 Bytes
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
#include <cstdio>
#include <queue>
using namespace std;
struct state{
int len;
int time;
state(int l,int t):len(l),time(t){}
state& operator = (const state& s){
len = s.len;
time = s.time;
return *this;
}
};
bool visit[10001];//注注注注注注注:BFS 和 DFS 一定要visit数组记录节点是否被遍历过
int main(){
int N,K;
while(scanf("%d %d",&N,&K) != EOF){
queue<state> q;
q.push(state(N,0));
visit[N] = true;
while(!q.empty()){
state s = q.front();
if(s.len == K){
printf("%d\n",s.time);
break;
}
q.pop();
int l = s.len,t = s.time;
if(l+1 > 0 && l+1 < 10001){
q.push(state(l+1,t+1));
visit[l+1] = true;
}
if(l-1 > 0 && l-1 < 10001){
q.push(state(l-1,t+1));
visit[l-1] = true;
}
if(l*2 > 0 && l*2 < 10001){
q.push(state(l*2,t+1));
visit[l*2] = true;
}
}
}
return 0;
}