Advertisement
eddiehy

Untitled

Oct 25th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. struct blocks
  4. {
  5. int location;
  6. int upcount;
  7. int up[24];
  8. };
  9. void moveback(struct blocks *b,int objA)
  10. {
  11. for (int i=0;i<b[objA].upcount;i++)
  12. {
  13. int num=b[objA].up[i]; //up list裡東西歸位
  14. b[num].location=num;
  15. }
  16. b[objA].upcount=1; //clean upcount
  17. }
  18. void move(struct blocks *b, int objA,int objB)
  19. //a 放到 b上面,a不見得在自己位置上
  20. {
  21. //printf("%d %d\n",b[objA].upcount,b[objB].upcount);
  22. for(int i=0;i<b[objA].upcount;i++)
  23. {
  24. b[objB].up[i+b[objB].upcount]=b[objA].up[i];
  25. }
  26. b[objA].location=b[objB].location;
  27. b[objB].upcount=b[objB].upcount+b[objA].upcount;
  28. //printf("%d %d\n",b[objA].upcount,b[objB].upcount);
  29.  
  30. }
  31.  
  32. int main()
  33. {
  34. int n;
  35. scanf("%d",&n); //多少blocks
  36. char action1[10];
  37. struct blocks b[25];
  38. for (int i=0;i<n;i++)
  39. {
  40. b[i].location=i;
  41. b[i].upcount=1;
  42. b[i].up[0]=i;
  43. }
  44. //輸入語句是可以用多個scanf及變數拼出來的
  45. scanf("%s",&action1);//管第一個動作及quit
  46. int objA;
  47. char action2[10];
  48. int objB;
  49. //動作可以正確執行,但是執行第二次開始就會在while無窮迴圈跑不出來
  50. while(strcmp(action1,"quit")!=0)
  51. //字串比對要用strcmp,不能用==,字串相同是傳回 "0"
  52. {
  53. scanf("%d%s%d",&objA,&action2,&objB);//管第二個動作及兩個變數
  54. if (b[objA].location==b[objB].location)
  55. //在同一堆, 不動作
  56. {
  57. continue;
  58. }
  59. if (strcmp(action1,"move")==0) //move
  60. //onto:a,b上的都要放回去, over:a上的要放回去
  61. {
  62. if (strcmp(action2,"onto")==0)
  63. {
  64. moveback(b,objA);
  65. moveback(b,objB);
  66. }
  67. else
  68. {
  69. moveback(b,objA);
  70. }
  71. move(b,objA,objB);
  72. }
  73. else //pile
  74. //onto:b上面放回去, over:b上面不放回去
  75. {
  76. if(strcmp(action2,"onto")==0)
  77. {
  78. moveback(b,objB);
  79. }
  80. move(b,objA,objB);
  81. }
  82. }
  83. for (int i=0;i<n;i++)
  84. {
  85. if (b[i].location!=i)
  86. {
  87. printf("%d:\n",i);
  88. }
  89. else
  90. {
  91. printf("%d:",i);
  92. for (int j=0;j<b[i].upcount;j++)
  93. {
  94. printf("%d ",b[i].up[j]);
  95. }
  96. printf("\n");
  97. }
  98. }
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement