程序设计基础9 广度优先搜索BFS (2)

【题目描述】
  有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油。如果能就输出“yes”,并且说明最少倒几次,否则输出“no”。例如:10升油在10升的容器中,另有两个7升和3升的空容器,要求用这三个容器倒油,使得最后在abc三个容器中有一个刚好存有5升油,问最少的倒油次数是多少?(每次倒油,A容器倒到B容器,或者A内的油倒完,或者B容器倒满。
 10 7 3
(10 0 0)
(3 7 0):第一次
(3 4 3):第二次
(6 4 0):第三次
(6 1 3):第四次
(9 1 0):第五次
(9 0 1):第六次
(2 7 1):第七次
(2 5 3):第八次,出现5了。

Input

【输入格式】
  有多组测试数据。
  输入a,b,c, k四个正整数( 100 ≥ a > b > c≥1 , 1≤k< 100 )

Output

【输出格式】
  如果能得到k就输出两行。
  第一行“yes”,第二行为最少的次数
  否则输出“no”

Sample Input

10 7 3 5

Sample Output

yes
8

一,正确方法

    把每种倒法都模拟一遍,利用三维数组标记每倒完一次的状态,标记所以状态。这样就好用BSF了,当初没想到利用三维数组这种标记方式,一个杯子一个杯子地计状态,这是不对的。注意:倒0次不是no,是yes

#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
//到16:50
int a = 0;
int b = 0;
int c = 0;
int k = 0;
int flag[105][105][105] = { 0 };
struct Node {
	int a;
	int b;
	int c;
	int step;
}cur, start, temp;
int BFS(Node start) {
	queue<Node> que;
	que.push(start);
	flag[start.a][start.b][start.c] = 1;
	while (!que.empty()) {
		cur = que.front();
		if (cur.a == k || cur.b == k || cur.c == k) {
			printf("yes\n%d\n", cur.step);
			return 1;
		}
		que.pop();
		//v1->v2
		if (cur.a > 0 && cur.b < b) {
			int t = min(cur.a, b - cur.b);
			temp.a = cur.a - t;
			temp.b = cur.b + t;
			temp.c = cur.c;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
		//v1->v3
		if (cur.a > 0 && cur.c < c) {
			int t = min(cur.a, c - cur.c);
			temp.a = cur.a - t;
			temp.b = cur.b;
			temp.c = cur.c + t;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
		//v2->v1
		if (cur.b > 0 && cur.a < a) {
			int t = min(cur.b, a - cur.a);
			temp.a = cur.a + t;
			temp.b = cur.b - t;
			temp.c = cur.c;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
		//v2->v3
		if (cur.b > 0 && cur.c < c) {
			int t = min(cur.b, c - cur.c);
			temp.a = cur.a;
			temp.b = cur.b - t;
			temp.c = cur.c + t;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
		//v3->v1
		if (cur.c > 0 && cur.a < a) {
			int t = min(cur.c, a - cur.a);
			temp.a = cur.a + t;
			temp.b = cur.b;
			temp.c = cur.c - t;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
		//v3->v2
		if (cur.c > 0 && cur.b < b) {
			int t = min(cur.c, b - cur.b);
			temp.a = cur.a;
			temp.b = cur.b + t;
			temp.c = cur.c - t;
			temp.step = cur.step + 1;
			if (flag[temp.a][temp.b][temp.c] != 1) {
				que.push(temp);
				flag[temp.a][temp.b][temp.c] = 1;
			}
		}
	}
	return 0;
}
int main() {
	int answer = 0;
	while (~scanf("%d %d %d %d", &a, &b, &c, &k)) {
		memset(flag, 0, sizeof(flag));
		start.a = a;
		start.b = 0;
		start.c = 0;
		start.step = 0;
		///		answer = BFS(start);
		if (!BFS(start)) {
			printf("no\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/82496476