原题链接地址:http://ac.jobdu.com/problem.php?pid=1147
转载请注明本文链接:http://blog.csdn.net/yangnanhai93/article/details/42016353
BFS很简单的思想,但是注意剪枝,因为很多会重复,比如,不断的empty,这个重复很严重,所以很有必要去除重复,即记录1000 *1000的矩阵,保证对想通的a,b不重复计算
#include <stdio.h>
#include <queue>
#include <string>
#include <memory.h>
using namespace std;
string op[6]={"fill A","fill B","pour B A","pour A B","empty A","empty B"};
bool visited[1001][1001];
struct Node
{
int left,right;
vector<int> op;
};
void Cal(int a,int b,int q)
{
queue<Node> result;
Node first,second;
first.left=0;
first.right=0;
result.push(first);
memset(visited,0,sizeof(visited));
while(!result.empty())
{
first=result.front();
result.pop();
for(int i=0;i<6;i++)
{
second=first;
switch (i)
{
case 0:
second.left=a;
break;
case 1:
second.right=b;
break;
case 2://pour b to a
if(second.right<=a-second.left)
{
second.left=second.left+second.right;
second.right=0;
}
else
{
second.right=second.right-(a-second.left);
second.left=a;
}
break;
case 3:
if(second.left<=b-second.right)
{
second.right=second.right+second.left;
second.left=0;
}
else
{
second.left=second.left-(b-second.right);
second.right=b;
}
break;
case 4:
second.left=0;
break;
case 5:
second.right=0;
break;
}
second.op.push_back(i);
if(second.right==q)
{
for(int i=0;i<second.op.size();i++)
printf("%s\n",op[second.op[i]].c_str());
printf("success\n");
return;
}
else
{
if(!visited[second.left][second.right])
result.push(second);
visited[second.left][second.right]=true;
}
}
}
}
int main()
{
int a,b,q;
while(scanf("%d%d%d",&a,&b,&q)!=EOF)
{
Cal(a,b,q);
}
return 0;
}
/**************************************************************
Problem: 1147
User: vincent_ynh
Language: C++
Result: Accepted
Time:10 ms
Memory:2036 kb
****************************************************************/