Pots
?POJ - 3414?
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?jis 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)
題意:給你三個數字A,B,C,A,B代表容器的體積,C代表需要得到的水的體積,每次可以進行六種操作:將A容器填滿;將B容器填滿:將A容器的水倒入B中;將B容器的水倒入A中;倒掉A中的水;倒掉B中的水。問最少需要多少步才能夠使A,B中某容器的水恰好為C升,并將過程輸出。
方法:通過廣搜每次遍歷六種情況,記錄其前一個步驟,再通過深搜輸出,博主代碼過于冗長,望見諒!!!
AC代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e6 + 5;
const ll mod = 1e9+7;
int a,b,a1,b1,c;
struct node
{int x,y;int step;int pre;int id;int way;
};
int ids;
int tpl;
bool vis[1000][1000];
int tmp[maxn];
int tmp1[maxn];
int bfs()
{node no;no.x=0;no.y=0;vis[0][0]=true;no.pre=-1;no.step=0;no.way=-1;no.id=0;tmp[no.id]=no.pre;tmp[no.id]=no.way;ids=0;queue<node> q;while(!q.empty())q.pop();q.push(no);while(!q.empty()) {node front=q.front();q.pop();for(int i=1;i<=6;i++){node tail;switch(i) {case 1:{if(front.x<a&&vis[a][front.y]==false){tail.x=a;tail.y=front.y;tail.step=front.step+1;ids++;tail.id=ids;tail.pre=front.id;tail.way=1;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;vis[a][front.y]=true;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}break;}case 2:{if(front.y<b&&vis[front.x][b]==false){tail.y=b;tail.x=front.x;ids++;tail.id=ids;tail.step=front.step+1;tail.pre=front.id;tail.way=2;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;vis[front.x][b]=true;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}break;}case 3:{if(front.x>0&&front.y<b){int k1=front.x-(b-front.y),k2;if(k1<0){k1=0;k2=front.y+front.x;}else{k1=front.x-(b-front.y);k2=b;}if(vis[k1][k2]==false){vis[k1][k2]=true;tail.x=k1;tail.y=k2;ids++;tail.id=ids;tail.step=front.step+1;tail.pre=front.id;tail.way=3;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}}break;}case 4:{if(front.y>0&&front.x<a){int k1=front.y-(a-front.x),k2;if(k1<0){k1=0;k2=front.x+front.y;}else{k1=front.y-(a-front.x);k2=a;}if(vis[k2][k1]==false){vis[k2][k1]=true;tail.y=k1;tail.x=k2;ids++;tail.id=ids;tail.step=front.step+1;tail.pre=front.id;tail.way=4;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}}break;}case 5:{if(front.x>0&&vis[0][front.y]==false){vis[0][front.y]=true;tail.x=0;tail.y=front.y;ids++;tail.id=ids;tail.step=front.step+1;tail.pre=front.id;tail.way=5;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}break;}case 6:{if(front.y>0&&vis[front.x][0]==false){vis[front.x][0]=true;tail.x=front.x;tail.y=0;ids++;tail.id=ids;tail.step=front.step+1;tail.pre=front.id;tail.way=6;tmp[tail.id]=tail.pre;tmp1[tail.id]=tail.way;if(tail.x==c||tail.y==c){tpl=tail.id;return tail.step;}q.push(tail);//printf("%d-----%d %d\n",tail.way,tail.x,tail.y);}break;}default :break;}//printf("----------------%d\n",i);}}return -1;
}
void dfs(int x)
{if(tmp[tmp[x]]!=-1)dfs(tmp[x]);switch(tmp1[x]){case 1:{printf("FILL(1)\n");break;}case 2:{printf("FILL(2)\n");break;}case 3:{printf("POUR(1,2)\n");break;}case 4:{printf("POUR(2,1)\n");break;}case 5:{printf("DROP(1)\n");break;}case 6:{printf("DROP(2)\n");break;}default :{break;}}
}int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);scanf("%d %d %d",&a,&b,&c);int ans=bfs();if(ans==-1)printf("impossible\n");else{printf("%d\n",ans);dfs(tpl);}return 0;
}
?