原题目:
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 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)
题目大概中文:
字面挺简单的,就是给你俩壶,进行操作3种操作,比如把a壶装满,或者a壶倒入b壶,或者a壶水倒完,最后能不能凑出来c升水
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<fstream>
#include<set>
#include<math.h>
using namespace std;
int a,b,c;
struct N
{
int l,r;
int step;
string str;
};
queue<N> que;
bool vis[105][105];
N bfs()
{
N ans={0,0,-1,""};
while(!que.empty())
{
N cnt=que.front();
que.pop();
int step=cnt.step;
if(cnt.l==c||cnt.r==c)
{
return cnt;
}
if(cnt.l>0)//要进行a倒水
{
N newp={0,cnt.r,step+1,cnt.str};
newp.str.insert(newp.str.length(),"DROP(1)\n");
if(vis[newp.l][newp.r]==0)
{
vis[newp.l][newp.r]=1;
que.push(newp);
}
}
if(cnt.l<a)//a要进行加水
{
N nnewp={a,cnt.r,step+1,cnt.str};
nnewp.str.insert(nnewp.str.length(),"FILL(1)\n");
if(vis[nnewp.l][nnewp.r]==0)
{
vis[nnewp.l][nnewp.r]=1;
que.push(nnewp);
}
}
if (cnt.r<b){//a倒水b
N nnewp = { cnt.l - b + cnt.r, b, step + 1, cnt.str };
if (cnt.l<b - cnt.r){
nnewp.l = 0, nnewp.r = cnt.r + cnt.l;
}
nnewp.str.insert(nnewp.str.length(), "POUR(1,2)\n");
if (vis[nnewp.l][nnewp.r] == 0){
vis[nnewp.l][nnewp.r] = 1;
que.push(nnewp);
}
}
if (cnt.r >0){//b倒水
N newp = { cnt.l, 0, step + 1, cnt.str };
newp.str.insert(newp.str.length(), "DROP(2)\n");
if (vis[newp.l][newp.r] == 0){
vis[newp.l][newp.r] = 1;
que.push(newp);
}
}
if (cnt.r<b){//b装水
N nnewp = { cnt.l,b, step + 1, cnt.str };
nnewp.str.insert(nnewp.str.length(), "FILL(2)\n");
if (vis[nnewp.l][nnewp.r] == 0){
vis[nnewp.l][nnewp.r] = 1;
que.push(nnewp);
}
}
if (cnt.l<a){//b倒水a
N nnewp = {a, cnt.r-a+cnt.l, step + 1, cnt.str };
if (cnt.r<a - cnt.l){
nnewp.r = 0, nnewp.l = cnt.r + cnt.l;
}
nnewp.str.insert(nnewp.str.length(), "POUR(2,1)\n");
if (vis[nnewp.l][nnewp.r] == 0){
vis[nnewp.l][nnewp.r] = 1;
que.push(nnewp);
}
}
}
return ans;
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
N beg={0,0,0,""};
vis[0][0]=1;
que.push(beg);
N ans=bfs();
if(ans.step==-1)
{
printf("impossible\n");
}
else
{
printf("%d\n",ans.step);
cout<<ans.str;
}
return 0;
}
思路:(广度优先搜索)
一共分成6种情况,把a倒满,把a倒完,a倒入b,把b倒满,把b倒完,把b倒入a,实属非常麻烦,难受
PS:昨天的杭电多校联赛简直了,最简单的题目就是网络流问题,最小割最大流,毫无疑问的爆零场,差距不是一般的大啊