3个水杯倒水问题(广度优先搜索)

本文介绍了一个经典的编程问题——三杯水问题,通过广度优先搜索算法解决如何在不同容量的三个水杯间倒水以达到特定目标状态的问题。提供了一段C语言实现代码,详细展示了算法的具体步骤。
部署运行你感兴趣的模型镜像

三个水杯

时间限制: 1000ms | 内存限制: 65535KB
难度: 4
描述
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
输入
第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
输出
每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
样例输入
2
6 3 1
4 1 1
9 3 2
7 1 1
样例输出
3
-1
来源
经典题目

广度优先搜索,貌似可以用A*,可惜不怎么会,以后试试。

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <string>

void check (int queue[], int x, int &tail) {
	for (int i = 0; i <= tail; ++ i) {
		if (queue[i] == x) {
			return;
		}
	}
	++ tail;
	//printf ("%d -> ", x);
	queue[tail] = x;
	return;
}

int getans(int to[], int i) {
	if (i == to[1] * 10000 + to[2] * 100 + to[3]) return 1;
	return 0;
}

int daoshui(int queue[], int &a, int &b, int to, int from[]) {
	if (a) {
		int t = from[to] - b;
		if (t > a) {
			b += a;
			a = 0;
			return 1;
		}
		else if (t) {
			a -= t;
			b += t;
			return 1;
		}
	}
	return 0;
}

int main () {
	int n;
	scanf ("%d", &n);
	while (n --) {
		int from[4];
		int to[4];
		scanf ("%d %d %d %d %d %d", &from[1], &from[2], &from[3], &to[1], &to[2], &to[3]);
		int head = 0;
		int tail = 0;
		int step = 0; 
		int queue[100000];
		queue[0] = from[1] * 10000;
		int flag = 0;
		if (queue[0] == to[1] * 10000 + to[2] * 100 + to[3]) {printf("0\n");continue;}
		while (head <= tail) {
			++ step;
			//printf("******************************%d************************************\n", step);
			int size = tail - head;
			for (int i = head; i <= head + size; ++ i) {
				int a, b, c;
				//printf ("|%d|", queue[i]);
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, a, b, 2, from)) {
					//printf ("(a -> b)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, a, c, 3, from)) {
					//printf ("(a -> c)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, b, a, 1, from)) {
					//printf ("(b -> a)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, b, c, 3, from)) {
					//printf ("(b -> c)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, c, a, 1, from)) {
					//printf ("(c -> a)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
				a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;
				if (daoshui(queue, c, b, 2, from)) {
					//printf ("(c -> b)");
					check(queue, a * 10000 + b * 100 + c, tail);
				}
			}
			//printf ("\n****************************************************************");
			head += size + 1;
			for (int i = head; i <= tail; ++ i) {
				if (getans(to, queue[i])) {
					printf ("%d\n", step);
					flag = 1;
					break;
				}
			}
			if (flag) break;
		}
		//printf("\n");
		if (!flag) printf ("-1\n");
	}
	return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

倒水问题通常是指有几个不同容量的杯子,通过倒来倒去达到某个目标水量的问题。一般可以使用广度优先搜索(BFS)算法来解决此类问题。 ### 算法思路 1. **状态表示**:使用一个元组来表示每个杯子中的水量。 2. **队列**:使用队列来进行广度优先搜索,队列中存储每个状态。 3. **访问集合**:使用集合来记录已经访问过的状态,避免重复搜索。 4. **操作**:定义所有可能的倒水操作,如从一个杯子倒到另一个杯子、将一个杯子倒满、将一个杯子倒空等。 5. **搜索**:从初始状态开始,不断进行操作,将新的状态加入队列,并记录到访问集合中,直到找到目标状态或队列为空。 ### 代码示例 ```cpp #include <iostream> #include <queue> #include <set> #include <vector> using namespace std; // 表示状态的结构体 struct State { vector<int> volumes; int steps; State(const vector<int>& v, int s) : volumes(v), steps(s) {} }; // 判断是否达到目标状态 bool isTarget(const State& state, int target) { for (int volume : state.volumes) { if (volume == target) { return true; } } return false; } // 倒水操作 vector<State> getNextStates(const State& state, const vector<int>& capacities) { vector<State> nextStates; int n = capacities.size(); // 倒空操作 for (int i = 0; i < n; ++i) { if (state.volumes[i] > 0) { vector<int> newVolumes = state.volumes; newVolumes[i] = 0; nextStates.emplace_back(newVolumes, state.steps + 1); } } // 倒满操作 for (int i = 0; i < n; ++i) { if (state.volumes[i] < capacities[i]) { vector<int> newVolumes = state.volumes; newVolumes[i] = capacities[i]; nextStates.emplace_back(newVolumes, state.steps + 1); } } // 倒水操作 for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != j && state.volumes[i] > 0 && state.volumes[j] < capacities[j]) { vector<int> newVolumes = state.volumes; int pour = min(state.volumes[i], capacities[j] - state.volumes[j]); newVolumes[i] -= pour; newVolumes[j] += pour; nextStates.emplace_back(newVolumes, state.steps + 1); } } } return nextStates; } // 广度优先搜索 int bfs(const vector<int>& capacities, int target) { queue<State> q; set<vector<int>> visited; vector<int> initialVolumes(capacities.size(), 0); State initialState(initialVolumes, 0); q.push(initialState); visited.insert(initialVolumes); while (!q.empty()) { State current = q.front(); q.pop(); if (isTarget(current, target)) { return current.steps; } vector<State> nextStates = getNextStates(current, capacities); for (const State& nextState : nextStates) { if (visited.find(nextState.volumes) == visited.end()) { q.push(nextState); visited.insert(nextState.volumes); } } } return -1; } int main() { vector<int> capacities = {3, 5}; int target = 4; int result = bfs(capacities, target); if (result != -1) { cout << "Minimum steps: " << result << endl; } else { cout << "No solution found." << endl; } return 0; } ``` ### 代码解释 1. **State结构体**:用于表示每个状态,包含每个杯子的水量和当前的步数。 2. **isTarget函数**:判断当前状态是否达到目标状态。 3. **getNextStates函数**:根据当前状态和杯子的容量,生成所有可能的下一步状态。 4. **bfs函数**:使用广度优先搜索算法,从初始状态开始搜索,直到找到目标状态或队列为空。 5. **main函数**:定义杯子的容量和目标水量,调用bfs函数进行搜索,并输出结果。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值