标签:str tac cst code math cto ons set algorithm
最近做题养成了一个不太好的习惯,习惯性的先去看discuss有没有坑,越是惧怕错误越可能出错,之后的锻炼,出错再去check discuss吧
简单的BFS
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxn= 1e5;
bool vis[maxn+5];
int dis[maxn+5];
int BFS(const int s, const int e)
{
if (s== e){
return 0;
}
memset(vis, 0, sizeof(vis));
queue<int> Q;
vis[s]= 1;
dis[s]= 0;
Q.push(s);
int cur, v;
while (!Q.empty()){
cur= Q.front();
Q.pop();
v= dis[cur]+1;
int np= cur<<1;
if (e== np){
return v;
}
if (np>= 0 && np<= maxn && !vis[np]){
vis[np]= 1;
dis[np]= v;
Q.push(np);
}
np= cur-1;
if (e== np){
return v;
}
if (np>= 0 && np<= maxn && !vis[np]){
vis[np]= 1;
dis[np]= v;
Q.push(np);
}
np= cur+1;
if (e== np){
return v;
}
if (np>= 0 && np<= maxn && !vis[np]){
vis[np]= 1;
dis[np]= v;
Q.push(np);
}
}
return -1;
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
printf("%d\n", BFS(n, k));
return 0;
}
标签:str tac cst code math cto ons set algorithm
原文地址:https://www.cnblogs.com/Idi0t-N3/p/14679884.html