Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

各位帮忙看看为什么老是TLE

Posted by fdy1045 at 2009-11-27 02:37:30 on Problem 3278
提交了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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator