hdu1495 Very Coke (BFS)

Title: There is a bottle of Coke and two cups, then at first two cups are empty, because there is no scale. (At first, ignoring this condition, the title is treated as a water problem.) So, pouring Coke at a time can only fill one cup or empty one. Then ask you, pour at least a few times so that two of the three cups (with a Coke bottle) are the same and the other one is empty (I always feel ambiguous).

AC Code:

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
struct node
{
    int a,b,c;
    int step;
    node(){}
    node(int a,int b,int c,int s):a(a),b(b),c(c),step(s){}
};
node tmp ;int n,m,k;
bool vis[105][105][105];
void bfs()//There are only three cases, so we can discuss them in different categories.
{
    queue<node> Q;
    Q.push(tmp);
    while(Q.size())
    {
        tmp = Q.front();
        Q.pop();
        int a = tmp.a,b = tmp.b,c = tmp.c;
       // cout<<a<<" "<<b<<" "<<c<<endl;
        int s = tmp.step ;
        if((b == c&&b!=0&&a == 0)||(a == b &&a!=0&&c==0)||(a==c&&a!=0&&b==0)) {
         //cout<<a<<" "<<b<<" "<<c<<endl;
          cout<<s<<endl;
          return ;
        }
        if(a){
            if(a+b<=m){
                if(!vis[0][a+b][c]) vis[0][a+b][c] = 1,Q.push(node(0,a+b,c,s+1));
            }
            else if(!vis[a+b-m][m][c]){
               vis[a+b-m][m][c] = 1, Q.push(node(a+b-m,m,c,s+1));
            }
            if(a+c<=k){
                if(!vis[0][b][a+c]) vis[0][b][a+c] = 1,Q.push(node(0,b,a+c,s+1));
            }
            else if(!vis[a+c-k][b][k]){
               vis[a+c-k][b][k] = 1, Q.push(node(a+c-k,b,k,s+1));
            }
        }
        if(b){
            if(a+b<=n){
                if(!vis[a+b][0][c]) vis[a+b][0][c] = 1,Q.push(node(a+b,0,c,s+1));
            }
            else if(!vis[n][a+b-n][c]) vis[n][a+b-n][c]  = 1,Q.push(node(n,a+b-n,c,s+1));
            if(b+c<=k){
              if(!vis[a][0][b+c]) vis[a][0][b+c] = 1,Q.push(node(a,0,b+c,s+1));
            }
            else if(!vis[a][b+c-k][k]) vis[a][b+c-k][k] = 1,Q.push(node(a,b+c-k,k,s+1));
        }
        if(c)
        {
            if(a+c<=n){
                if(!vis[a+c][b][0]) vis[a+c][b][0] = 1,Q.push(node(a+c,b,0,s+1));
            }
            else if(!vis[n][b][c+a - n]) vis[n][b][c+a - n] = 1,Q.push(node(n,b,a+c-n,s+1));
            if(b+c<=m){
                if(!vis[a][b+c][0]) vis[a][b+c][0] = 1,Q.push(node(a,b+c,0,s+1));
            }
            else if(!vis[a][m][b+c-m]) vis[a][m][b+c-m] = 1,Q.push(node(a,m,b+c-m,s+1));
        }
    }
    puts("NO");
}
void init()
{
    for(int i = 0;i <= 100;++i)
        for(int j = 0;j <= 100;++j)
         for(int k = 0;k <= 100;++k)
           vis[i][j][k] = 0;
}
int main()
{
    while(cin>>n>>m>>k&&n + m + k)
    {
        if(n&1) {//If it's odd, it's impossible to divide it equally.
            puts("NO");
            continue;
        }
        init();
        vis[n][0][0] = 1;
        tmp .a = n,tmp.b = 0,tmp.c = 0;tmp.step = 0;
        bfs();
    }

}

 

Posted by TGM on Tue, 01 Oct 2019 08:38:27 -0700