您的位置:首页 > 其它

UVa101 - The Blocks Problem

2013-04-19 23:23 357 查看
桌上放有若干“blocks”

    


Figure: Initial Blocks World
初始位置如图所示;有五种操作:

(1)move a onto b :先将a和b之上的所有block都复位(恢复为initial状态),然后把a放在b上;

(2)move a over b :先将a之上的所有block都复位,然后把a放在b所在的堆顶端;

(3)pile a onto b :先将b之上的所有block都复位,然后把a以及a之上的所有block放在b之上;

(4)pile a over b :将a以及a之上所有的block放在b所在的堆顶端

(5)quit :退出

Sample Input

10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit

Sample Output

0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:


解题思路:

        应该用链表来做,我用数组模拟了链表,因为觉得指针容易出错。

代码:

#include <string.h>
#include <stdio.h>

#define MAXNUM 25
typedef struct
{
int adress ;
int start ;
int pre ;
int next ;
} BlockType ;

BlockType arr[MAXNUM];

void resetinit(int from)
{
int q ;
int p = arr[from].next ;
arr[from].next = -1 ;
while(p != -1)
{
arr[p].adress = p ;
arr[p].start = p ;
arr[p].pre = -1 ;
q = arr[p].next ;
arr[p].next = -1 ;
p = q ;
}
}

int main()
{
int T ;
scanf("%d" , &T) ;
for(int i = 0 ; i < T ; i ++)
{
arr[i].start = i ;
arr[i].adress = i ;
arr[i].next = -1 ;
arr[i].pre = -1 ;
}

char cmd1[10],cmd2[10];
int pmt1,pmt2;
while(scanf("%s" , cmd1))
{
if(! strcmp(cmd1,"quit"))
break ;
scanf("%d%s%d" , &pmt1 , cmd2 , &pmt2) ;

int cmd ;
if(! strcmp(cmd1,"move"))
{
if(! strcmp(cmd2,"onto"))
cmd = 1 ;
else
cmd = 2 ;
}
else
{
if(! strcmp(cmd2,"onto"))
cmd = 3 ;
else
cmd = 4 ;
}

int p,q ;
switch(cmd)
{
case 1 :
if(arr[pmt1].adress == arr[pmt2].adress)
break ;
resetinit(pmt1);
resetinit(pmt2);

if(arr[pmt1].pre != -1)
{
p = arr[pmt1].pre ;
arr[p].next = -1 ;
}
else
{
p = arr[pmt1].adress ;
arr[p].start =  -1;
}

arr[pmt1].adress = arr[pmt2].adress ;

arr[pmt1].pre = pmt2 ;
arr[pmt2].next = pmt1 ;

break ;

case 2 :
if (arr[pmt1].adress == arr[pmt2].adress)
break ;
resetinit(pmt1);

if(arr[pmt1].pre != -1)
{
p = arr[pmt1].pre ;
arr[p].next = -1 ;
}
else
{
p = arr[pmt1].adress ;
arr[p].start =  -1;
}

arr[pmt1].adress = arr[pmt2].adress ;

p = pmt2 ;
while(p != -1)
{
q = p ;
p = arr[p].next ;
}

arr[q].next = pmt1 ;
arr[pmt1].pre = q ;

break;

case 3 :
if (arr[pmt1].adress == arr[pmt2].adress)
break ;

resetinit(pmt2);

p = arr[pmt1].pre ;
if(p != -1)
{
arr[p].next = -1 ;
}
else
{
q = arr[pmt1].adress ;
arr[q].start = -1 ;
}

p = pmt1 ;
while(p != -1)
{
arr[p].adress = arr[pmt2].adress ;
p = arr[p].next ;
}

arr[pmt2].next = pmt1 ;
arr[pmt1].pre = pmt2 ;

break;

case 4 :
if (arr[pmt1].adress == arr[pmt2].adress)
break ;

p = arr[pmt1].pre ;
if(p != -1)
{
arr[p].next = -1 ;
}
else
{
q = arr[pmt1].adress ;
arr[q].start = -1 ;
}

p = pmt1 ;
while(p != -1)
{
arr[p].adress = arr[pmt2].adress ;
p = arr[p].next ;
}

p = pmt1 ;
q = pmt2 ;
while(arr[q].next != -1)
{
q = arr[q].next ;
}

arr[q].next = p ;
arr[p].pre = q ;

break;
}
}

for(int i = 0 ; i < T ; i ++)
{
printf("%d:" , i) ;
int tmp = arr[i].start ;
while(tmp != -1)
{
printf(" %d" , tmp) ;
tmp = arr[tmp].next ;
}
printf("\n") ;
}

return 0 ;
}


CT说用了cin,cout,又是c语言风格,乱七八糟的……苦口婆心劝说下,老老实实改成了scanf和printf,结果AC的时间减少了0.004~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: