Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct blocks
- {
- int location;
- int upcount;
- int up[24];
- };
- void moveback(struct blocks *b,int objA)
- {
- for (int i=0;i<b[objA].upcount;i++)
- {
- int num=b[objA].up[i]; //up list裡東西歸位
- b[num].location=num;
- }
- b[objA].upcount=1; //clean upcount
- }
- void move(struct blocks *b, int objA,int objB)
- //a 放到 b上面,a不見得在自己位置上
- {
- //printf("%d %d\n",b[objA].upcount,b[objB].upcount);
- for(int i=0;i<b[objA].upcount;i++)
- {
- b[objB].up[i+b[objB].upcount]=b[objA].up[i];
- }
- b[objA].location=b[objB].location;
- b[objB].upcount=b[objB].upcount+b[objA].upcount;
- //printf("%d %d\n",b[objA].upcount,b[objB].upcount);
- }
- int main()
- {
- int n;
- scanf("%d",&n); //多少blocks
- char action1[10];
- struct blocks b[25];
- for (int i=0;i<n;i++)
- {
- b[i].location=i;
- b[i].upcount=1;
- b[i].up[0]=i;
- }
- //輸入語句是可以用多個scanf及變數拼出來的
- scanf("%s",&action1);//管第一個動作及quit
- int objA;
- char action2[10];
- int objB;
- //動作可以正確執行,但是執行第二次開始就會在while無窮迴圈跑不出來
- while(strcmp(action1,"quit")!=0)
- //字串比對要用strcmp,不能用==,字串相同是傳回 "0"
- {
- scanf("%d%s%d",&objA,&action2,&objB);//管第二個動作及兩個變數
- if (b[objA].location==b[objB].location)
- //在同一堆, 不動作
- {
- continue;
- }
- if (strcmp(action1,"move")==0) //move
- //onto:a,b上的都要放回去, over:a上的要放回去
- {
- if (strcmp(action2,"onto")==0)
- {
- moveback(b,objA);
- moveback(b,objB);
- }
- else
- {
- moveback(b,objA);
- }
- move(b,objA,objB);
- }
- else //pile
- //onto:b上面放回去, over:b上面不放回去
- {
- if(strcmp(action2,"onto")==0)
- {
- moveback(b,objB);
- }
- move(b,objA,objB);
- }
- }
- for (int i=0;i<n;i++)
- {
- if (b[i].location!=i)
- {
- printf("%d:\n",i);
- }
- else
- {
- printf("%d:",i);
- for (int j=0;j<b[i].upcount;j++)
- {
- printf("%d ",b[i].up[j]);
- }
- printf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement