• 101 - The Blocks Problem (UVA)


    题目链接如下:

    Online Judge

    一开始的代码(能AC但是比较复杂,是考虑到还能再精简,但犯懒....以后还是应该自己先动脑想一想再看别人解法):

    1. #include
    2. #include
    3. #include
    4. const int maxx = 25;
    5. // #define debug
    6. int n, a, b;
    7. std::vector<int> vec[maxx];
    8. char op[5], op1[5];
    9. int loc[maxx];
    10. void moveOnto(int u, int v){
    11. int tu = loc[u];
    12. int tv = loc[v];
    13. while(vec[tu].back() != u){
    14. int temp = vec[tu].back();
    15. vec[temp].push_back(temp);
    16. loc[temp] = temp;
    17. vec[tu].pop_back();
    18. }
    19. while(vec[tv].back() != v){
    20. int temp = vec[tv].back();
    21. vec[temp].push_back(temp);
    22. loc[temp] = temp;
    23. vec[tv].pop_back();
    24. }
    25. vec[tv].push_back(u);
    26. loc[u] = tv;
    27. vec[tu].pop_back();
    28. }
    29. void moveOver(int u, int v){
    30. int tu = loc[u];
    31. int tv = loc[v];
    32. while(vec[tu].back() != u){
    33. int temp = vec[tu].back();
    34. vec[temp].push_back(temp);
    35. loc[temp] = temp;
    36. vec[tu].pop_back();
    37. }
    38. vec[tv].push_back(u);
    39. loc[u] = tv;
    40. vec[tu].pop_back();
    41. }
    42. void pileOnto(int u, int v){
    43. int tu = loc[u];
    44. int tv = loc[v];
    45. while(vec[tv].back() != v){
    46. int temp = vec[tv].back();
    47. vec[temp].push_back(temp);
    48. loc[temp] = temp;
    49. vec[tv].pop_back();
    50. }
    51. auto location = find(vec[tu].begin(), vec[tu].end(), u) - vec[tu].begin();
    52. for(int i = location; i < vec[tu].size(); ++i){
    53. vec[tv].push_back(vec[tu][i]);
    54. loc[vec[tu][i]] = tv;
    55. }
    56. while(vec[tu].back() != u){
    57. vec[tu].pop_back();
    58. }
    59. vec[tu].pop_back();
    60. }
    61. void pileOver(int u, int v){
    62. int tu = loc[u];
    63. int tv = loc[v];
    64. auto location = find(vec[tu].begin(), vec[tu].end(), u) - vec[tu].begin();
    65. for(int i = location; i < vec[tu].size(); ++i){
    66. vec[tv].push_back(vec[tu][i]);
    67. loc[vec[tu][i]] = tv;
    68. }
    69. while(vec[tu].back() != u){
    70. vec[tu].pop_back();
    71. }
    72. vec[tu].pop_back();
    73. }
    74. int main(){
    75. #ifdef debug
    76. freopen("0.txt", "r", stdin);
    77. freopen("1.txt", "w", stdout);
    78. #endif
    79. scanf("%d\n", &n);
    80. for(int i = 0; i < n; ++i){
    81. vec[i].push_back(i);
    82. loc[i] = i;
    83. }
    84. while(scanf("%s", op) == 1 && op[0] != 'q'){
    85. scanf("%d %s %d", &a, op1, &b);
    86. if(a == b || loc[a] == loc[b]){
    87. continue;
    88. }
    89. if(op[0] == 'm' && op1[1] == 'n'){
    90. moveOnto(a, b);
    91. } else if(op[0] == 'm' && op1[1] == 'v'){
    92. moveOver(a, b);
    93. } else if(op1[1] == 'n'){
    94. pileOnto(a, b);
    95. } else{
    96. pileOver(a, b);
    97. }
    98. }
    99. for(int i = 0; i < n; ++i){
    100. printf("%d:", i);
    101. for(int j = 0; j < vec[i].size(); ++j){
    102. printf(" %d", vec[i][j]);
    103. }
    104. printf("\n");
    105. }
    106. #ifdef debug
    107. fclose(stdin);
    108. fclose(stdout);
    109. #endif
    110. return 0;
    111. }

    然后看了刘汝佳的代码(不愧是大佬)之后修改的代码如下:

    1. #include
    2. #include
    3. #include
    4. const int maxx = 25;
    5. // #define debug
    6. int n, a, b, ta, tb;
    7. std::vector<int> vec[maxx];
    8. char op[5], op1[5];
    9. int loc[maxx];
    10. int main(){
    11. #ifdef debug
    12. freopen("0.txt", "r", stdin);
    13. freopen("1.txt", "w", stdout);
    14. #endif
    15. scanf("%d\n", &n);
    16. for(int i = 0; i < n; ++i){
    17. vec[i].push_back(i);
    18. loc[i] = i;
    19. }
    20. while(scanf("%s", op) == 1 && op[0] != 'q'){
    21. scanf("%d %s %d", &a, op1, &b);
    22. ta = loc[a];
    23. tb = loc[b];
    24. if(ta == tb){
    25. continue;
    26. }
    27. if(op1[1] == 'n'){
    28. while(vec[tb].back() != b){
    29. int temp = vec[tb].back();
    30. vec[temp].push_back(temp);
    31. loc[temp] = temp;
    32. vec[tb].pop_back();
    33. }
    34. }
    35. if(op[0] == 'm'){
    36. while(vec[ta].back() != a){
    37. int temp = vec[ta].back();
    38. vec[temp].push_back(temp);
    39. loc[temp] = temp;
    40. vec[ta].pop_back();
    41. }
    42. }
    43. auto location = find(vec[ta].begin(), vec[ta].end(), a) - vec[ta].begin();
    44. for(int i = location; i < vec[ta].size(); ++i){
    45. vec[tb].push_back(vec[ta][i]);
    46. loc[vec[ta][i]] = tb;
    47. }
    48. vec[ta].resize(location);
    49. }
    50. for(int i = 0; i < n; ++i){
    51. printf("%d:", i);
    52. for(int j = 0; j < vec[i].size(); ++j){
    53. printf(" %d", vec[i][j]);
    54. }
    55. printf("\n");
    56. }
    57. #ifdef debug
    58. fclose(stdin);
    59. fclose(stdout);
    60. #endif
    61. return 0;
    62. }

  • 相关阅读:
    Linux常用命令
    大厂秋招真题【二分查找】小红书20230726秋招提前批T2-精华帖子
    React进阶之路(一)-- JSX基础、组件基础
    开源风雷CFD软件多物理场耦合接口开发路线分享!!!
    MFC子类控件化
    算法训练(leetcode)第四十六天 | 110. 字符串接龙、105. 有向图的完全可达性、106. 岛屿的周长
    连接器信号完整性仿真教程 八
    Python中输入输出函数input和print用法
    大数据开发语言Scala入门
    React props属性使用及子传父组件使用
  • 原文地址:https://blog.csdn.net/linh2006/article/details/133787570