Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7809 | Accepted: 3277 | 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, andC. 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 operationsK. 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
#include<stdio.h>
#include<queue>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
using namespace std;
int goal,m,n,flag=1;//goal 目标;m,n两个瓶子的容量;flag 标记能否达到目标。
int p = 0; //用于记录路径的下标
struct State //状态
{
int x,y;
char op[MAX]; // 记录路径
int step_counter; //记录步数
}st;
bool visit[MAX][MAX]; //记录是否已访问
bool check_state(State s)
{
if(!visit[s.x][s.y])
return 1;
else
return 0;
};
State full1 (State a)
{
a.x=m;
a.op[p] = '1';
return a;
};
State full2 (State a )
{
a.y=n;
a.op[p] = '2';
return a;
}
State drop1(State a)
{
a.x=0;
a.op[p] = '3';
return a;
};
State drop2(State a)
{
a.y=0;
a.op[p] = '4';
return a;
};
State pour1(State a)
{
if(a.x>n-a.y)
{
a.x = a.x-n+a.y;
a.y = n;
}
else
{
a.y += a.x;
a.x = 0;
}
a.op[p] = '5';
return a;
};
State pour2(State a)
{
if(a.y>m-a.x)
{
a.y = a.y+a.x-m;
a.x = m;
}
else
{
a.x += a.y;
a.y = 0;
}
a.op[p] = '6';
return a;
};
void bfs(State start)
{
queue <State> q;
State now,next;
q.push(start);
start.step_counter = 0;
visit[start.x][start.y] = 1;
while(!q.empty())
{
now = q.front();
p=now.step_counter;
if(now.x == goal || now.y == goal)//符合条件
{
printf("%d\n",now.step_counter);
flag = 0;
for(int k=0;k<now.step_counter;k++)
{
switch(now.op[k]-'0')// 输出路径
{
case(1): printf("FILL(1)\n");break;
case(2): printf("FILL(2)\n");break;
case(3): printf("DROP(1)\n");break;
case(4): printf("DROP(2)\n");break;
case(5): printf("POUR(1,2)\n");break;
case(6): printf("POUR(2,1)\n");break;
}
}
return;
}
for(int i=1;i<=6;i++)
{
switch(i)
{
case(1):next = full1(now);break;//六种操作
case(2):next = full2(now);break;
case(3):next = drop1(now);break;
case(4):next = drop2(now);break;
case(5):next = pour1(now);break;
case(6):next = pour2(now);break;
}
if(check_state(next))
{
next.step_counter = now.step_counter + 1;
q.push(next);
visit[next.x][next.y]= 1;
}
}
q.pop();
}
}
int main()
{
scanf("%d %d %d",&m,&n,&goal);
st.step_counter = st.x = st.y = 0;
for(int h=0;h<MAX;h++)
st.op[h] = '\0';//初始化
bfs(st);
if(flag) printf("impossible\n");
system("pause");
return 0;
}