UVa101 - The Blocks Problem

举报
小哈里 发表于 2022/05/10 23:51:36 2022/05/10
【摘要】 //UVa101 - The Blocks Problem#include<iostream>#include<cstdio>#include<string>#include<vector>using namespace std; const int maxn = 30;int n;vec...

  
  1. //UVa101 - The Blocks Problem
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<string>
  5. #include<vector>
  6. using namespace std;
  7. const int maxn = 30;
  8. int n;
  9. vector<int>pile[maxn];
  10. //找木块a所在的pile和height,以引用的形式返回调用者
  11. void find(int a, int &p, int &h){
  12. for(p = 0; p < n; p++)
  13. for(h = 0; h < pile[p].size(); h++)
  14. if(pile[p][h] == a) return;
  15. }
  16. //把p堆高度为h的木块上方的所有木块移回原位
  17. void back(int p, int h){
  18. for(int i = h+1; i < pile[p].size(); i++){
  19. int b = pile[p][i];
  20. pile[b].push_back(b);
  21. }
  22. pile[p].resize(h+1);
  23. }
  24. //把p堆高度为h及其上方的木块整体移动到p2的顶部
  25. void doing(int p, int h, int p2){
  26. for(int i = h; i< pile[p].size(); i++)
  27. pile[p2].push_back(pile[p][i]);
  28. pile[p].resize(h);
  29. }
  30. int main(){
  31. int a, b;
  32. cin >> n;
  33. string s1, s2;
  34. for(int i = 0; i < n; i++) pile[i].push_back(i);
  35. while(cin >> s1 && s1 != "quit"){
  36. cin >> a >> s2 >> b;
  37. int pa, pb, ha, hb;
  38. find(a, pa, ha);
  39. find(b, pb, hb);
  40. if(pa == pb) continue;
  41. if(s2 == "onto") back(pb, hb);
  42. if(s1 == "move") back(pa, ha);
  43. doing(pa, ha, pb);
  44. }
  45. for(int i = 0; i < n; i++){
  46. printf("%d:",i);
  47. for(int j = 0; j < pile[i].size(); j++)
  48. printf(" %d",pile[i][j]);
  49. printf("\n");
  50. }
  51. return 0;
  52. }
  53. /*
  54. 10
  55. move 9 onto 1
  56. move 8 over 1
  57. move 7 over 1
  58. move 6 over 1
  59. pile 8 over 6
  60. pile 8 over 5
  61. move 2 over 1
  62. move 4 over 9
  63. quit
  64. 0: 0
  65. 1: 1 9 2 4
  66. 2:
  67. 3: 3
  68. 4:
  69. 5: 5 8 7 6
  70. 6:
  71. 7:
  72. 8:
  73. 9:
  74. */

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/55693958

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。