使用案例
BFS 奇怪的电梯
题目选自洛谷P1135
#include
#include<stdio.h>
#include
#include
using namespace std;
int N,A,B;
int book[201];
int nt[201];
queue<pair<int,int> > q;
bool judge(int x){
if(book[x] != 1 && x > 0 && x <= N) return true;
return false;
}
int bfs(int s){
q.push(make_pair(s,0)); book[s] = 1;
while(!q.empty()){
int f = q.front().first;
if(f == B) return q.front().second;
if(judge(f+f[nt])) {
q.push(make_pair(f+f[nt],q.front().second+1));
book[f+f[nt]] = 1;
}
if(judge(f-f[nt])){
q.push(make_pair(f-f[nt],q.front().second+1));
book[f-f[nt]] = 1;
}
q.pop();
}
return -1;
}
int main(){