HDU_1495(BFS)

数论做法
HDU 1495

BFS:

  1. 剪枝:奇数可乐肯定不能被平分
  2. 枚举6种操作a->c a->b b->a b->c c->a c->b
  3. 将每个杯子有多少可乐作为状态保存下来
  4. a == b && c == 0 或者a == c && b == 0 或者 b == c && a == 0 时则平分成功,
  5. 或者我们可以规定b是小杯子,c是大杯子,这样到最后肯定,平分到了b,c中,反过来规定也行
    即:
// 规定
if (n[2] > n[3]) { // 规定 b 杯子比 c杯子小, 
			swap(n[2],n[3]);
		}
// 退出状态
if (cur.v[1] == cur.v[3] && cur.v[2] == 0) { // 平分了,装在两2大杯子中 
			printf("%d\n",cur.step);
			return ;
		}

bfs:

#include <queue>
#include <iostream>
#include <string.h>

using namespace std;

int n[4];
int vis[102][102][102];
struct cup{
	int v[4];
	int step;
	cup(){}
	cup(int a,int b,int c,int s){
		v[1] = a;
		v[2] = b;
		v[3] = c;
		step = s;
	}
};

void pour(int a,int b,cup* t) { // 杯子a的可乐倒进杯子b 
	int sum = t->v[a] + t->v[b];
	if (sum >= n[b])  // 超出了b杯子的容量
		t->v[b] = n[b]; // 只能到把杯子倒满,剩下的不倒
	else 
		t->v[b] = sum;
	t->v[a] = sum - t->v[b]; // 原来a,b,的总量,减去现在b的总量
}
void bfs() {
	// 剪枝
	if (n[1] % 2) {
		printf("NO\n");
		return ;
	} 
	
	queue<cup> que;
	memset(vis,0,sizeof vis);
	
	que.push( cup(n[1],0,0,0) );
	vis[n[1]][0][0] = 1;
	
	while ( !que.empty() ) {
		cup cur = que.front();
		que.pop();

		if (cur.v[1] == cur.v[3] && cur.v[2] == 0) { // 平分了,装在两2大杯子中 
			printf("%d\n",cur.step);
			return ;
		} else if (cur.v[1] == cur.v[2] && cur.v[3] == 0) {
			printf("%d\n",cur.step);
			return ;
		}
		else if (cur.v[2] == cur.v[3] && cur.v[1] == 0) {
			printf("%d\n",cur.step);
			return ;
		}
		
		for (int i=1; i<4; i++) { // 枚举6种 倒 法 
			for (int j=1; j<4; j++) {
				if (i != j) { // 每个杯子倒水2中选择,不到给自己就行了
					cup temp = cur;
					pour(i,j,&temp);
					if( !vis[temp.v[1]][temp.v[2] ][temp.v[3]] ) {
						vis[temp.v[1]][temp.v[2]][temp.v[3]] = 1;
						temp.step += 1;
						que.push( temp );
					}
				}
			}
		}
	}
	printf("NO\n");
	return ;
}

int main() {
//	freopen("a.txt","r",stdin);
	while (scanf("%d%d%d",&n[1],&n[2],&n[3]) && n[1]) {
//		if (n[2] > n[3]) { // 规定 b 杯子比 c杯子小, 
//			swap(n[2],n[3]);
//		}
		bfs();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43580151/article/details/90473691