大家小心hdu2717,本身这道题就是一个裸的bfs,而且是状态转移特别简单的题。
但是题目说明的不清楚,这道题没有说是多组输入输出,然后样例也只给出了一组输入输出。
导致我死活改不出来,(昨天直接心态炸裂)
受害者视角。大家一定要注意多组输入输出的问题,一定要认真审题啊。
ac代码:
#include <bits/stdc++.h>
using namespace std;
int a, b;
struct node
{
int x;
int time;
};
const int N = 2e6 + 5;
int vis[N];
int check(int x)
{
if (x < 0 || x >= N || vis[x])
return 0;
return 1;
}
void bfs()
{
struct node cur, nex;
cur.x = a;
vis[cur.x] = 1;
cur.time = 0;
queue<node> qu;
qu.push(cur);
while (!qu.empty())
{
cur = qu.front();
qu.pop();
if (cur.x == b)
{
printf("%d\n", cur.time);
return;
}
else
{
nex.x = cur.x + 1;
if (check(nex.x))
{
vis[nex.x] = 1;
nex.time = cur.time + 1;
qu.push(nex);
}
nex.x = cur.x - 1;
if (check(nex.x))
{
vis[nex.x] = 1;
nex.time = cur.time + 1;
qu.push(nex);
}
nex.x = cur.x * 2;
if (check(nex.x))
{
vis[nex.x] = 1;
nex.time = cur.time + 1;
qu.push(nex);
}
}
}
}
int main()
{
while (cin >> a >> b)
{
memset(vis, 0, sizeof(vis));
bfs();
}
}