您的位置:首页 > 其它

九度OJ 题目1147:Jugs

2015-09-03 17:14 375 查看

一.题目描述:

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when
the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A

fill B

empty A

empty B

pour A B

pour B A

success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

输入:

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca
<= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

输出:

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success".
Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

样例输入:

3 5 4

5 7 3

样例输出:

fill B

pour B A

empty A

pour B A

fill B

pour B A

success

fill A

pour A B

fill A

pour A B

empty B

pour A B

success

二.题目分析

典型的BFS搜索,代码较长,主要是输出路径部分不简洁,使用了pre数组和栈来逆序输出.

三.代码

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>

using namespace std;

#define MAX 1010

int visit[MAX][MAX],CA,CB,N,cn;

typedef struct
{
int a,b;
int action;
}Status;  //记录每次搜索的状态

Status pre[MAX][MAX];  //记录每个状态的前一个状态

void BFS(int sa,int sb)
{
queue<Status> q;
Status node;

node.a=sa;
node.b=sb;
node.action=-1;
visit[sa][sb]=1;
pre[node.a][node.b].a=-1;
pre[node.a][node.b].b=-1;
pre[node.a][node.b].action=-1;

while(!q.empty())
q.pop();

q.push(node);    //放入原始状态

while(!q.empty())
{
Status elem;
elem=q.front();
q.pop();

if(elem.b==N)    //找到解,输出搜索顺序
{
stack<Status>s;  //借助栈按照pre数组逆序输出

while(!s.empty())
s.pop();

while(elem.a!=0||elem.b!=0)
{
Status temp;
temp.a=elem.a;
temp.b=elem.b;
temp.action=elem.action;
s.push(temp);    //逆序入栈

elem.a=pre[temp.a][temp.b].a;
elem.b=pre[temp.a][temp.b].b;
elem.action=pre[temp.a][temp.b].action;
}

while(!s.empty())  //输出每个状态的动作
{
Status elem;
elem=s.top();
s.pop();
switch(elem.action)
{
case 1: printf("fill A\n"); break;
case 2: printf("fill B\n"); break;
case 3: printf("empty A\n"); break;
case 4: printf("empty B\n"); break;
case 5: printf("pour A B\n"); break;
case 6: printf("pour B A\n"); break;
default :
break;
}
}

printf("success\n");
break;
}

Status e1,e2,e3,e4,e5,e6;

//fill A
e1.a=CA;
e1.b=elem.b;
e1.action=1;
if(!visit[e1.a][e1.b])
{
visit[e1.a][e1.b]=1;
pre[e1.a][e1.b].a=elem.a;
pre[e1.a][e1.b].b=elem.b;
pre[e1.a][e1.b].action=elem.action;
q.push(e1);
}

//fill B
e2.a=elem.a;
e2.b=CB;
e2.action=2;
if(!visit[e2.a][e2.b])
{
visit[e2.a][e2.b]=1;
pre[e2.a][e2.b].a=elem.a;
pre[e2.a][e2.b].b=elem.b;
pre[e2.a][e2.b].action=elem.action;
q.push(e2);
}

//empty A
if(elem.a>0)
{
e3.a=0;
e3.b=elem.b;
e3.action=3;
if(!visit[e3.a][e3.b])
{
visit[e3.a][e3.b]=1;
pre[e3.a][e3.b].a=elem.a;
pre[e3.a][e3.b].b=elem.b;
pre[e3.a][e3.b].action=elem.action;
q.push(e3);
}
}

//empty B
if(elem.b>0)
{
e4.a=elem.a;
e4.b=0;
e4.action=4;
if(!visit[e4.a][e4.b])
{
visit[e4.a][e4.b]=1;
pre[e4.a][e4.b].a=elem.a;
pre[e4.a][e4.b].b=elem.b;
pre[e4.a][e4.b].action=elem.action;
q.push(e4);
}
}

//pour A B
if(elem.a>0)
{
e5.action=5;
if(elem.a>(CB-elem.b))   //A remains,B full
{
e5.a=elem.a-(CB-elem.b);
e5.b=CB;
}
else  //A empty,B unknow
{
e5.a=0;
e5.b=elem.a+elem.b;
}
if(!visit[e5.a][e5.b])
{
visit[e5.a][e5.b]=1;
pre[e5.a][e5.b].a=elem.a;
pre[e5.a][e5.b].b=elem.b;
pre[e5.a][e5.b].action=elem.action;
q.push(e5);
}
}

//pour B A
if(elem.b>0)
{
e6.action=6;
if(elem.b>(CA-elem.a))    //B remains,A full
{
e6.a=CA;
e6.b=elem.b-(CA-elem.a);
}
else  //B empty,A unknow
{
e6.a=elem.a+elem.b;
e6.b=0;
}
if(!visit[e6.a][e6.b])
{
visit[e6.a][e6.b]=1;
pre[e6.a][e6.b].a=elem.a;
pre[e6.a][e6.b].b=elem.b;
pre[e6.a][e6.b].action=elem.action;
q.push(e6);
}
}
}
}

int main()
{

int i,j;

freopen("1147.txt","r",stdin);
while(scanf("%d%d%d",&CA,&CB,&N)!=EOF)
{
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
visit[i][j]=0;

BFS(0,0);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: