Pots
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem 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. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Example Input
3 5 4
Example Output
6
Hint
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Author
这道题是一道BFS的运用,有六种状态:
1.将A中的水倒满;
2.将B中的水倒满;
3.将A中的水倒空;
4.将B种的水倒空;
5.将A中的水倒入B中;
6.将B中的水倒入A中(考虑杯中是否已满);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 121
int a, b, c;
int v[MAXN][MAXN];
struct node
{
int x,y,step;
}que[MAXN*MAXN], p, q;
void bfs(int a, int b, int c)
{
int front = 0, rear = 0;
p.x = 0;
p.y = 0;
p.step = 0;
que[rear++] = p;
v[p.x][p.y] = 1;
while(front<rear)
{
q = que[front++];
if(q.x==c||q.y==c)
{
printf("%d\n", q.step);
return ;
}
for(int i=0;i<6;i++)
{
if(i==0)//将B倒满
{
p.x = q.x;
p.y = b;
}
if(i==1)//将A倒满
{
p.y = q.y;
p.x = a;
}
if(i==2)//A倒空
{
p.x = 0;
p.y = q.y;
}
if(i==3)//B倒空
{
p.y = 0;
p.x = q.x;
}
if(i==4)//B倒入A
{
p.x = q.x + q.y;
if(p.x>a)
{
p.y = p.x - a;
p.x = a;
}
else
p.y = 0;
}
if(i==5)//A倒入B
{
p.y = q.y + q.x;
if(p.y>b)
{
p.x = p.y - b;
p.y = b;
}
else
p.x = 0;
}
if(v[p.x][p.y]==0)//判断是否可行
{
v[p.x][p.y]=1;
p.step = q.step + 1;
que[rear++] = p;
}
}
}
printf("impossible\n");
}
int main()
{
while(~scanf("%d %d %d", &a, &b, &c))
{
memset(v, 0, sizeof(v));//清零
bfs(a, b, c);
}
return 0;
}
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)
题目差不多,但是此题要记录所走的每一步~
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#define MAXN 123
using namespace std;
struct node
{
int x,y,step;
char operate[MAXN][MAXN];
}que[MAXN*MAXN], p, q, w;
int v[MAXN][MAXN];
int a, b, c;
void bfs()
{
int front = 0, rear = 0;
q.x = 0;
q.y = 0;
q.step = 0;
que[rear++] = q;
v[q.x][q.y] = 1;
while(front<rear)
{
p = que[front++];
if(p.x==c||p.y==c)
{
printf("%d\n", p.step);
for(int i=1;i<=p.step;i++)
printf("%s\n", p.operate[i]);
return ;
}
if(p.x==0)
{
//FILL(1)
q = p;
q.x = a;
q.y = p.y;
q.step = p.step + 1;
strcpy(q.operate[q.step], "FILL(1)");
if(v[q.x][q.y]==0)
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
}
else if(p.x!=0)
{
//DROP(1)
q = p;
q.x = 0;
q.y = p.y;
q.step = p.step + 1;
strcpy(q.operate[q.step], "DROP(1)");
if(v[q.x][q.y]==0)
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
//POUR(1, 2)
if(p.y<b)
{
q = p;
if(p.x+p.y>=b)
{
q.x = p.x + p.y - b;
q.y = b;
}
else
{
q.x = 0;
q.y = p.x + p.y;
}
q.step = p.step + 1;
strcpy(q.operate[q.step], "POUR(1,2)");
if(!v[q.x][q.y])
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
}
}
if(p.y==0)
{
//FILL(2)
q = p;
q.x = p.x;
q.y = b;
q.step = p.step + 1;
strcpy(q.operate[q.step], "FILL(2)");
if(v[q.x][q.y]==0)
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
}
else if(p.y!=0)
{
//DROP(2)
q = p;
q.x = p.x;
q.y = 0;
q.step = p.step + 1;
strcpy(q.operate[q.step], "DROP(2)");
if(v[q.x][q.y]==0)
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
//POUR(2, 1)
if(p.x<a)
{
q = p;
if(p.x+p.y>=a)
{
q.y = p.x + p.y - a;
q.x = a;
}
else
{
q.y = 0;
q.x = p.x + p.y;
}
q.step = p.step + 1;
strcpy(q.operate[q.step], "POUR(2,1)");
if(!v[q.x][q.y])
{
v[q.x][q.y] = 1;
que[rear++] = q;
}
}
}
}
printf("impossible\n");
}
int main()
{
scanf("%d %d %d", &a, &b, &c);
memset(v, 0, sizeof(v));
bfs();
return 0;
}