| ||||||||||
| 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里面的queue很实用的!学了一下就用了一下果然很有效!附代码!你绝对不容错过!#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Node
{
int point;
int time;
};
class poj3278
{
public:
int visited[100010];
int s,e;
int result();
};
int poj3278::result()
{
if(s==e)
return 0;
memset(visited,0,sizeof(visited));
visited[s]=1;
Node t;
t.point=s;
t.time=0;
queue<Node> p;
p.push(t);
while(!p.empty())
{
Node temp1,temp2;
temp1=p.front();
p.pop();
if(temp1.point-1>=0&&temp1.point-1<=100000&&!visited[temp1.point-1])
{
visited[temp1.point-1]=1;
temp2.point=temp1.point-1;
temp2.time=temp1.time+1;
if(temp2.point==e)
return temp2.time;
p.push(temp2);
}
if(temp1.point+1>=0&&temp1.point+1<=100000&&!visited[temp1.point+1])
{
visited[temp1.point+1]=1;
temp2.point=temp1.point+1;
temp2.time=temp1.time+1;
if(temp2.point==e)
return temp2.time;
p.push(temp2);
}
if(2*temp1.point>=0&&temp1.point*2<=100000&&!visited[temp1.point*2])
{
visited[temp1.point*2]=1;
temp2.point=2*temp1.point;
temp2.time=temp1.time+1;
if(temp2.point==e)
return temp2.time;
p.push(temp2);
}
}
}
int main()
{
poj3278 t;
while(scanf("%d %d",&t.s,&t.e)!=EOF)
{
int ans=t.result();
cout<<ans<<endl;
}
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator