| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
用STL队列AC了,之前一直WA 都想吐血了#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int Max = 100000 + 10;
int s[Max],vis[Max];
int main()
{
int n,m;
cin>>n>>m;
int x;
queue<int>q;
q.push(n);
s[n] = 0;
while (!q.empty())
{
x = q.front();
q.pop();
vis[x] = 1;
if (x==m) break;
if (x-1>=0 && vis[x-1]==0)
{
q.push(x-1);
s[x-1] = s[x] + 1;
vis[x-1] = 1;
}
if (x+1<100001 && vis[x+1]==0)
{
q.push(x+1);
s[x+1] = s[x] + 1;
vis[x+1] = 1;
}
if (x*2<100001 && vis[x*2]==0)
{
q.push(x*2);
s[x*2] = s[x] + 1;
vis[x*2] = 1;
}
}
cout<<s[m]<<endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator