火车车厢重排

来源:互联网 发布:jquery-1.4.4.min.js 编辑:程序博客网 时间:2024/05/01 06:01

用stack 来实现
// k个缓冲铁轨,车厢初始排序为 p[1:n]
bool Railroad(int p[], int n, int k)
{
 LinkedStack<int> *H;
 H=new LinkedStack<int>[k+1];
 // 下次要输出的车厢
 int NowOut=1;
 // 缓冲铁轨中编号最小的车厢
 int minH=n+1;
 // minH号车厢对应的缓冲铁轨
 int minS;

 // 车厢重排
 for(int i=1; i<=n;i++)
  if(p[i]==NowOut)
  {
   cout<<"Move car" <<p[i]<<"from input to output"<<endl;
   NowOut++;
   while(minH==NowOut)
   {
    Output(minH, minS, H, k, n);
    NowOut++;
   }
  }
  else
  {
   if(!Hold(p[i],minH,minS,H,k,n))
    return false;
  }
  return ture;
  
}

void OutPut(int& minH, int&minS, LinkedStack<int>H[], int k, int n)
{
 int c;
 // 从堆栈minS中删除编号最小的车厢minH
 H[minS].Delete(c);
 cout<<"Move car"<<minH<<"from holding track"<<minS<<"to output"<<endl;
 // 通过检查所有的栈顶,搜索新的minH和minS
 minH=n+2;
 for(int i=1;i<=k;i++)
 {
  if(!H[i].IsEmpty() && (c=H[i].Top())<minH)
  {
   minH=c;
   minS=i;
  }
 } 
}

// 在一个缓冲铁轨中放入车厢c
bool Hold(int c, int& minH, int& minS, LinkedStack<int>H[], int k, int n)
{
 //目前最优铁轨
 int BestTrack=0;
 //最优铁轨上的头辆车箱
 int BestTop=n+1;
 //车厢索引
 int x;

 for(int i=1; i<=k; i++)
  if(!H[i].IsEmpty())
  {
   x=H[i].Top();
   if(c<x && x<BestTop)
   {
    BestTop=x;
    BestTrack=i;
   }
  }
  else
   if(!BestTrack)
    BestTrack=i;
 if(!BestTrack)
  return false;
 H[BestTrack].Add(c);
 cout<<"Move car"<<c<<"from input to holding track"<<BestTrack<<endl;
 // 必要时修改minH 和minS
 if(c<minH)
 {
  minH=c;
  minS=BestTrack;
 }
 return true;
}

Ref:<<数据结构,算法与应用>>P174~176


用Queue 来实现
void OutPut(int & minH, int & minQ, LinkedQueue < int > H [ ], int k, int n)
{
 // 从缓冲轨移动到出轨,并修改minH和minQ

 int c; //车厢编号

 // 从队列minQ 中删除编号最小的车厢minH
 H[minQ].Delete(c);
 cout<<"Move car"<<minH<<"from holding track"<<minQ<<"to output"<<endl;

 // 通过检查所有队列的首部,寻找新的minH 和minQ
 minH=n+2;
 for(int i=1;i<=k;i++)
  if(!H[i].IsEmpty() && (c=H[i].Frist())<minH)
  {
   minH=c;
   minQ=i;
  }
}

bool Hold(int c, int& minH, int& minQ, LinkedQueue<int> H[], int k)
{
 // 把车厢c 移动到缓冲铁轨中
 // 如果没有可用的缓冲铁轨,则返回false, 否则返回true
 
 // 为车厢c 寻找最优的铁轨

 // 目前最优的铁轨
 int BestTrack=0;
 // BestTrack中最后一节车厢
 int BestLast=0;
 // 车厢编号
 int x;

 // 扫描缓冲铁轨
 for(int i=1; i<=k; i++)
  if(!H[i].IsEmpty())
  {
   x=H[i].Last();
   if(c>x && x>BestLast)
   {
    BestLast=x;
    BestTrack=i;
   }
  }
  else
   if(!BestTrack)
    BestTrack=i;

 if(!BestTrack)
  return false;

 H[BestTrack].Add(c);
 cout<<"Move car"<<c<<"from input"<<"to holding track"<<BestTrack<<endl;

 // 如果有必要,修改minH, minQ
 if(c<minH)
 {
  minH=c;
  minQ=BestTrack;
 }
 return true;
}

Ref:<<数据结构,算法与应用>> P197~199


不用stack 与queue 实现车厢重排
void OutPut(int NowOut, int Track, int& Last)
{
 // 将车厢NowOut 从缓冲铁轨移动到出轨, 并修改Last
 cout <<"Move car" << NowOut <<"from holding track"<<Track<<"to output"<<endl;
 if(NowOut==Last)
  Last=0;
}
bool Hold(int c, int last[], int track[], int k)
{
 // 把车厢c 移动到缓冲铁轨中

 int BestTrack=0;
 int BestLast=0;

 // 扫描缓冲铁轨
 for(int i=1; i<=k; i++)
  if(last[i])
  {
   if(c>last[i] && last[i]>BestLast)
   {
    BestTrack=i;
    BestLast=last[i];
   }
  }
  else
   if(!BestTrack)
    BestTrack=i;
 if(!BestTrack)
  return false;

 track[c]=BestTrack;
 last[BestTrack]=c;
 cout <<"Move car"<<c<<"from input"<<" to holding track"<< BestTrack<< endl;

 return true;
}
bool Railroad(int p[], int n, int k)
{
 // 用k 个缓冲铁轨进行车厢重排, 车厢的初始次序为p[1:n]

 // 对数组last 和track 进行初始化
 int *last=new int [k+1];
 int *track=new int [n+1];
 for(int i=1;i<=k;i++)
  last[i]=0;
 for(int i=1; i<=n; i++)
  track[i]=0;

 int NowOut=1;

 // 按序输出车厢
 for(int i=1; i<=n; i++)
  if(p[i]==NowOut)
  {
   cout<<"Move car"<<p[i]<<"from input to output"<<endl;
   NowOut++;

   // 从缓冲铁轨中输出
   while(NowOut<=n && track[NowOut])
   {
    OutPut(NowOut, track[NowOut], last[track[NowOut]]);
    NowOut++
   }
  }
  else
  {
   if(!Hold(p[i],last, track,k))
    return false;
  }

  return true;
}


Ref:<<数据结构,算法与应用>>P199~201