作者在 2010-05-25 12:34:57 发布以下内容
用栈写的啊
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define stacksize 100
#define MaxLength 100
const N=100;
struct seqstack
{ int data[stacksize];
int top;
};
void Initial(seqstack *s)
{
s->top=-1;
}
int Isempty(seqstack *s)
{
return s->top==-1;
}
int Isfull(seqstack *s)
{
return int(s->top==stacksize-1);
}
void push(seqstack *s,int x)
{
if (Isfull(s))
{
cout<<"栈上益";
exit(1);
}
else s->data[++s->top]=x;
}
int Pop(seqstack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
return -1;
}
return s->data[s->top--];
}
int Top(seqstack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
exit(1);
}
return s->data[s->top];
}
int Hold(int c,int *minH,int *mins,seqstack H[],int k,int n)
{
int BestTrack=1,i;//目前最优车轨
int BestTop=n+1;//最优车轨上的头辆车厢
int x;//车厢索引
//扫描缓冲铁轨
for(i=1;i<k;i++)
if(!Isempty(&H[i]))//铁轨i不空
{
x=Top(&H[i]);
if(c<x&&x<BestTop)
{//铁轨i顶部的车辆编号最小
BestTop=x;
BestTrack=i+1;
}
}
else//铁轨i为空
if(!BestTrack)
BestTrack=i;
if(!BestTrack)return 0;//没有可用的车轨
push(&H[BestTrack],c);
cout<<"Move car"<<c<<"from input to holding track"<<BestTrack<<"\n";
if(c<*minH)
{
*minH=c;
*mins=BestTrack;
}
return 1;
}
void Output(int *minH,int *mins,seqstack H[],int k,int n)
{
int c,i;
c=Pop(&H[*mins]);
cout<<"Move car"<<*minH<<"from holding track"<<*mins<<"to output\n";
*minH=n+2;
for(i=1;i<k;i++)
if(Isempty(&H[i])&&(c=Top(&H[i]))<*minH)
{
*minH=c;
*mins=i;
}
}
int Railroad(int p[],int n,int k)
{
seqstack *H;
int NowOut=1;
int minH=n+1;
int mins,i;
H=(seqstack *)calloc(sizeof(seqstack),(k));
for(i=0;i<n;i++)
if(p[i]==NowOut)
{
cout<<"移动车厢"<<p[i]<<"从出口到入口\n";
++NowOut;
while(minH==NowOut)
{
Output(&minH,&mins,H,k,n);
NowOut++;
}
}
else{
if(!Hold(p[i],&minH,&mins,H,k,n))
return 0;
}
return 1;
}
void main()
{
int A[N];
int a,d;
cout<<"输入a表示车厢总数和d表示缓冲轨的数 "<<endl;
cin>>a>>d;
cout<<"进入车站车子的号码"<<endl;
for(int b=0;b<a;b++)
cin>>A[b];
Railroad(A,a,d);
}
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#define stacksize 100
#define MaxLength 100
const N=100;
struct seqstack
{ int data[stacksize];
int top;
};
void Initial(seqstack *s)
{
s->top=-1;
}
int Isempty(seqstack *s)
{
return s->top==-1;
}
int Isfull(seqstack *s)
{
return int(s->top==stacksize-1);
}
void push(seqstack *s,int x)
{
if (Isfull(s))
{
cout<<"栈上益";
exit(1);
}
else s->data[++s->top]=x;
}
int Pop(seqstack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
return -1;
}
return s->data[s->top--];
}
int Top(seqstack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
exit(1);
}
return s->data[s->top];
}
int Hold(int c,int *minH,int *mins,seqstack H[],int k,int n)
{
int BestTrack=1,i;//目前最优车轨
int BestTop=n+1;//最优车轨上的头辆车厢
int x;//车厢索引
//扫描缓冲铁轨
for(i=1;i<k;i++)
if(!Isempty(&H[i]))//铁轨i不空
{
x=Top(&H[i]);
if(c<x&&x<BestTop)
{//铁轨i顶部的车辆编号最小
BestTop=x;
BestTrack=i+1;
}
}
else//铁轨i为空
if(!BestTrack)
BestTrack=i;
if(!BestTrack)return 0;//没有可用的车轨
push(&H[BestTrack],c);
cout<<"Move car"<<c<<"from input to holding track"<<BestTrack<<"\n";
if(c<*minH)
{
*minH=c;
*mins=BestTrack;
}
return 1;
}
void Output(int *minH,int *mins,seqstack H[],int k,int n)
{
int c,i;
c=Pop(&H[*mins]);
cout<<"Move car"<<*minH<<"from holding track"<<*mins<<"to output\n";
*minH=n+2;
for(i=1;i<k;i++)
if(Isempty(&H[i])&&(c=Top(&H[i]))<*minH)
{
*minH=c;
*mins=i;
}
}
int Railroad(int p[],int n,int k)
{
seqstack *H;
int NowOut=1;
int minH=n+1;
int mins,i;
H=(seqstack *)calloc(sizeof(seqstack),(k));
for(i=0;i<n;i++)
if(p[i]==NowOut)
{
cout<<"移动车厢"<<p[i]<<"从出口到入口\n";
++NowOut;
while(minH==NowOut)
{
Output(&minH,&mins,H,k,n);
NowOut++;
}
}
else{
if(!Hold(p[i],&minH,&mins,H,k,n))
return 0;
}
return 1;
}
void main()
{
int A[N];
int a,d;
cout<<"输入a表示车厢总数和d表示缓冲轨的数 "<<endl;
cin>>a>>d;
cout<<"进入车站车子的号码"<<endl;
for(int b=0;b<a;b++)
cin>>A[b];
Railroad(A,a,d);
}