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 |
RE了n次,终于过了,贴代码,大家注意使用队列时范围的问题~~~#include <iostream> #include <queue> using namespace std; int n,k; int number[210000]; int visit[210000]; int main() { while(scanf("%d%d",&n,&k) != EOF) { queue<int>q; memset(number,0,sizeof(number)); memset(visit,0,sizeof(visit)); q.push(n); number[n] ++; visit[n] ++; while(!q.empty()) { int cf = q.front(); if(cf == k) break; q.pop(); if(!visit[cf - 1] && cf - 1 <= 100000 && cf - 1 >= 0) { q.push(cf - 1); visit[cf - 1] ++; number[cf - 1] += number[cf] + 1; } if(!visit[cf + 1] && cf + 1 <= 100000 && cf + 1 >= 0) { q.push(cf + 1); visit[cf + 1] ++; number[cf + 1] += number[cf] + 1; } if(!visit[2 * cf] && cf * 2 <= 100000 && cf * 2 >= 0) { q.push(2 * cf); visit[2 * cf] ++; number[2 * cf] += number[cf] + 1; } } while(!q.empty()) q.pop(); printf("%d\n",number[k] - 1); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator