1. 程式人生 > >poj 3414 路徑輸出的bfs(pre)

poj 3414 路徑輸出的bfs(pre)

  1. #include <iostream>
  2. #include <algorithm>
  3. usingnamespace std;  
  4. #include <cstring>
  5. #include <queue>
  6. #include <stack>
  7. struct cup  
  8. {  
  9.     int x, y;  
  10.     int step;  
  11.     int flag;//標記操作
  12.     cup *pre;//記錄路徑
  13. };  
  14. queue<cup>Q;  
  15. stack<int>R;  
  16. int a, b, e;  
  17. int vis[117][117]={0};
    //標記當前狀態是否到達過
  18. int ans;  
  19. void BFS(int x, int y)  
  20. {  
  21.     cup c;  
  22.     cup t[317];//目前瓶子裡剩餘的水量
  23.     c.x = 0, c.y = 0;  
  24.     c.flag = 0;  
  25.     c.pre = NULL;  
  26.     c.step = 0;  
  27.     Q.push(c);  
  28.     vis[x][y] = 1;  
  29.     int count = -1;  
  30.     while(!Q.empty())  
  31.     {  
  32.         count++;  
  33.         t[count] = Q.front();  
  34.         Q.pop();  
  35.         for(int i = 1; i <= 6; i++)  
  36.         {  
  37.             switch(i)  
  38.             {  
  39.                 case 1:                     //fill a
  40.                     c.x = a;  
  41.                     c.y = t[count].y;  
  42.                     c.flag = 1;  
  43.                     break;  
  44.                 case
     2:                     //fill b
  45.                     c.x = t[count].x;  
  46.                     c.y = b;  
  47.                     c.flag = 2;  
  48.                     break;  
  49.                 case 3:                     //drop a
  50.                     c.x = 0;  
  51.                     c.y = t[count].y;  
  52.                     c.flag = 3;  
  53.                     break;  
  54.                 case 4:                     //drop b
  55.                     c.x = t[count].x;  
  56.                     c.y = 0;  
  57.                     c.flag = 4;  
  58.                     break;  
  59.                 case 5:                     //pour a to b
  60.                     if(t[count].x > b-t[count].y)  
  61.                     {  
  62.                         c.x = t[count].x-(b-t[count].y);  
  63.                         c.y = b;  
  64.                     }  
  65.                     else
  66.                     {  
  67.                         c.x = 0;  
  68.                         c.y = t[count].y+t[count].x;  
  69.                     }  
  70.                     c.flag = 5;  
  71.                     break;  
  72.                 case 6:                     //pour b to a
  73.                     if(t[count].y > a-t[count].x)  
  74.                     {  
  75.                         c.y = t[count].y - (a-t[count].x);  
  76.                         c.x = a;  
  77.                     }  
  78.                     else
  79.                     {  
  80.                         c.x = t[count].x+t[count].y;  
  81.                         c.y = 0;  
  82.                     }  
  83.                     c.flag = 6;  
  84.                     break;  
  85.             }  
  86.             if(vis[c.x][c.y])  
  87.                 continue;  
  88.             vis[c.x][c.y] = 1;  
  89.             c.step = t[count].step+1;  
  90.             c.pre = &t[count];  
  91.             if(c.x == e || c.y == e)  
  92.             {  
  93.                 ans = c.step;  
  94.                 while(c.pre)  
  95.                 {  
  96.                     R.push(c.flag);  
  97.                     c = *c.pre;  
  98.                 }  
  99.                 return;  
  100.             }  
  101.             Q.push(c);  
  102.         }  
  103.     }  
  104. }  
  105. void print()  
  106. {  
  107.     while(!R.empty())  
  108.     {  
  109.         int i = R.top();  
  110.         R.pop();  
  111.         switch(i)  
  112.         {  
  113.             case 1:cout<<"FILL(1)"<<endl;break;  
  114.             case 2:cout<<"FILL(2)"<<endl;break;  
  115.             case 3:cout<<"DROP(1)"<<endl;break;  
  116.             case 4:cout<<"DROP(2)"<<endl;break;  
  117.             case 5:cout<<"POUR(1,2)"<<endl;break;  
  118.             case 6:cout<<"POUR(2,1)"<<endl;break;  
  119.         }  
  120.     }  
  121. }  
  122. int main()  
  123. {  
  124.     cin >>a>>b>>e;  
  125.     BFS(0,0);  
  126.     if(ans == 0)  
  127.         cout<<"impossible"<<endl;  
  128.     else
  129.     {  
  130.         cout<<ans<<endl;  
  131.         print();  
  132.     }  
  133.     return 0;  
  134. }