有三个杯子容积如下:A:80L B:50L C:30L

zhangyong369 2008-10-28 10:21:59
请教
有三个杯子容积如下:A:80L B:50L C:30L
现在将A杯注满水,要达到如下状态(a:40 b:40 c:0)
用C语言如何操作????

...全文
200 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
//   三个杯子平分液体.cpp   :   Defines   the   entry   point   for   the   console   application.   
//

/*有三个杯子容积如下:A:80L B:50L C:30L,现在将A杯注满水,
要达到如下状态(a:40 b:40 c:0),应该如何操作????*/

# include "stdafx.h"
# include <stdio.h>
# define N 4000
# define M 100
struct movestate{
int num;
int source;
int target;
int volume;
int stateA;
int stateB;
int stateC;
int lastnum;
};
struct movestate node[N];
/*实施倾倒液体的操作,各杯子的操作状态列表保存到一个特殊的数据结构node[]中*/


int stateexist(int pfather,int cup[3])
/* stateexist函数查看当前状态是否与历史路径上的状态相同,相同返回1;
显然返回1状态的节点说明路径循环,应该丢弃该状态节点,
结束这个无意义的路径分支*/
{
int tag=0;
while (pfather!=-1)
{
if (node[pfather].stateA==cup[0] && node[pfather].stateB==cup[1] \
&& node[pfather].stateC==cup[2]) tag=1;
pfather=node[pfather].lastnum;
}
//printf("!%d\t",tag);
return tag;
}


/*函数searchpath 回朔并打印历史操作路径,入口参数point为当前
叶子节点的序号*/
void searchpath (int point)
{
if (node[point].lastnum==-1)
{
printf(" ******** the basic state ******* ");
printf("A:%2d B:%2d C:%2d \n",80,0,0);
return;
}
else
{
searchpath(node[point].lastnum);
//(*step)++;
}

printf("step:%3d move %c to %c volume %d L ******* A:%2d B:%2d C:%2d \n"\
,point,node[point].\
source+65,node[point].target+65,node[point].volume,\
node[point].stateA,node[point].stateB,node[point].\
stateC);
}








void main()
{
int max[3]; /* 三个杯子的容积*/
int cup[3]; /* 三个杯子的状态*/
int move; /* cup[i]向cup[j]倒出溶液的体积*/
int i,j; /* i 源杯代号,j为目标杯代号*/
int pfather=0; /* 表征子纪录编号,为父操作节点指针*/
int pchild=0; /* 表征父节点编号,为子操作节点指针*/



/***************************程序初始化设置******************************************/

max[0]=80;max[1]=50;max[2]=30; /*初始化设置三个杯子的容积*/
cup[0]=80;cup[1]=0 ;cup[2]=0 ; /*初始化设置三个杯子的状态*/
{
node[0].num=0;
node[0].source=0;
node[0].target=0;
node[0].volume=0;
node[0].stateA=cup[0];
node[0].stateB=cup[1];
node[0].stateC=cup[2];
node[0].lastnum=-1;
}
/*操作状态列表初始化设置 初始状态:A:80 B:0 C:0 求解目标状态:A:40 B:40 C:0*/

pfather=pchild=0; /*初始状态下父子指针均指向node[0]*/


/***************************类似树的按层次遍历******************************************/
while (pfather<=pchild)
{
while (node[pfather].stateA==40 && node[pfather].stateB==40 \
&& node[pfather].stateC==0) pfather++;
/*目标状态的节点不可以再作为父节点实施倾倒液体的操作,
上述操作跳过达到目标状态(A:40 B:40 C:0)的节点,
终止其状态变化*/
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
/*为了遍历针对同一状态各种操作可能达到的所有状态
必须在此还原父节点状态现场*/
cup[0]=node[pfather].stateA;
cup[1]=node[pfather].stateB;
cup[2]=node[pfather].stateC;
if (i!=j && cup[i]!=0 && cup[j]!=max[j])
/*杯子向本身倒水,空杯子倒出水,满杯子倒入水都是没有意义的,
所以过滤掉*/
{
/*由cup[i]向cup[j]倒水导致两个杯子容量的变化
1.cup[i]将cup[j]注满,cup[i]有剩余
2.cup[i]全部倒入cup[j],cup[i]倒空 */
if (cup[i]>max[j]-cup[j])
{
move=max[j]-cup[j];
cup[i]=cup[i]-move;
cup[j]=max[j];
}
else
{
move=cup[i];
cup[j]=cup[i]+cup[j];
cup[i]=0;
}

/*回朔历史路径上的所有状态,当前状态如果与之重复,将不会
加入状态列表;反之,推动pchild指针,加入当前状态信息*/
if (!stateexist(pfather,cup))
{
pchild++;
node[pchild].num=pchild;
node[pchild].source=i;
node[pchild].target=j;
node[pchild].volume=move;
node[pchild].stateA=cup[0];
node[pchild].stateB=cup[1];
node[pchild].stateC=cup[2];
node[pchild].lastnum=pfather;
}

}
}
pfather++;

}

/*此处输出节点列表,目标节点的记录号,用于测试程序的正确性*/
/*for (i=0;i<155;i++)
printf("%5d %5d %5d %5d %5d %5d %5d %5d \n",\
node[i].num,node[i].source,node[i].target,node[i].volume,\
node[i].stateA,node[i].stateB,node[i].stateC,node[i].lastnum);

printf("pchild is %d,pfather is %d\n",pchild,pfather);
*/

/**************** 根据目标纪录的纪录号回朔输出操作过程*************/

int group=1; //操作组合的标号
while (pchild>=0)
{

if (node[pchild].stateA==40 && node[pchild].stateB==40 \
&& node[pchild].stateC==0)
/*满足目标状态(A:40 B:40 C:0)的叶子节点,
用函数searchpath输出*/
{
printf("\n\t\t输出第 %d 组可能的的操作过程:\n",group);
searchpath(pchild);
group++;
}
pchild--;
}

}
  • 打赏
  • 举报
回复
 #include   <iostream.h>       
class Jug
{
public:
int Set();
void FillA();
void FillB();
void EmptyB();
void EmptyA();
void PourA_B();
void PourB_A();
int AFull();
int BFull();
int A_Pet();
int B_Pet();
int Test();
private:
int A,B;
int Ca,Cb;
int N;
};
int Jug::Set()
{
A=0;
B=0;
if(cin>>Ca>>Cb>>N)
return 1;
return 0;
}

void Jug::FillA()
{
cout<<"fill A"<<endl;
A=Ca;
}


void Jug::FillB()
{
cout<<"fill B"<<endl;
B=Cb;
}

void Jug::EmptyA()
{
cout<<"empty A"<<endl;
A=0;
}

void Jug::EmptyB()
{
cout<<"empty B"<<endl;
B=0;
}

void Jug::PourA_B()
{
cout<<"pour A B"<<endl;
if((A+B)>=Cb)
{
A=A-(Cb-B);
B=Cb;
}
else
{
B=A+B;
A=0;
}
}

void Jug::PourB_A()
{
cout<<"pour B A"<<endl;
if((B+A)>=Ca)
{
A=Ca;
B=B-(Ca-A);
}
else
{
A=A+B;
B=0;
}
}

int Jug::A_Pet()
{
if(A==N)
return 1;
else
return 0;
}

int Jug::B_Pet()
{
if(B==N)
return 1;
else
return 0;
}

int Jug::AFull()
{
if(A==Ca)
return 1;
return 0;
}


int Jug::BFull()
{
if(B==Cb)
return 1;
return 0;
}

int Jug::Test()
{
if(N==Ca)
{
FillA();
return 1;
}
if(N==Cb)
{
FillB();
return 1;
}
return 0;
}

int main()
{
Jug a;
while(a.Set())
{
if(!a.Test())
while(!a.A_Pet()&&!a.B_Pet())
{
a.FillA();
a.PourA_B();
if(a.BFull())
{
a.EmptyB();
a.PourA_B();
}
}
cout<<"success"<<endl;
}

return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧