想到博客有人在看就开始紧张了......
这个题...饿,后面又飞速打了一份,竟然还过了,但是还是要有注意的东西
过了!(感激涕零)
bfs也就是每个都跑一遍,跑到p队列
p有三个情况,后面压进去,前面把每个p分析出来跑
一直到所有的都带上访问标记为止...
#include<iostream>
#include<queue>
using namespace std;
//奶牛的题...我可真tm强大啊 加油
//周末还是得玩玩儿啊(笑cey
int vis[100005] = { 0 };
int record[100005] = { 0 };
//x+1 x-1 2*x
int main()
{
int n, m; while (cin >> n >> m)
{
if (n < m) {
queue<int>q;
q.push(n);//用于初始化,把第一个压入了..
vis[n] = 1;//**************
record[n] = 0;
//*******初始化问题,初始化问题,初始化问题,这样子跑的起来哦???
//(好喜欢新电脑啊 !!!我自己首先写出来的时候是不对的啊
//int k = n;
while (!q.empty()) {
int k = q.front();
q.pop();
int head = k;
//这个k的值被改来改去的吧......老问题了哈哈哈
for (int i = 0; i < 3; i++)
{
k = head;//????初始化问题???
if (i == 0)
k = k - 1;
else if (i == 1)
k = k + 1;
else k = 2 * k;
if (k < 0 || k>100005)continue;
//****这里是||啊..runtime error....
//*****这里不是m啊....要是能达到,要有n-1的话
//这里要是越界....要是越界.... m的话你太狭窄了1太狭窄了
if (vis[k] == 1)continue;
vis[k] = 1;//标记;
q.push(k);//把k压入以便进行下面的运算= -
record[k] = record[head] + 1;
if (k == m)
{
cout << record[k];
return 0;// break;
}
}
}//n m
}
else cout << n-m;
}
return 0;
}
注意的东西
1.n和m还是没给哪个大哪个小= -
这个坑真的是= -少写了那么一个号
2.runtime error.....while (!q.empty()) {
int k = q.front();
q.pop();
int head = k;
for (int i = 0; i < 3; i++)
这个....因为有3个嘛,然后k-1 k+1 k*2
这个k的值要被改变的啊!!!!!
然后Int head=k;
3.if (k < 0 || k>100005)continue;
这个是||啊啊啊
还有就是k这里是越界的
不是m而已
4.
queue<int>q;
q.push(n);//用于初始化,把第一个压入了..
vis[n] = 1;//**************
record[n] = 0;
这里记得初始化
**初始化,初始化记得,把n的vist改成1
因为这个是最近的...不然会出问题吧= -
5.范围记得...
100005
***另外....... 用set的时候如何实现访问?
周六那个天梯赛里面用到,,,count当然效率很低
http://blog.csdn.net/castledrv/article/details/46897575
对于set而言.. insert是一样的 然后你要这么访问 用迭代器啊迭代器
- set<int>::iterator iter;
set<int>::iterator it;
set<int>::iterator it;
- iter = num.begin();
- cout<<*iter;
- iter++;
- for (; iter != num.end(); iter++)
- cout<<" "<<*iter;
- cout<<endl;
- num.clear();
迭代器多好用啊你看 = =
iter=num.begin();
cout<<*iter<<endl;iter++;
for(;iter!num.end();iter++)
cout<<" "<<endl;
num.clear(大概set要清空吧..
周六那个还有个很暴力的x*x+ y*y+z*z
输入的不大于10000那只要循环是100就可以了
或者直接k*k<10000?(记不清了..开黑开个屁啊)
最开始的想法是3*x*x 10000 其实这样的话x最大的是60
似乎是那个三角形定理不行我脑子根本写不下去啊以后在外面打完回来写题解好了