| ||||||||||
| 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 | |||||||||
晕,BFS超了,不知道是卡STL,还是没优化,G++过,C++超,附Code#include<iostream>
#include<queue>
#include<stdio.h>
#include<iostream>
using namespace std;
#define MAXN 100001
struct node
{
int data;
int pre;
int no;
}road[MAXN];
int ans[101],n;
void bfs(int n)
{
queue<node>temp;
node mid2;
road[0].data=1;
road[0].pre=-1;
road[0].no=0;
int k=1;
bool flag=false;
temp.push(road[0]);
while(!temp.empty()&&!flag)
{
mid2=temp.front();
temp.pop();
if(mid2.data==n)
{
next:
int end=mid2.no,t=0;
while(end!=-1)
{
ans[t++]=road[end].data;
end=road[end].pre;
}
for(int i=t-1;i>=0;i--)
printf("%d ",ans[i]);
printf("\n");
flag=true;
return;
}
int end=mid2.no;
while(end!=-1&&!flag)
{
if(mid2.data+road[end].data<=n)
{
road[k].data=mid2.data+road[end].data;
road[k].pre=mid2.no;
road[k].no=k;
temp.push(road[k]);
k++;
}
if(road[k-1].data==n)
{
mid2=road[k-1];
goto next;
}
end=road[end].pre;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
bfs(n);
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator