动态规划(3)Pots (BFS)

来源:互联网 发布:怎么在ubuntu上安装qq 编辑:程序博客网 时间:2024/06/06 06:45
Pots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7809 Accepted: 3277 Special Judge

Description

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

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

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

Input

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

Output

The first line of the output must contain the length of the sequence of operationsK. The followingK 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

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

Source

Northeastern Europe 2002, Western Subregion
 
 
今天的DP已经上升到这个高度了。。。一道很有趣的倒水游戏,用BFS做就变得简单了,麻烦的是记录最后的路径,问了学长半天,才弄懂。。。
我采用的方法是用字符数组来记录每一步的操作,比如用1~6表示6种操作,那么数组“263626”就表示 FILL(2);POUR(2,1);DROP(1);POUR(2,1);FILL(2);POUR(2,1)。
就这样,。。代码如下:
#include<stdio.h>#include<queue>#include<stdlib.h>#include<string.h>#define MAX 1000using namespace std;int goal,m,n,flag=1;//goal  目标;m,n两个瓶子的容量;flag 标记能否达到目标。int p = 0;  //用于记录路径的下标struct State        //状态{int x,y;char op[MAX];   // 记录路径int step_counter;  //记录步数  }st;bool visit[MAX][MAX];  //记录是否已访问bool check_state(State s){if(!visit[s.x][s.y])return 1;else return 0;};State full1 (State a){a.x=m;a.op[p] = '1';return a;};State full2 (State a ){a.y=n;a.op[p] = '2';return a;}State drop1(State a){a.x=0;a.op[p] = '3';return a;};State drop2(State a){a.y=0;a.op[p] = '4';return a;};State pour1(State a){if(a.x>n-a.y){    a.x = a.x-n+a.y;a.y = n;}else{ a.y += a.x; a.x = 0;    }a.op[p] = '5';return a;};State pour2(State a){if(a.y>m-a.x){    a.y = a.y+a.x-m;a.x = m;}else{ a.x += a.y; a.y = 0;    }a.op[p] = '6';return a;};void bfs(State start){queue <State> q;State now,next;q.push(start);start.step_counter = 0;visit[start.x][start.y] = 1;while(!q.empty()){now = q.front();p=now.step_counter;if(now.x == goal || now.y == goal)//符合条件{printf("%d\n",now.step_counter);flag = 0;for(int k=0;k<now.step_counter;k++){                switch(now.op[k]-'0')// 输出路径                {                         case(1): printf("FILL(1)\n");break;                 case(2): printf("FILL(2)\n");break;                 case(3): printf("DROP(1)\n");break;                 case(4): printf("DROP(2)\n");break;                 case(5): printf("POUR(1,2)\n");break;                 case(6): printf("POUR(2,1)\n");break;        }}return;}for(int i=1;i<=6;i++){switch(i){case(1):next = full1(now);break;//六种操作case(2):next = full2(now);break;case(3):next = drop1(now);break;case(4):next = drop2(now);break;case(5):next = pour1(now);break;case(6):next = pour2(now);break;}if(check_state(next)){next.step_counter = now.step_counter + 1;q.push(next);visit[next.x][next.y]= 1;}}q.pop();}}int main(){scanf("%d %d %d",&m,&n,&goal);st.step_counter = st.x = st.y = 0;for(int h=0;h<MAX;h++)st.op[h] = '\0';//初始化bfs(st);if(flag) printf("impossible\n");system("pause");return 0;}

原创粉丝点击