HDU1495 BFS

题意:三个瓶子,容量为s,n,m,且s装满饮料 s=m+n

求至少倒多少下能使得某两个瓶子装着相同多的饮料。

bfs 模拟

View Code
  1 #include<stdio.h>

  2 #include<string.h>

  3 #include<math.h>

  4 #include<queue>

  5 #include<algorithm>

  6 using namespace std;

  7 const int maxn = 105;

  8 struct node{

  9     int s,n,m,t;

 10 };

 11 int s,n,m;

 12 int vis[ maxn ][ maxn ][ maxn ];

 13 

 14 int bfs(){

 15     node now,next;

 16     queue<node>q;

 17     while( !q.empty() )

 18         q.pop();

 19     now.s=s,now.n=now.m=now.t=0;

 20     memset( vis,0,sizeof( vis ));

 21     vis[s][0][0]=1;//vis[ now ]=1;

 22     q.push( now );

 23     while( !q.empty() ){

 24         now=q.front(),q.pop();

 25         if( ( now.s==now.n&&now.m==0 )||( now.s==now.m&&now.n==0 )||( now.m==now.n&&now.s==0 ) )

 26             return now.t;

 27         int tmp;

 28         if( now.s!=0 ){

 29             if( now.n!=n ){

 30                 tmp=min( now.s,n-now.n );

 31                 next=now;

 32                 next.t=now.t+1;

 33                 next.s-=tmp;

 34                 next.n+=tmp;

 35                 if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 36                     vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 37                     q.push( next );

 38                 }

 39             }

 40             if( now.m!=m ){

 41                 tmp=min( now.s,m-now.m );

 42                 next=now;

 43                 next.t=now.t+1;

 44                 next.s-=tmp;

 45                 next.m+=tmp;

 46                 if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 47                     vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 48                     q.push( next );

 49                 }

 50             }

 51         }// 1 to (2 or 3)

 52         if( now.n!=0 ){

 53             tmp=now.n;

 54             next=now;

 55             next.t=now.t+1;

 56             next.s+=tmp;

 57             next.n=0;

 58             if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 59                 vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 60                 q.push( next );

 61             }

 62             if( now.m!=m ){

 63                 tmp=min( m-now.m,now.n );

 64                 next=now;

 65                 next.t=now.t+1;

 66                 next.m+=tmp;

 67                 next.n-=tmp;

 68                 if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 69                     vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 70                     q.push( next );

 71                 }

 72             }

 73         }//2 to( 1 or 3 )

 74         if( now.m!=0 ){

 75             tmp=now.m;

 76             next=now;

 77             next.t=now.t+1;

 78             next.m=0;

 79             next.s+=tmp;

 80             if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 81                 vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 82                 q.push( next );

 83             }

 84             if( now.n!=n ){

 85                 tmp=min( n-now.n,now.m );

 86                 next=now;

 87                 next.t=now.t+1;

 88                 next.n+=tmp;

 89                 next.m-=tmp;

 90                 if( vis[next.s][next.n][next.m]==0/*vis[ next ]==0*/ ){

 91                     vis[next.s][next.n][next.m]=1;//vis[ next ]=1;

 92                     q.push( next );

 93                 }

 94             }

 95         }

 96     }

 97     return -1;

 98 }

 99 

100 int main(){

101     while( scanf("%d%d%d",&s,&n,&m),s+n+m ){

102         if( s%2==1 ){

103             printf("NO\n");

104             continue;

105         }

106         if( n==m ){

107             printf("1\n");

108             continue;

109         }

110         int ans=bfs();

111         if( ans!=-1 )

112             printf("%d\n",ans);

113         else

114             printf("NO\n");

115     }

116     return 0;

117 }

 

你可能感兴趣的:(HDU)