乐视的蚱蜢问题,这么做不知道对不对。
//蚱蜢问题
#include<iostream>
#include<vector>
#include<deque>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
deque<int>d;
d.push_back(0);
int count = 0;
int find = false;
while (!find)
{
int k=0;
while (k<pow(2,count))
{
if (d.front() - count - 1 != n&&d.front() + count + 1 != n)
{
d.push_back(d.front()-count-1);
d.push_back(d.front() +count +1);
d.pop_front();
}
else
{
find = true;
break;
}
k++;
}
count++;
}
cout << count << endl;
}
}
用的一个双向队列,应该是BFS思想
小天才公司福利 1165人发布