zoukankan      html  css  js  c++  java
  • POJ-3414.Pots.(BFS + 路径打印)

      这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome,创客晚上好吵呀,隔壁真的服...

      本题大意:给定两个杯子的容量,有六种操作,通过操作使得两个被子其中一个杯子的水等于待输入值C,并输出最少操作次数和操作名称。

      本题思路:BFS,遇到合适的直接输出,搜完都没有搜到就impossible。

      参考代码:

       将switch改为if能节省大部分篇幅,但是switch更美观明了一点。

     1 #include <string>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <queue>
     5 #include <stack>
     6 using namespace std;
     7 
     8 typedef pair<int ,int > P;
     9 const int maxn = 100 + 5, INF = 0x3f3f3f3f;
    10 int A, B, C, t;
    11 
    12 struct {
    13     int x, y, cnt;//用来存他爹的坐标和倒水的方式
    14 } Ans[maxn][maxn];
    15 int vis[maxn][maxn];
    16 string ans[6] = {
    17     "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)",
    18 };
    19 
    20 void bfs() {
    21     queue <P> Q;
    22     vis[0][0] = 0;
    23     Ans[0][0].x = -1, Ans[0][0].y = -1;
    24     Q.push(make_pair(0, 0));
    25     while(!Q.empty()) {
    26         P now = Q.front(), Next;
    27         stack <string> s1;
    28         Q.pop();
    29         if(now.first == C || now.second == C) {
    30             cout << vis[now.first][now.second] << endl;
    31             int X = now.first;
    32             int Y = now.second;
    33             while(X != -1) {
    34                 s1.push(ans[Ans[X][Y].cnt]);
    35                 int tmp = X;
    36                 X = Ans[X][Y].x;
    37                 Y = Ans[tmp][Y].y;
    38             }
    39             s1.pop();
    40             while(!s1.empty()) {
    41                 cout << s1.top() << endl;
    42                 s1.pop();
    43             }
    44             return;
    45         }
    46         for(int i = 0; i < 6; i ++) {
    47             switch(i) {
    48                 case 0 ://将A加满
    49                    Next.first = A;
    50                    Next.second = now.second;
    51                    break;
    52                 case 1 ://将B加满
    53                     Next.second = B;
    54                     Next.first = now.first;
    55                     break;
    56                 case 2 ://将A倒掉
    57                     Next.second = now.second;
    58                     Next.first = 0;
    59                     break;
    60                 case 3 ://将B倒掉
    61                     Next.first = now.first;
    62                     Next.second = 0;
    63                     break;
    64                 case 4 ://将A倒入B中
    65                     t = B - now.second;//B中还能倒入的量
    66                     Next.first = now.first - t;
    67                     if(Next.first < 0) Next.first = 0;
    68                     Next.second = now.first + now.second;
    69                     if(Next.second > B) Next.second = B;
    70                     break;
    71                 case 5 ://将B倒入A中
    72                     t = A - now.first;//A中还能倒入的量
    73                     Next.second = now.second  - t;
    74                     if(Next.second < 0) Next.second = 0;
    75                     Next.first = now.first + now.second;
    76                     if(Next.first > A) Next.first = A;
    77                     break;
    78             }
    79             if(vis[Next.first][Next.second] == INF) {
    80                 vis[Next.first][Next.second] = vis[now.first][now.second] + 1;
    81                 Q.push(Next);
    82                 Ans[Next.first][Next.second].x = now.first;
    83                 Ans[Next.first][Next.second].y = now.second;
    84                 Ans[Next.first][Next.second].cnt = i;
    85             }
    86         }
    87     }
    88     cout << "impossible" << endl;
    89 }
    90 
    91 int main () {
    92     memset(vis, INF, sizeof(vis));
    93     cin >> A >> B >> C;
    94     bfs();
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    学习进度
    作业8:单元测试练习
    用户体验设计案例分析
    团队协作一
    需求分析
    结对编程——词频统计 2
    结对编程——词频统计
    个人项目-词频统计
    数组求和
    个人介绍和Github使用流程
  • 原文地址:https://www.cnblogs.com/bianjunting/p/10492314.html
Copyright © 2011-2022 走看看