本文主要是介绍POJ 3414 Pots(模拟倒水),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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)
题意:给出两个水杯的容量A B,问怎么操作可以使A B中一个的水量到C,题目保证AB中有一个大于C
其实也就是一个简单的BFS,因为把顺序写错了,导致自己看了两天都没知道到错误,真的是脑残
倒水也就有六种情况,分别让他们入队就行了。详解见代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int maxcup[2],endcup;//被子的容量和最终状态
int vis[110][110];//标记,vis[i][j]就是杯子1的水是i,杯子2的水是j
struct cupwater
{int curcup[2],step;//当前水量,步数int op[10000];//记录上一个的节点
};
queue<cupwater>Q;
cupwater p,q;
void Fillwater(int i)//往杯子里面倒水
{q=p;if(q.curcup[i]<maxcup[i])//如果杯子里面的水是满的就不用倒水了{q.curcup[i]=maxcup[i];if(i) q.op[q.step]=2;//记录倒水的杯子,方便输出else q.op[q.step]=1;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0)//一种状态只能出现一次{Q.push(q);//入队vis[q.curcup[0]][q.curcup[1]]=1;//下次在遇到相同状态不处理}}
}
void Dropwater(int i)//把杯子里面的水倒出来
{q=p;if(q.curcup[i]>0&&q.curcup[i]<=maxcup[i])//其实只有当杯子一个杯子里面的水满了才会倒出,这样写完全是为了安全起见{q.curcup[i]=0;if(i) q.op[q.step]=4;else q.op[q.step]=3;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}
}
void Pourwater(int i,int j)//杯子i里面的水倒到j里面
{q=p;if(q.curcup[i]==0||q.curcup[j]==maxcup[j])//杯子i是空的或者杯子j是满的,直接跳出return ;if(q.curcup[i]>(maxcup[j]-q.curcup[j]))//杯子i里面的水多用不完的情况{q.curcup[i]-=(maxcup[j]-q.curcup[j]);q.curcup[j]=maxcup[j];if(i)q.op[q.step]=6;else q.op[q.step]=5;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}else if(q.curcup[i]<=(maxcup[j]-q.curcup[j]))//杯子i里面的水不够倒满杯子j{q.curcup[j]=q.curcup[i]+q.curcup[j];//我把这两行写颠倒了,课害苦我了q.curcup[i]=0;if(i)q.op[q.step]=6;else q.op[q.step]=5;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}
}int main()
{int i,j,f;while(~scanf("%d%d%d",&maxcup[0],&maxcup[1],&endcup)){memset(vis,0,sizeof(vis));f=0;p.curcup[0]=0;p.curcup[1]=0;p.step=0;Q.push(p);while(!Q.empty()){p=Q.front();Q.pop();if(p.curcup[0]==endcup||p.curcup[1]==endcup){f=1;printf("%d\n",p.step);for(i=0; i<p.step; i++){if(p.op[i]==1)printf("FILL(1)\n");if(p.op[i]==2)printf("FILL(2)\n");if(p.op[i]==3)printf("DROP(1)\n");if(p.op[i]==4)printf("DROP(2)\n");if(p.op[i]==5)printf("POUR(1,2)\n");if(p.op[i]==6)printf("POUR(2,1)\n");}break;}//六种情况分别考虑,能入队就入队Fillwater(0);Fillwater(1);Dropwater(0);Dropwater(1);Pourwater(0,1);Pourwater(1,0);vis[p.curcup[0]][p.curcup[1]]=1;}if(!f) printf("impossible\n");while(!Q.empty())Q.pop();}return 0;
}
这篇关于POJ 3414 Pots(模拟倒水)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!