TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(77)波瓦松分酒趣题

用递归实现了:
- import java.io.*;
- public class Test{
- int V[]={12,8,5};//瓶子容量
- int src[] ={0,0,1,1,2,2};//有6种倒酒方法,src[0]->dest[0]表示大瓶往中瓶倒,src[3]->dest[3]表示中瓶往小瓶倒,其它类同.
- int dest[]={1,2,0,2,0,1};
- /* 记录分酒步骤的数组:如
- * A B C
- * 12 0 0
- * 4 8 0
- * 0 8 4
- * 8 0 4
- * 8 4 0
- * 3 4 5
- * 3 8 1
- * 11 0 1
- * 11 1 0
- * 6 1 5
- * 6 6 0
- */
- int record[][]=new int[100][3];
- int rec_index=0;
- int count=1;//分酒方法的种数
- //在控制台和文件中输出
- public void Output(PrintWriter pw)
- {
- pw.printf("(%d)----------------------------------
- ",count);
- System.out.printf("(%d)----------------------------------
- ",count);
- pw.printf(" A B C
- ");
- System.out.printf(" A B C
- ");
- for(int i=0;i< rec_index;++i){
- System.out.printf("%4d %4d %4d
- ",record[i][0],record[i][1],record[i][2]);
- pw.printf("%4d %4d %4d
- ",record[i][0],record[i][1],record[i][2]);
- }
-
-
- }
- public void Record(int state[])//记录步骤
- {
- record[rec_index][0]=state[0];
- record[rec_index][1]=state[1];
- record[rec_index][2]=state[2];
- ++rec_index;
- }
- public boolean Exist(int state[])//是否回到了前一步
- {
- for(int i=0;i< rec_index;++i)
- if (state[0]==record[i][0]
- && state[1]==record[i][1]
- && state[2]==record[i][2])
- return true;
- return false;
- }
- public void Solve(int state[],PrintWriter pw) //递归求解
- {
- int a=state[0],b=state[1],c=state[2];
- Record(state);
- if(a==6 && b==6 && c==0) { //找到一种分酒方法,输出
- Output(pw);
- count++;
- return;
- }
- for(int i=0;i< 6;++i)//针对六种倒酒方法,递归求出每一种的所有解。
- {
- if(state[src[i]]==0) continue;//
- Pour(state,src[i],dest[i]);
- if(!Exist(state))
- {
- Solve(state,pw);
- --rec_index;
- }
- state[0]=a;state[1]=b;state[2]=c;
- }
- }
- public void Pour(int state[],int x,int y)
- {
- int r=V[y]-state[y];//计算y瓶中还可以盛多少酒,y的总容量-y的状态(当时的容量)
- if(state[x]< r) {state[y]+=state[x];state[x]=0;}//将x中的酒全部倒入y
- else {state[y]=V[y];state[x]-=r;}//从x瓶中倒入r品脱酒到y瓶中,此时y瓶满
- }
- public static void main(String args[]) throws IOException
- {
- //PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("result.txt")),false);
- File file=new File("result.txt");//将结果写入文件
- PrintWriter pw=new PrintWriter(new FileWriter(file));
- int init[]={12,0,0};
- new Test().Solve(init,pw);
- pw.close();
- }
- }
- 运行结果:
- (1)----------------------------------
- A B C
- 12 0 0
- 4 8 0
- 0 8 4
- 8 0 4
- 8 4 0
- 3 4 5
- 3 8 1
- 11 0 1
- 11 1 0
- 6 1 5
- 6 6 0
- (2)----------------------------------
- A B C
- 12 0 0
- 4 8 0
- 4 3 5
- 0 7 5
- 7 0 5
- 7 5 0
- 2 5 5
- 2 8 2
- 0 8 4
- 8 0 4
- 8 4 0
- 3 4 5
- 3 8 1
- 11 0 1
- 11 1 0
- 6 1 5
- 6 6 0
- (3)----------------------------------
- 。。。。。。。。。。。。。。。。。。。。。。。
- (120)----------------------------------
- A B C
- 12 0 0
- 7 0 5
- 7 5 0
- 2 5 5
- 2 8 2
- 4 8 0
- 4 3 5
- 9 3 0
- 9 0 3
- 1 8 3
- 1 6 5
- 0 7 5
- 0 8 4
- 8 0 4
- 8 4 0
- 3 4 5
- 3 8 1
- 11 0 1
- 11 1 0
- 6 1 5
- 6 6 0
- (121)----------------------------------
- A B C
- 12 0 0
- 7 0 5
- 7 5 0
- 2 5 5
- 2 8 2
- 4 8 0
- 4 3 5
- 9 3 0
- 9 0 3
- 1 8 3
- 1 6 5
- 6 6 0
复制代码
源码下载:http://file.javaxxz.com/2014/11/14/000834312.zip |
|