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 |
BFS乱搞AC注意三件事 1.把较大的数据放在同一个位置,并且可以证明较小的那个数据不超过200,超过就剪掉 2.hash判重 3.循环队列 #include<cstdio> #include<cstring> #include<algorithm> #define Maxdata 1000000 using namespace std; int a[Maxdata][3]; int p,l,r,ans; bool hash[20001][201]; void fill(int x,int y,int s) { if (x<y) swap(x,y); if (x>20000) return; if (y>200) return; if (hash[x][y]==true) return; hash[x][y]=true; r++; if (r==Maxdata) r=0; a[r][0]=x; a[r][1]=y; a[r][2]=s+1; if (a[r][0]==p or a[r][1]==p) ans=a[r][2]; return; } int bfs() { a[0][0]=1; a[0][1]=0; a[0][2]=0; l=0;r=0; while (true) { fill(a[l][0]*2,a[l][0],a[l][2]); fill(a[l][1]*2,a[l][0],a[l][2]); fill(a[l][0]*2,a[l][1],a[l][2]); fill(a[l][1]*2,a[l][1],a[l][2]); fill(a[l][0]+a[l][1],a[l][0],a[l][2]); fill(a[l][0]+a[l][1],a[l][1],a[l][2]); fill(abs(a[l][0]-a[l][1]),a[l][0],a[l][2]); fill(abs(a[l][0]-a[l][1]),a[l][1],a[l][2]); if (ans!=-1) return ans; l++; if (l==Maxdata) l=0; } } int main() { scanf("%d",&p); memset(hash,0,sizeof(hash)); ans=-1; printf("%d",bfs()); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator