B -- POJ 1208 The Blocks Problem

我不是女神ヾ 2021-09-29 13:52 370阅读 0赞

参考:https://blog.csdn.net/yxz8102/article/details/53098575

https://www.cnblogs.com/tanjuntao/p/8678927.html

  1. 1 #include <iostream>
  2. 2 #include <cstdio>
  3. 3 #include <vector>
  4. 4 #define N 30
  5. 5 using namespace std;
  6. 6 int n,a,b;
  7. 7 string s,sa;
  8. 8 vector<int> v[N];
  9. 9 void init()
  10. 10 {
  11. 11 int i;
  12. 12 for (i=0;i<N;i++)
  13. 13 {
  14. 14 v[i].clear();
  15. 15 v[i].push_back(i);
  16. 16 }
  17. 17 }
  18. 18 void test()
  19. 19 {
  20. 20 cout<<s<<a<<sa<<b<<endl;
  21. 21 }
  22. 22 bool judge(int& ba,int& bb,int& ha,int& hb)//判断输入合法性 ,地址传参
  23. 23 {
  24. 24 int i,j;
  25. 25 for (i=0;i<n;i++)
  26. 26 {
  27. 27 for (j=0;j<v[i].size();j++)
  28. 28 {
  29. 29 if (v[i][j]==a)
  30. 30 {
  31. 31 ba=i;
  32. 32 ha=j;
  33. 33 }
  34. 34 if (v[i][j]==b)
  35. 35 {
  36. 36 bb=i;
  37. 37 hb=j;
  38. 38 }
  39. 39 }
  40. 40 }
  41. 41 if (ba==bb)
  42. 42 {
  43. 43 return true;
  44. 44 }
  45. 45 else
  46. 46 {
  47. 47 return false;
  48. 48 }
  49. 49 }
  50. 50 void clear(int block,int high)
  51. 51 {
  52. 52 int i;
  53. 53 for (i=high+1;i<v[block].size();i++)
  54. 54 {
  55. 55 int t=v[block][i];
  56. 56 v[t].push_back(t);
  57. 57 }
  58. 58 v[block].resize(high+1);
  59. 59 }
  60. 60 void pile(int pba,int pbb,int pha,int phb)
  61. 61 {
  62. 62 int i;
  63. 63 for (i=pha;i<v[pba].size();i++)
  64. 64 {
  65. 65 v[pbb].push_back(v[pba][i]);
  66. 66 }
  67. 67 v[pba].resize(pha);//重设长度会删除后面元素~
  68. 68 }
  69. 69 void show()
  70. 70 {
  71. 71 int i;
  72. 72 for (i=0;i<n;i++)
  73. 73 {
  74. 74 printf("%d:",i);
  75. 75 for (int j=0;j<v[i].size();j++)
  76. 76 {
  77. 77 printf(" %d",v[i][j]);//依据方便实用printf,cout!!!
  78. 78 }
  79. 79 printf("\n");
  80. 80 }
  81. 81 }
  82. 82 int main()
  83. 83 {
  84. 84 while (scanf("%d",&n)!=EOF)
  85. 85 {
  86. 86 int i;
  87. 87 init();
  88. 88 while (cin>>s)
  89. 89 {
  90. 90 if (s=="quit")
  91. 91 {
  92. 92 break;
  93. 93 }
  94. 94 cin>>a>>sa>>b;
  95. 95 int ba,bb,ha,hb;//ba为a的堆,ha为a在堆的高度
  96. 96 if (judge(ba,bb,ha,hb))//多用传参形式,少用全局变量!!!
  97. 97 {
  98. 98 continue;
  99. 99 }
  100. 100 if (s=="move")
  101. 101 {
  102. 102 clear(ba,ha);
  103. 103 }
  104. 104 if (sa=="onto")
  105. 105 {
  106. 106 clear(bb,hb);
  107. 107 }
  108. 108 pile(ba,bb,ha,hb);
  109. 109 }
  110. 110 show();
  111. 111 }
  112. 112
  113. 113 return 0;
  114. 114 }

转载于:https://www.cnblogs.com/hemeiwolong/p/9377058.html

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces1208A&B

    CodeForces1208A 不得不承认,这题猛地一看吓到我了,吓得我直接看了\\(B\\)题,要不是\\(B\\)也吓到我了我就直接做\\(B\\)了. 打打表,找