题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=21
题目分析:
暴力+BFS。将所有可能的状态压到队列中,直到找到目标状态为止。
我自己AC的代码。
#include<stdio.h>
#include<string.h>
//三个水杯的盛水的状态
struct Node
{
int a;//水杯1的水量
int b;//水杯2的水量
int c;//水杯3的水量
int flag;
};
Node queue[100];
bool used[101][101][101];
inline void Pour(Node p,int &tail)
{
if(!used[p.a][p.b][p.c])
{
queue[++tail] = p;
used[p.a][p.b][p.c] = true;
}
}
inline int min(const int a, const int b)
{
return a < b ? a :b;
}
//寻找倒水路径
int PourWaterTwo(int *goal, int *vol)
{
int head = 0;
int tail = 0;
int temp;
Node s,p;
while(head <= tail)
{
s = queue[head];
//将1倒入2或3中
if(s.a > 0)
{
if(s.b < vol[1])
{
temp = min(vol[1] - s.b , s.a);
p.a = s.a - temp