The block problem poj1208

POJ1208地址:http://poj.org/problem?id=1208

move  a  onto  b  : 先将 a 和 b 上的其他木块移回到它们的初始位置,然后将木块 a 摞在木块 b 上 .
move  a  over  b  : 先将木块 a 上的其他木块移到它们的初始位置后,然后将木块 a 摞到包含了木块 b 的那一堆木块上面
pile  a  onto  b  : 先将木块 b 上的所有木块移回到它们的初始位置,然后将木块 a 及其上的木块移到木块 b 上 .    
pile  a  over  b  : 将包含木块 a 的那一摞木块移到包含了木块 b 的那一堆木块上面 .
#include <iostream>
using namespace std;

struct Node{
int no;
struct Node *next;
};
struct Node *station[25];
int Bplace[25];

void Init(int n){
for(int i=0;i<n;i++){
station[i]=(struct Node*)malloc(sizeof(struct Node));
station[i]->no=i;
Bplace[i]=i;
station[i]->next=NULL;
}
}
struct Node *Find(int a,bool setNULL){
int b;
struct Node *p,*q;
b=Bplace[a];
if(station[b]->no==a)
{
p=station[b];
if(setNULL){
station[b]=NULL;
}
return p;
}
else{
q=station[b];
p=q->next;
while(p->no!=a){
q=p;
p=p->next;
}
if(setNULL)
q->next=NULL;
return p;
}
}

void f(int a){
struct Node *p;
p=Find(a,false); //constant point to the station[a->no]
if(p->next==NULL)
return;
int j;
while(p->next!=NULL){
j=p->next->no;
station[j]=p->next;
Bplace[j]=j;
p->next=NULL;
p=station[j];
}
}

void move(int a,int b){
if(a==b || Bplace[a]==Bplace[b])
return;
struct Node *pa,*pb;
pa=Find(a,true);
pb=Find(b,false);
while(pb->next){
pb=pb->next;
}
pb->next=pa;
while(pa!=NULL){
Bplace[pa->no]=Bplace[pb->no];
pa=pa->next;
}
}

void monto(int a,int b)
{
f(a);
f(b);
move(a,b);
}
void mover(int a,int b){
f(a);
move(a,b);
}
void ponto(int a,int b){
f(b);
move(a,b);
}
void pover(int a,int b){
move(a,b);
}

int main(){
int n,a,b;
struct Node *p;
char ch1[5],ch2[5];
//freopen("acm.txt","r",stdin);
cin>>n;
Init(n);
while(cin>>ch1 && strcmp(ch1,"quit")!=0){
cin>>a;
cin>>ch2;
cin>>b;
if(strcmp(ch1,"move")==0){
if(strcmp(ch2,"onto")==0)
monto(a,b);
else
mover(a,b);
}
else{
if(strcmp(ch2,"onto")==0)
ponto(a,b);
else
pover(a,b);

}
}//while
for(int i=0;i<n;i++)
{
cout<<i<<": ";
p=station[i];
while(p!=NULL)
{
cout<<p->no<<"";
p=p->next;
}
cout<<endl;
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值