1017 非常可乐

1017 非常可乐

题意:有体积为s的一瓶可乐,和体积为m,n的两个杯子,他们都没有刻度,求将可乐平分最少倒多少次。

思路:三个杯子倒可乐,共有六种情况,s->n,s->m,m->s,m->n,n->s,n->m,判断每种可能的情况,添加到队列中,逐一搜索。

感想:直接对所有可能的情况搜索,思路并不复杂。

#include<iostream>

#include<algorithm>

#include<queue>

#include<math.h>

#include<string.h>

using namespace std;

#define maxn 101

bool visit[maxn][maxn];

int m,n,s,si,sj;

struct node

{

   int x,y,all,t;  //x,y,all分别表示m,n,s杯中可乐的体积,t表示倒了多少次

};

void BFS()

{

   queue<node> que;

   memset(visit,false,sizeof(visit));

   node p,q;

   p.x = 0,p.y = 0,p.t = 0,p.all = s;

   que.push(p);

   visit[p.x][p.y] = true;

   while(!que.empty())

    {

       p = que.front();

       que.pop();

       if(p.y == p.all && p.y == s/2)

       {

           printf("%d\n",p.t);

           return;

       }

       if(p.all+p.x>m)    //s倒m

       {

           q.x = m,q.y= p.y,q.all =p.all+p.x-m,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       else

       {

           q.x = p.all+p.x,q.y = p.y,q.all = 0,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       if(p.all+p.y>n)          //s倒n

       {

           q.x= p.x, q.y = n, q.all = p.all+p.y-n,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       else

       {

           q.x = p.x,q.y = p.all+p.y,q.all = 0,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       if(p.x+p.y>n)    //m倒n

       {

           q.x = p.x+p.y-n,q.y = n,q.all = p.all,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       else

       {

           q.x = 0,q.y = p.x+p.y,q.all = p.all,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       if(p.x+p.all>s)     //m倒s

{

            q.all =s,q.x = p.x+p.all-s,q.y = p.y,q.t = p.t+1;

           if(!visit[q.x][q.y])

           que.push(q),visit[q.x][q.y] = true;

      }

      else

{

       q.all =p.all+p.x,q.x = 0,q.y = p.y,q.t = p.t+1;

       if(!visit[q.x][q.y])

           que.push(q),visit[q.x][q.y] = true;

      }

       if(p.x+p.y > m)      //n倒m

       {

       q.y=p.y+p.x-m,q.x=m,q.all=p.all,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       else

       {

           q.x = p.x+p.y,q.y = 0,q.all = p.all,q.t = p.t+1;

           if(!visit[q.x][q.y])

                que.push(q),visit[q.x][q.y] =true;

       }

       if(p.all+p.y>s)     //n倒s

       {

           q.all = s,q.x = p.x,q.y = p.all+p.y-s,q.t = p.t+1;

           if(!visit[q.x][q.y])

           que.push(q),visit[q.x][q.y] = true;

       }

       else

       {

       q.all = p.all+p.y,q.x = p.x,q.y = 0,q.t = p.t+1; //n倒s

       if(!visit[q.x][q.y])

           que.push(q),visit[q.x][q.y] = true;

       }

 

    }

   printf("NO\n");

}

int main()

{

   while(scanf("%d%d%d",&s,&m,&n) &&(s||m||n))

    {

       if(s%2)

       {

           printf("NO\n");

           continue;

       }

        if(m > n) swap(m,n);

       BFS();

    }

   return 0;

}

你可能感兴趣的:(1017 非常可乐)