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

贴个代码留念

Posted by nothing828 at 2010-09-08 00:33:27 on Problem 2248
#include<iostream>
using namespace std;

const int INF=100000000;

struct Que{
	int num,step,pre;
}que[1000002];

int BFS(int n)
{
	int front=0,rear=0,minStep=INF,index;
	que[rear].num=que[rear].step=1;
	que[rear++].pre=-1;
	while(front<rear)
	{
		Que temp=que[front++];
		if(temp.num==n && temp.step<minStep)
		{
			minStep=temp.step;
			index=front-1;
			continue;
		}
		int pre=front-1;
		while(pre!=-1)
		{
			int k=temp.num+que[pre].num;
			if(k<=n && temp.step+1<minStep)
			{
				que[rear].num=k;
				que[rear].step=temp.step+1;
				que[rear++].pre=front-1;
			}
			pre=que[pre].pre;
		}
	}
	return index;
}

void PRINT(int id)
{
	if(que[id].pre!=-1)
		PRINT(que[id].pre);
	printf("%d ",que[id].num);
}

int main()
{
	int n;
	while(scanf("%d",&n) && n!=0)
	{
		PRINT(BFS(n));
		printf("\n");
	}
	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