分酒问题
来源:互联网 发布:小米手机淘宝 编辑:程序博客网 时间:2024/04/29 10:39
三个水杯
- 描述
- 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
- 输入
- 第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态 - 输出
- 每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
- 样例输入
-
26 3 14 1 19 3 27 1 1
- 样例输出
-
3-1
#include<stdio.h>typedef struct Node{ int a; int b; int c; int step;}Node;Node start;Node temp;int store[110][110][110];int v1,v2,v3,e1,e2,e3;Node queen[10000] = {0,0,0,0};int front;int rear;void init(){ int i,j,k; for(i = 0;i<110;i++) for(j = 0;j<110;j++) for(k = 0;k<110;k++) store[i][j][k] = 0;}void BFS(){ front = -1; rear = -1; start.a = v1; start.b = 0; start.c = 0; start.step = 0; queen[++rear] = start; store[v1][0][0] = 1; while(front != rear) { start = queen[++front]; if(start.a == e1&&start.b == e2&&start.c == e3) { printf("%d\n",start.step); return ; } //1->2 if(start.a-(v2-start.b)>=0) { temp.a = start.a-(v2-start.b); temp.b = v2; temp.c = start.c; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = 0; temp.b = start.b + start.a; temp.c = start.c; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } //1->3 if(start.a-(v3-start.c)>0) { temp.a = start.a-(v3-start.c); temp.b = start.b; temp.c = v3; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = 0; temp.b = start.b; temp.c = start.c + start.a; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } //2->3 if(start.b-(v3-start.c)>0) { temp.a = start.a; temp.b = start.b-(v3-start.c); temp.c = v3; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = start.a; temp.b = 0; temp.c = start.b+start.c; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } //2->1 if(start.b-(v1-start.a)>0) { temp.a = v1; temp.b = start.b-(v1-start.a); temp.c = start.c; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = start.a+start.b; temp.b = 0; temp.c = start.c; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } //3->1 if(start.c-(v1-start.a)>0) { temp.a = v1; temp.b = start.b; temp.c = start.c-(v1-start.a); temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = start.a+start.c; temp.b = start.b; temp.c = 0; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } //3->2 if(start.c-(v2-start.b)>0) { temp.a = start.a; temp.b = v2; temp.c = start.c-(v2-start.b); temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } else { temp.a = start.a; temp.b = start.c+start.b; temp.c = 0; temp.step = start.step+1; if(store[temp.a][temp.b][temp.c] == 0) { store[temp.a][temp.b][temp.c] = 1; queen[++rear] = temp; } } } printf("-1\n"); return ;}int main(){ int n; scanf("%d",&n); while(n--) { scanf("%d%d%d%d%d%d",&v1,&v2,&v3,&e1,&e2,&e3); //初始化状态数组 init(); //利用队列,广度搜索 BFS(); }}
阅读全文
0 0
- 分酒问题
- 分酒问题
- 分酒问题
- 分酒问题
- 分酒问题(摘录)
- 关于分酒的问题
- 一个分酒问题的求解
- 分酒问题与JOJ 1200 Jugs
- 海盗分酒(数学问题)
- 波瓦松分酒问题的分析
- 分酒问题(BFS或DFS)
- 海盗分金块问题
- 水手分椰子问题
- 海盗分金问题
- 分球问题
- 分盐问题
- 分球问题
- 海盗分金问题
- DFS:112. Path Sum
- 5、Longest Palindromic Substring
- 杂七杂八什么都有(一)
- 基于React的仿QQ音乐(移动端)
- 第三课 排序检索数据
- 分酒问题
- c++ 派生类和基类之间的特殊关系
- 特殊函数谈论 e^x,exp(x),分段函数
- 从JavaScript学习设计模式(Iterator)
- Spark及其核心概念理解
- Linux的进程互斥机制
- ReadMe
- 数据仓库
- jindutiao