非常可乐 HDU - 1495(bfs+三维标记)

题目链接:https://cn.vjudge.net/problem/HDU-1495

题意:有一瓶S升的可乐,给容量分别为A,B的两个杯子倒可乐,它们三个之间可以互相倒可乐。问这个三个容器之间的两个容器能不能平分S升的可乐。

思路:如果S是奇数的话,一定不能平分的.然后有六种倒法嘛,S-A,A-S,S-B,B-S,A-B,B-A。因为不清楚刻度,所以只有两种情况,1.一个容器里的可乐全倒进另一个容器 2.一个容器将另一个容器倒满并且还剩可乐。将三个容器出现过的情况用一个三维数组进行标记。

注意:一开始标记的是vis[s][0][0]

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <queue>
  5 #include <stack>
  6 #include <algorithm>
  7 #include <cmath>
  8 #include <map>
  9 #define mem(a,b) memset(a,b,sizeof(a));
 10 using namespace std;
 11 #define INF 0x3f3f3f3f
 12 typedef long long ll;
 13 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
 14 const int maxn = 5000005;
 15 int s,n,m,vis[105][105][105],ans;
 16 bool flag;
 17 struct node {
 18     int x,y,z,w;
 19     node(int x1,int y1,int z1,int w1):x(x1),y(y1),z(z1),w(w1){};
 20 };
 21 void BFS(int x1,int y1,int z1) {
 22     queue<node>q;
 23     q.push(node(x1,y1,z1,0));
 24     vis[x1][y1][z1] = 1;
 25     int av = x1 / 2;
 26     while(!q.empty()) {
 27         node temp= q.front();
 28         q.pop();
 29         if(temp.x == av&&temp.y == av) {
 30             flag = 1;
 31             ans  = temp.w;
 32             break;
 33         }
 34         else if(temp.x == av&&temp.z == av) {
 35             flag = 1;
 36             ans  = temp.w;
 37             break;
 38         }
 39         else if(temp.z == av&&temp.y == av) {
 40             flag = 1;
 41             ans  = temp.w;
 42             break;
 43         }
 44         int fx,fy,fz;
 45         //s-a
 46             if(temp.x > (n - temp.y)) {
 47                 fy = n;
 48                 fx = temp.x+temp.y-n;
 49                 fz = temp.z;
 50             }
 51             else {
 52                 fy = temp.x + temp.y;
 53                 fx = 0;
 54                 fz = temp.z;
 55             }
 56             if(!vis[fx][fy][fz]) {
 57                 vis[fx][fy][fz] = 1;
 58                 q.push(node(fx,fy,fz,temp.w+1));
 59             }
 60         //a-s
 61             fy = 0;
 62             fz = temp.z;
 63             fx = temp.x + temp.y;
 64             if(!vis[fx][fy][fz]) {
 65                 vis[fx][fy][fz] = 1;
 66                 q.push(node(fx,fy,fz,temp.w+1));
 67             }
 68         //s-b
 69             if(temp.x > (m - temp.z)) {
 70                 fy = temp.y;
 71                 fx = temp.x+temp.z-m;
 72                 fz = m;
 73             }
 74             else {
 75                 fy = temp.y;
 76                 fx = 0;
 77                 fz = temp.z + temp.x;
 78             }
 79             if(!vis[fx][fy][fz]) {
 80                 vis[fx][fy][fz] = 1;
 81                 q.push(node(fx,fy,fz,temp.w+1));
 82             }
 83          //b-s
 84             fx = temp.x + temp.z;
 85             fy = temp.y;
 86             fz = 0;
 87             if(!vis[fx][fy][fz]) {
 88                 vis[fx][fy][fz] = 1;
 89                 q.push(node(fx,fy,fz,temp.w+1));
 90             }
 91          //a-b
 92             if(temp.y > (m- temp.z)) {
 93                 fz = m;
 94                 fy = temp.y + temp.z - m;
 95                 fx = temp.x;
 96              }
 97              else {
 98                 fy = 0;
 99                 fz = temp.y + temp.z;
100                 fx = temp.x;
101              }
102              if(!vis[fx][fy][fz]) {
103                 vis[fx][fy][fz] = 1;
104                 q.push(node(fx,fy,fz,temp.w+1));
105             }
106          //b-a
107             if(temp.z > (n - temp.y)) {
108                 fy = n;
109                 fx = temp.x;
110                 fz = temp.z + temp.y - n;
111             }
112             else {
113                 fz = 0;
114                 fy = temp.z + temp.y;
115                 fx = temp.x;
116             }
117             if(!vis[fx][fy][fz]) {
118                 vis[fx][fy][fz] = 1;
119                 q.push(node(fx,fy,fz,temp.w+1));
120             }
121 
122     }
123 }
124 int main()
125 {
126     while(cin >> s >> n >> m && s && m && n) {
127          mem(vis,0);
128          flag = 0;
129          if(s % 2 == 1) {
130             cout << "NO" << endl;
131             continue;
132          }
133          BFS(s,0,0);
134          if(flag) {
135             cout << ans << endl;
136          }
137          else {
138             cout << "NO" << endl;
139          }
140     }
141     return 0;
142 }

 

posted on 2019-08-20 09:11  一只小毛球  阅读(597)  评论(1编辑  收藏  举报

导航