Pots
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 8821 | Accepted: 3739 | 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
/*
*project:3414 Pots
*version:BFS+AC
*author:骨灰在飞扬
*Memory:664K
*Time:16MS
*note:
date:2014/2/25
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int A,B,C;
bool flag = false;
bool visit[110][110];
struct Node
{
int a; // 瓶1中的水量
int b; // 瓶2中的水量
int step;
string operat; // 操作
Node *former;
} q[10000];
void printResult(Node *p) // 输出所有操作
{
stack<string> s;
while(p->former != NULL)
{
s.push(p->operat);
p = p->former;
}
while(!s.empty())
{
cout << s.top() << endl;
s.pop();
}
return;
}
void BFS()
{
int head = 0, tail = 1;
while(head < tail)
{
Node *temp = &q[head++];
int i;
int a = temp->a;
int b = temp->b;
int step = temp->step;
if(a == C || b == C)
{
cout << step << endl;
printResult(temp);
flag = true;
return;
}
for(i = 0; i < 3; i++)
{
switch (i)
{
case 0: // FILL
if(a < A && !visit[A][b]) // FILL A
{
visit[A][b] = true;
q[tail].a = A;
q[tail].b = b;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "FILL(1)";
}
if(b < B && !visit[a][B]) // FILL B
{
visit[a][B] = true;
q[tail].a = a;
q[tail].b = B;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "FILL(2)";
}
break;
case 1: // DROP
if(a > 0 && !visit[0][b]) // DROP A
{
visit[0][b] = true;
q[tail].a = 0;
q[tail].b = b;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "DROP(1)";
}
if(b > 0 && !visit[a][0]) // DROP B
{
visit[a][0] = true;
q[tail].a = a;
q[tail].b = 0;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "DROP(2)";
}
break;
case 2:
if(a > 0 && b < B) // POUR A to B
{
if(a > (B-b) && !visit[a-(B-b)][B]) // A有剩,B装满
{
visit[a-(B-b)][B] = true;
q[tail].a = a-(B-b);
q[tail].b = B;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "POUR(1,2)";
}
else if(!visit[0][a+b]) // A倒空,B未满
{
visit[0][a+b] = true;
q[tail].a = 0;
q[tail].b = a+b;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "POUR(1,2)";
}
}
if(a < A && b > 0 ) // POUR B to A
{
if(b > (A-a) && !visit[A][b-(A-a)]) // A装满,B有剩
{
visit[A][b-(A-a)] = true;
q[tail].a = A;
q[tail].b = b-(A-a);
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "POUR(2,1)";
}
else if(!visit[a+b][0]) // A未满,B倒空
{
visit[a+b][0] = true;
q[tail].a = a+b;
q[tail].b = 0;
q[tail].step = step+1;
q[tail].former = temp;
q[tail++].operat = "POUR(2,1)";
}
}
break;
}
}
}
return;
}
int main()
{
//ifstream cin("E:\\vcpp\\test.txt");
cin >> A >> B >> C;
memset(visit,false,sizeof(visit));
visit[0][0] = true;
q[0].a = 0; q[0].b = 0; q[0].step = 0; q[0].operat = ""; q[0].former = NULL;
BFS();
if(!flag)
cout << "impossible" << endl;
return 0;
}
本文探讨了如何通过人工智能算法解决两个水壶不同容量的问题,找到最短的操作序列以达到指定水量之一的目标。
1771

被折叠的 条评论
为什么被折叠?



