| ||||||||||
| 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 | |||||||||
高手指点哪里出错了#include <iostream>
#include <queue>
using namespace std;
#define MAXN 1000000
struct state { int x; int y; };
bool mark[2*MAXN+2];
int bfs(int n, int k);
int main()
{
int N, K;
scanf("%d%d", &N, &K);
if(N>=K) printf("%d\n", N-K);
else printf("%d\n", bfs(N, K));
system("pause");
}
int bfs(int n, int k)
{
state inistate, s, t;
inistate.x = n;
inistate.y = 0;
queue<state> q;
q.push(inistate);
mark[n] = true;
while(true)
{
s = q.front();
q.pop();
t.x = s.x+1;
t.y = s.y+1;
if(t.x==k) return t.y;
else if(t.x<k && t.x==false)
{
q.push(t);
mark[t.x] = true;
}
t.x = s.x-1;
t.y = s.y+1;
if(t.x==k) return t.y;
else if(t.x>=1 && mark[t.x]==false)
{
q.push(t);
mark[t.x] = true;
}
t.x = s.x*2;
t.y = s.y+1;
if(t.x==k) return t.y;
else if(t.x<k && mark[t.x]==false)
{
q.push(t);
mark[t.x] = true;
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator