| ||||||||||
| 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 | |||||||||
WTF,交了7、8次啦!超级简单的BFS,尼玛就是要剪枝,利用哈希剪枝,还有如果不用循环队列的话,将数组要开到200000附源代码
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int time,l;
}Q;
Q queue[200000];
int hash[100001];
int main()
{
int n,k,tou=0,wei=0,location;
cin>>n>>k;
queue[wei].time=0;queue[wei].l=n;
wei++;hash[n]=1;
while(tou<wei&&queue[tou].l!=k)
{
location=queue[tou].l;
if (location-1>=0&&location-1<=100000&&hash[location-1]==0)
{
queue[wei].time=queue[tou].time+1;
queue[wei].l=location-1;
hash[location-1]=1;
wei++;
}
if (location+1>=0&&location+1<=100000&&hash[location+1]==0)
{
queue[wei].time=queue[tou].time+1;
queue[wei].l=location+1;
hash[location+1]=1;
wei++;
}
if (location*2>=0&&location*2<=100000&&hash[location*2]==0)
{
queue[wei].time=queue[tou].time+1;
queue[wei].l=location*2;
hash[location*2]=1;
wei++;
}
tou++;
}
cout<<queue[tou].time<<endl;
system("pause");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator