题目链接http://noi.openjudge.cn/ch0205/2152/
-
描述
-
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.
输入
-
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).
输出
-
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’.
样例输入
-
3 5 4
样例输出
-
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
查看
在BFS的时候,如果题目要求输出路径,就要保存路径的节点,就不能使用stl的queue,需要自己实现。为了最后可以读取路径,需要在状态节点中保存前一个节点的地址。
StaticQueue模板
template<typename T> class StaticQueue{ public: T Array[MAXN]; int Head,Tail; StaticQueue(){ Tail=Head=0; } void Push(T t){ Array[Tail]=t; Tail++; } void Pop(){ Head++; } T Front(){ return Array[Head]; } bool Empty(){ return Head==Tail; } };
ac代码:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<string> #include<vector> #include<string> #define DEBUG(x) cout<<#x<<" = "<<x<<endl using namespace std; const int MAXN=1e4+10; template<typename T> class StaticQueue{ public: T Array[MAXN]; int Head,Tail; StaticQueue(){ Tail=Head=0; } void Push(T t){ Array[Tail]=t; Tail++; } void Pop(){ Head++; } T Front(){ return Array[Head]; } bool Empty(){ return Head==Tail; } }; struct Node{ int a,b; int step; int f; ///通过什么操作达到这个状态 int op;/// 1,2||3,4||5,6 Node(){} Node(int aa,int bb,int s,int ff,int o) { a=aa,b=bb,step=s,f=ff,op=o; } }; int A,B,C; int visited[110][110]; StaticQueue<Node>q; string text[]={"","FILL(1)","FILL(2)", "POUR(1,2)","POUR(2,1)", "DROP(1)","DROP(2)"}; void Print(int pos) { Node r=q.Array[pos]; if(pos==0)return; Print(r.f); cout<<text[r.op]<<endl; } int main() { // freopen("in.txt","r",stdin); scanf("%d%d%d",&A,&B,&C); memset(visited,0,sizeof(visited)); q.Push(Node(0,0,0,0,0)); visited[0][0]=true; bool succ=false; while(!q.Empty()){ Node t=q.Front(); if(t.a==C||t.b==C){ succ=true; cout<<t.step<<endl; Print(q.Head); return 0; } ///fill ///a if(t.a<A&&!visited[A][t.b]){ Node n=Node(A,t.b,t.step+1,q.Head,1); q.Push(n); visited[A][t.b]=true; } ///b if(t.b<B&&!visited[t.a][B]){ Node n=Node(t.a,B,t.step+1,q.Head,2); q.Push(n); visited[t.a][B]=true; } ///pour ///a->b if(t.a>0){ int r=B-t.b; if(t.a<=r){ ///!!!与else连用的if最好是单条件,否则会出现难以发现的bug if(!visited[0][t.a+t.b]) { Node n=Node(0,t.b+t.a,t.step+1,q.Head,3); q.Push(n); visited[0][t.a+t.b]=true; } } else { if(!visited[t.a-r][B]){ Node n=Node(t.a-r,B,t.step+1,q.Head,3); q.Push(n); visited[t.a-r][B]=true; } } } ///b->a if(t.b>0){ int r=A-t.a; if(t.b<=r&&!visited[t.a+t.b][0]){ Node n=Node(t.a+t.b,0,t.step+1,q.Head,4); q.Push(n); visited[t.a+t.b][0]=true; } else { if(!visited[A][t.b-r]){ Node n=Node(A,t.b-r,t.step+1,q.Head,4); q.Push(n); visited[A][t.b-r]=true; } } } ///drop ///a if(t.a>0&&!visited[0][t.b]){ Node n=Node(0,t.b,t.step+1,q.Head,5); q.Push(n); visited[0][t.b]=true; } ///b if(t.b>0&&!visited[t.a][0]){ Node n=Node(t.a,0,t.step+1,q.Head,6); q.Push(n); visited[t.a][0]=true; } q.Pop(); } if(!succ)cout<<"impossible"<<endl; }