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 |
0ms...C++,注意数组和边界问题,开大开小都会RE,水过80题纪念!#include <cstdio> #include <algorithm> using namespace std; int ans,n,m,k,z,q[1000001],d[1000001]; bool f[1000001]; void gao (int x) { int h=0,t=1; q[1]=x; f[x]=1; d[x]=0; while(h<t) { h++; k=q[h]+1; if (f[k]==0&&k<=100000) { t++; q[t]=k; f[k]=1; d[k]=d[q[h]]+1; } k=q[h]-1; if (f[k]==0&&k>=0) { t++; q[t]=k; f[k]=1; d[k]=d[q[h]]+1; } k=q[h]*2; if (f[k]==0&&k<=100000) { t++; q[t]=k; f[k]=1; d[k]=d[q[h]]+1; } if(f[m]==1)return; } } int main () { scanf("%d%d",&n,&m); gao(n); printf("%d\n",d[m]); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator