【蓝桥杯】泊松分酒

题目:

    泊松是法国数学家、物理学家和力学家。他一生致力科学事业,成果颇多。有许多著名的公式定理以他的名字命名,比如概率论中著名的泊松分布。

    有一次闲暇时,他提出过一个有趣的问题,后称为:“泊松分酒”。在我国古代也提出过类似问题,遗憾的是没有进行彻底探索,其中流传较多是:“韩信走马分油”问题。

    有3个容器,容量分别为12升,8升,5升。其中12升中装满油,另外两个空着。要求你只用3个容器操作,最后使得某个容器中正好有6升油。

    下面的列表是可能的操作状态记录:
12,0,0
4,8,0
4,3,5
9,3,0
9,0,3
1,8,3
1,6,5

    每行3个数据,分别表示12,8,6升容器中的油量

    第一行表示初始状态,第二行表示把12升倒入8升容器后的状态,第三行是8升倒入5升,...

    当然,同一个题目可能有多种不同的正确操作步骤。

    本题目的要求是,请你编写程序,由用户输入:各个容器的容量,开始的状态,和要求的目标油量,程序则通过计算输出一种实现的步骤(不需要找到所有可能的方法)。如果没有可能实现,则输出:“不可能”。

    例如,用户输入:
12,8,5,12,0,0,6

    用户输入的前三个数是容器容量(由大到小),接下来三个数是三个容器开始时的油量配置,最后一个数是要求得到的油量(放在哪个容器里得到都可以)

    则程序可以输出(答案不唯一,只验证操作可行性):
12,0,0
4,8,0
4,3,5
9,3,0
9,0,3
1,8,3
1,6,5

    每一行表示一个操作过程中的油量状态。


题目理解:

        利用BFS的方法可以搜索出是否能够完成,然后利用有前驱的链表进行记录。


代码:

/*
ID:泊松分酒 
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#include <algorithm>
#define SWAP(x,y) {(x) = (x)^(y); (y)=(x)^(y); (x)=(x)^(y);}
#define MIN(x,y) (x<y?x:y)
#define INF 1e9
#define MAXN 100
struct Node{
	int a,b,c;
	struct Node *pre;
}node[MAXN*MAXN*MAXN],init;				//init记录容器的最大容量 

bool visit[MAXN][MAXN][MAXN];

void print(struct Node now){			//递归输出 
	if(now.pre != NULL){
		print(*now.pre);
	}
	printf("%d,%d,%d\n", now.a, now.b, now.c);
	
}

void bfs(int target){
	int head = 0, tail = 1;
	node[0].pre = NULL;
	visit[node[0].a][node[0].b][node[0].c] = true;
	while(tail-head>0){
		int count = tail-head;
		while(count--){
			struct Node now = node[head];
			if(now.a==target||now.b==target||now.c==target){
				print(now);
				return;
			}
			
			int min = MIN(now.a, init.b-now.b);	//a向b中倒 
			if(!visit[now.a-min][now.b+min][now.c]){
				node[tail].a = now.a-min;
				node[tail].b = now.b+min;
				node[tail].c = now.c;
				node[tail].pre = &node[head];
				visit[now.a-min][now.b+min][now.c] = true;
				tail++;
			}
			
			min = MIN(now.a, init.c-now.c);		//a向c中倒
			if(!visit[now.a-min][now.b][now.c+min]){
				node[tail].a = now.a-min;
				node[tail].b = now.b;
				node[tail].c = now.c+min;
				node[tail].pre = &node[head];
				visit[now.a-min][now.b][now.c+min] = true;
				tail++;
			}
			
			min = MIN(init.a-now.a, now.b);		//b向a 
			if(!visit[now.a+min][now.b-min][now.c]){
				node[tail].a = now.a+min;
				node[tail].b = now.b-min;
				node[tail].c = now.c;
				node[tail].pre = &node[head];
				visit[now.a+min][now.b-min][now.c] = true;
				tail++;
			}
			
			min = MIN(now.b, init.c-now.c);		//b向c 
			if(!visit[now.a][now.b-min][now.c+min]){
				node[tail].a = now.a;
				node[tail].b = now.b-min;
				node[tail].c = now.c+min;
				node[tail].pre = &node[head];
				visit[now.a][now.b-min][now.c+min] = true;
				tail++;
			}
			
			min = MIN(init.a-now.a, now.c);		//c向a 
			if(!visit[now.a+min][now.b][now.c-min]){
				node[tail].a = now.a+min;
				node[tail].b = now.b;
				node[tail].c = now.c-min;
				node[tail].pre = &node[head];
				visit[now.a+min][now.b][now.c-min] = true;
				tail++;
			}
			
			min = MIN(init.b-now.b, now.c);		//c向b
			if(!visit[now.a][now.b+min][now.c-min]){
				node[tail].a = now.a;
				node[tail].b = now.b+min;
				node[tail].c = now.c-min;
				visit[now.a][now.b+min][now.c-min] = true;
				tail++;
			} 
			head++;
		}
	}
	printf("Impossible\n"); 
}

int main(){
	memset(visit, false, sizeof(visit));
	int target;
	
	scanf("%d,%d,%d,%d,%d,%d,%d",
		&init.a, &init.b,&init.c,&node[0].a,&node[0].b,&node[0].c,&target);
	bfs(target);
	return 0;
}


参考样例:

/*
样例1:
 	输入:
	 	12,8,5,12,0,0,6
 	输出: 
 		12,0,0
		4,8,0
		4,3,5
		9,3,0
		9,0,3
		1,8,3
		1,6,5
		
样例2:
 	输入:
	 	30,13,7,30,0,0,5
 	输出:
 		30,0,0
		17,13,0
		17,6,7
		24,6,0
		24,0,6
		11,13,6
		11,12,7
		18,12,0
		18,5,7
		
样例3:
 	输入:
	 	31,19,11,31,0,0,5
 	输出:
	 	31,0,0
		12,19,0
		12,8,11
		23,8,0
		23,0,8
		4,19,8
		4,16,11
		15,16,0
		15,5,11
 	
样例4:
 	输入:
	 	65,33,12,65,0,0,18
 	输出:
	 	65,0,0
		32,33,0
		32,21,12
		44,21,0
		44,9,12
		56,9,0
		56,0,9
		23,33,9
		23,30,12
		35,30,0
		35,18,12
*/ 

附言:

由于没有使用STL,所以代码比较冗长。如果能很好的使用STL,可以很大程度的简约代码。

### 算法的 C++ 实现与解释 是经典的穷举法应用之一,主要通过模拟倒过程中的状态变化来解决问。以下是基于引用的内容以及专业知识对该问的详细解析。 #### 穷举法的核心思想 穷举法是一种依赖计算机强大计算能力的经典算法,适用于解决那些没有明显规律可循的问[^2]。对于而言,可以通过枚举所有可能的状态转移路径找到最终解。 #### 描述 假设存在三个容器 A、B 和 C,别具有容量 `a`、`b` 和 `c` 升(其中 c 是目标容量),初始状态下只有 A 容器装满水,其余两个为空。目标是从 A 向 B 或 C 转移液体直到某个容器恰好含有目标体积 `c` 的液体为止。 #### 状态表示 为了便于编程实现,可以定义三元组 `(x, y)` 表示当前状态,其中: - `x`: 当前容器 B 中的水量; - `y`: 当前容器 C 中的水量。 初始状态为 `(0, 0)`,即两容器均为空;终止条件则为任意时刻满足其中一个容器内的水量等于目标值 `c`。 #### 关键操作析 根据目设定,在每次操作过程中允许执行以下几种基本动作: 1. 将 A 的全部内容倒入 B 或者 C 直至后者满载或者前者清空; 2. 把 B 的部或整体转移到 C 反之亦然; 3. 清空任一非零存量的目标容器重新开始尝试填充其他对象直至达成预期结果为止。 这些逻辑可以用简单的数学关系表达出来并结合实际物理约束加以限制从而形成完整的解决方案框架如下所示: ```cpp #include <iostream> #include <queue> using namespace std; struct State { int a, b; }; bool visited[201][201]; // Assuming max capacity is less than or equal to 200 liters. void bfs(int capA, int capB, int target) { queue<State> q; State start = {capA, 0}; q.push(start); memset(visited, false, sizeof(visited)); visited[start.a][start.b] = true; while (!q.empty()) { State current = q.front(); q.pop(); if (current.a == target || current.b == target){ cout << "Solution found!" << endl; break; } vector<State> nextStates{ {0, current.b}, // Empty A. {current.a, 0}, // Empty B. {min(capA, current.a + current.b), ((current.a + current.b) > capA)?((current.a + current.b)-capA):0 },// Pour from B into A until A full. {(current.a + current.b >= capB)?(current.a-(capB-current.b)):0 , min(capB,(current.a+current.b))} //Pour from A into B untill B full. }; for(auto s :nextStates ){ if(!visited[s.a][s.b]){ visited[s.a][s.b]=true; q.push(s); } } } } int main(){ int A,B,C; cin>>A>>B>>C; bfs(A,B,C); } ``` 上述代码实现了利用广度优先搜索(BFS)方法寻找最短路径到达特定状态的功能[^3]^。这里采用了队列数据结构存储待访问节点,并借助二维布尔数组记录已探索过的组合防止重复处理相同情形造成死循环现象发生。 #### 结论 综上所述,通过对这一典型例子的学习我们可以发现即使面对看似复杂棘手的实际应用场景只要合理运用诸如穷举之类的通用技术手段同样能够有效应对各种挑战获得满意的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值