Basic programming 9 breadth first search BFS (2)

[Title Description]
There are three containers with capacity of a, B and C (a > b > C). At the beginning, a was filled with oil. Now, I asked if I could only use three containers of abc to measure k liters of oil. If you can output "yes", and state the minimum number of times, otherwise output "no". For example: 10 liter oil in 10 liter container, and two 7 liter and 3 liter empty containers. It is required to use these three containers to pour oil, so that one of the three containers in abc just has 5 liter oil. What is the minimum number of times to pour oil? (each time the oil is poured, container a is poured into container B, or the oil in container a is poured out, or container B is filled.
 10 7 3
(10 0 0)
(370): first time
(3 4 3): second time
(6 40): the third time
(6 13): the fourth time
(9 10): the fifth time
(901): the sixth time
(27 1): the seventh time
(23): for the eighth time, there are five.

Input

[input format]
There are multiple sets of test data.
Input four positive integers a, B, c, k (100 ≥ a > b > C ≥ 1, 1 ≤ K < 100)

Output

[output format]
If we can get k, we will output two lines.
The first line is "yes", and the second line is the least number of times
Otherwise output "no"

Sample Input

 

10 7 3 5

Sample Output

 

yes
8

1, The right way

Each inversion is simulated once, and the state of each inversion is marked with three-dimensional array, so the state is marked. So it's easy to use BSF. I didn't expect to use three-dimensional array as a way of marking. It's wrong to count the state of a cup and a cup. Note: it's not no, it's yes

#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
//To 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;
}

 

 

Posted by wispas on Tue, 31 Dec 2019 09:45:43 -0800