设为首页 加入收藏

TOP

POJ 3414 Pots (经典bfs )
2015-07-20 17:59:03 来源: 作者: 【 】 浏览:1
Tags:POJ 3414 Pots 经典 bfs

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;DROP(i) empty the pot i to the drain;POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

Source

Northeastern Europe 2002, Western Subregion
两个壶,可以清空,可以加满,可以相互倒水,不过每次到要么这个壶水倒完,要么那个壶满了,要求一个壶有一定的水结束,输出步骤
bfs具体在代码中

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        using namespace std; #define N 105 struct stud{ int a,b; int time; string mv; }; int a,b,c; int vis[N][N]; int judge(int x,int y) { if(x==c||y==c) return 1; return 0; } stud bfs() { struct stud next,cur; queue
       
        q; while(!q.empty()) q.pop(); cur.a=cur.b=0; cur.mv=""; cur.time=0; if(cur.a==c||cur.b==c) return cur; q.push(cur); memset(vis,0,sizeof(vis)); vis[0][0]=1; while(!q.empty()) { cur=q.front(); q.pop(); if(cur.a
        
         0&&!vis[0][cur.b]) //倒出a { next.a=0; next.b=cur.b; next.mv=cur.mv; next.mv+="D1"; next.time=cur.time+1; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } if(cur.b>0&&!vis[cur.a][0]) //倒出b { next.b=0; next.a=cur.a; next.mv=cur.mv; next.mv+="D2"; next.time=cur.time+1; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } //a给b倒水 if(cur.b
         
          0&&cur.a+cur.b<=b&&!vis[0][cur.a+cur.b]) { next.a=0; next.b=cur.a+cur.b; next.time=cur.time+1; next.mv=cur.mv+"P12"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } else if(cur.b
          
           0&&cur.a+cur.b>b&&!vis[cur.a+cur.b-b][b]) { next.a=cur.a+cur.b-b; next.b=b; next.time=cur.time+1; next.mv=cur.mv+"P12"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } //b给a倒水 if(cur.b>0&&cur.a!=a&&cur.a+cur.b<=a&&!vis[cur.a+cur.b][0]) { next.a=cur.a+cur.b; next.b=0; next.time=cur.time+1; next.mv=cur.mv+"P21"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } else if(cur.b>0&&cur.a!=a&&cur.a+cur.b>a&&!vis[a][cur.a+cur.b-a]) { next.a=a; next.b=cur.a+cur.b-a; next.time=cur.time+1; next.mv=cur.mv+"P21"; vis[next.a][next.b]=1; if(judge(next.a,next.b)) return next; q.push(next); } } struct stud ans; ans.a=-1; return ans; } int main() { int i; scanf("%d%d%d",&a,&b,&c); struct stud cur; cur=bfs(); if(cur.a==-1) { printf("impossible\n"); return 0; } printf("%d\n",cur.time); int len=cur.mv.size(); for(i=0;i
           
            


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1711 Number Sequence KMP题解 下一篇HDU 4920 Matrix multiplication

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: