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 |
各位帮忙看看为什么老是TLE提交了N便啦...哎 #include <iostream> #include <queue> using namespace std; void com(int n,int k) { queue<int> list; int node=1; int temp=0; int num[200000]={0}; list.push(n); list.push(-10000); while(list.empty() == false) { temp = list.front(); if(temp == k) { cout<<0<<endl;return; } list.pop(); if(temp == -10000) { list.push(-10000); node += 1; } if(temp >= 0) { if(num[temp - 1] == 0) { list.push(temp - 1); num[temp - 1] = 1; if(temp-1 == k) { cout<<node<<endl;return; } } if(num[temp + 1] == 0) { if(temp + 1 <= 100000) num[temp + 1] = 1; { list.push(temp + 1); if(temp + 1 == k) { cout<<node<<endl;return; } } } if(temp * 2 <= 200000) { if(num[temp * 2] == 0) num[temp * 2] = 1; { list.push(temp * 2); if(temp*2 == k) {cout<<node<<endl;return;} } } } } } int main() { int n,k; while(scanf("%d%d",&n,&k) == 2) { com(n,k); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator