原题:
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 32618 Accepted: 13470 Special Judge
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)
Source
Northeastern Europe 2002, Western Subregion
中文:
给你两个杯子,如下几种操作,可以将两个杯子灌满水,可以将两个杯子中的水倒掉,也可以将一个杯子中的水倒向另外一个杯子。给你起始的两个杯子的容量a,b,最后问你能否再最少的次数得到的c容量的水。
代码:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 101;
typedef long long ll;
typedef pair<int, int> pii;
struct node
{
int x,y,step;
};
int a,b,c;
int vis[maxn][maxn];
int fa[maxn][maxn];
int fb[maxn][maxn];
node bfs(node st)
{
memset(vis,0,sizeof(vis));
memset(fa,0,sizeof(fa));
memset(fb,0,sizeof(fb));
queue<node> Q;
vis[st.x][st.y] = maxn;
fa[st.x][st.y] = -1;
fb[st.x][st.y] = -1;
Q.push(st);
node val;
while(!Q.empty())
{
node tmp = Q.front();
Q.pop();
if (tmp.x == c || tmp.y == c)
{
return tmp;
}
int x, y;
// fill a
x = a;
y = tmp.y;
if (!vis[x][y])
{
vis[x][y] = 1;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
// fill b
x = tmp.x;
y = b;
if (!vis[x][y])
{
vis[x][y] = 2;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
// drop a
x = 0;
y = tmp.y;
if (!vis[x][y])
{
vis[x][y] = 3;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
// drop b
x = tmp.x;
y = 0;
if (!vis[x][y])
{
vis[x][y] = 4;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
// pour a to b
x = b - tmp.y >= tmp.x ? 0 : tmp.x - (b - tmp.y);
y = b - tmp.y >= tmp.x ? tmp.y + tmp.x : b;
if (!vis[x][y])
{
vis[x][y] = 5;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
// pour b to a
x = a - tmp.x >= tmp.y ? tmp.y + tmp.x : a;
y = a - tmp.x >= tmp.y ? 0 : tmp.y - (a - tmp.x);
if (!vis[x][y])
{
vis[x][y] = 6;
val.x = x;
val.y = y;
val.step = tmp.step + 1;
fa[x][y] = tmp.x;
fb[x][y] = tmp.y;
Q.push(val);
}
}
val.step = -1;
return val;
}
void get_path(int x, int y, int step)
{
if (step == 0)
{
return;
}
get_path(fa[x][y], fb[x][y], step - 1);
switch(vis[x][y])
{
case 1: cout<<"FILL(1)"<<endl; break;
case 2: cout<<"FILL(2)"<<endl; break;
case 3: cout<<"DROP(1)"<<endl; break;
case 4: cout<<"DROP(2)"<<endl; break;
case 5: cout<<"POUR(1,2)"<<endl; break;
case 6: cout<<"POUR(2,1)"<<endl; break;
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>a>>b>>c)
{
node st;
st.x = st.y = st.step = 0;
node ans = bfs(st);
if (ans.step == -1)
{
cout<<"impossible"<<endl;
}
else
{
cout<<ans.step<<endl;
get_path(ans.x, ans.y, ans.step);
}
}
return 0;
}
/*
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
*/
解答:
简单经典的倒水问题,如果看过lrj的紫书,再隐式图搜索一章有原题,不同的是,该题目求的是得到的c容量的水使用次数最少。
同样,使用广搜即可,注意保存状态和路径。