• Pots(DFS &BFS)


    //新生训练

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. typedef pair<int, int> PII;
    7. const int N = 205;
    8. int n, m;
    9. int l;
    10. int A, B, C;
    11. int dis[N][N];
    12. struct node
    13. {
    14. int px, py, op;
    15. };
    16. node pre[N][N];
    17. void dfs(int x, int y)
    18. {
    19. if (x == 0 && y == 0)
    20. {
    21. return;
    22. }
    23. dfs(pre[x][y].px, pre[x][y].py);
    24. switch (pre[x][y].op)
    25. {
    26. case 1:
    27. cout << "FILL(1)\n";
    28. break;
    29. case 2:
    30. cout << "FILL(2)\n";
    31. break;
    32. case 3:
    33. cout << "DROP(1)\n";
    34. break;
    35. case 4:
    36. cout << "DROP(2)\n";
    37. break;
    38. case 5:
    39. cout << "POUR(1,2)\n";
    40. break;
    41. case 6:
    42. cout << "POUR(2,1)\n";
    43. }
    44. }
    45. bool flag = 1;
    46. void bfs()
    47. {
    48. queue q;
    49. q.push(make_pair(0, 0));
    50. dis[0][0] = 0;
    51. while (!q.empty())
    52. {
    53. PII t = q.front();
    54. q.pop();
    55. int x = t.first, y = t.second;
    56. if (x == C || y == C)
    57. {
    58. cout << dis[x][y] << '\n';
    59. dfs(x, y);
    60. flag = 0;
    61. return;
    62. }
    63. int ix, iy;
    64. // FILL(1)
    65. ix = A, iy = y;
    66. if (dis[ix][iy] == -1)
    67. {
    68. dis[ix][iy] = dis[x][y] + 1;
    69. pre[ix][iy] = {x, y, 1};
    70. q.push(make_pair(ix, iy));
    71. }
    72. // FILL(2)
    73. ix = x, iy = B;
    74. if (dis[ix][iy] == -1)
    75. {
    76. dis[ix][iy] = dis[x][y] + 1;
    77. pre[ix][iy] = {x, y, 2};
    78. q.push(make_pair(ix, iy));
    79. }
    80. // DROP(1)
    81. ix = 0, iy = y;
    82. if (dis[ix][iy] == -1)
    83. {
    84. dis[ix][iy] = dis[x][y] + 1;
    85. pre[ix][iy] = {x, y, 3};
    86. q.push(make_pair(ix, iy));
    87. }
    88. // DROP(2)
    89. ix = x, iy = 0;
    90. if (dis[ix][iy] == -1)
    91. {
    92. dis[ix][iy] = dis[x][y] + 1;
    93. pre[ix][iy] = {x, y, 4};
    94. q.push(make_pair(ix, iy));
    95. }
    96. // POUR(1,2)
    97. ix = x, iy = y;
    98. while (ix && iy < B)
    99. {
    100. --ix;
    101. ++iy;
    102. }
    103. if (dis[ix][iy] == -1)
    104. {
    105. dis[ix][iy] = dis[x][y] + 1;
    106. pre[ix][iy] = {x, y, 5};
    107. q.push(make_pair(ix, iy));
    108. }
    109. // POUR(2,1)
    110. ix = x, iy = y;
    111. while (iy && ix < A)
    112. {
    113. ++ix;
    114. --iy;
    115. }
    116. if (dis[ix][iy] == -1)
    117. {
    118. dis[ix][iy] = dis[x][y] + 1;
    119. pre[ix][iy] = {x, y, 6};
    120. q.push(make_pair(ix, iy));
    121. }
    122. }
    123. }
    124. void solve()
    125. {
    126. memset(dis, -1, sizeof dis);
    127. cin >> A >> B >> C;
    128. bfs();
    129. if (flag)
    130. cout << "impossible";
    131. }
    132. signed main()
    133. {
    134. int t = 1;
    135. // cin>>t;
    136. while (t--)
    137. {
    138. solve();
    139. }
    140. return 0;
    141. }

    //感谢学长的讲解 hwh ;

    ~~~//仅当笔者个人备忘录使用。

  • 相关阅读:
    万字详述 Web3 彻底颠覆品牌行业的底层叙事
    贼好用的六款 Linux 远程连接工具介绍
    Dubbo(二)Dubbo和ZooKeeper的协同工作原理
    c语言:查漏补缺(二)
    Jmeter接口自动化(八)函数 上
    Echarts柱状图格式化Label加单位
    【ARM Coresight 系列文章 9.1 -- ITM 仪器化跟踪宏单元详细介绍】
    CSS的三大特性
    使用readelf和objdump查看ELF常见段
    只会postman单接口测试?这些高级功能你必须掌握
  • 原文地址:https://blog.csdn.net/Sky_miracle/article/details/137352439