三个水杯
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
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
-
-
麻烦的算法:
-
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int va,vb,vc; int ta,tb,tc; bool visit[205][205][205]; struct node{ int a; int b; int c; int step; }; bool check(node t) { if(t.a==ta && t.b==tb && t.c==tc) return true; return false; } int BFS(int a,int b,int c) { queue<node>q; node t1,t2; t1.a=a; t1.b=b; t1.c=c; t1.step=0; q.push(t1); while(!q.empty()) { t1=q.front(); q.pop(); if(t1.b!=vb && t1.a)//a往b中倒水 { if(t1.a<=vb-t1.b) { t2.a=0; t2.b=t1.b+t1.a; t2.c=t1.c; } else { t2.a=t1.a-(vb-t1.b); t2.b=vb; t2.c=t1.c; } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } if(t1.c!=vc && t1.a) //a往c中倒水 { if(t1.a<=vc-t1.c) { t2.a=0; t2.b=t1.b; t2.c=t1.c+t1.a; } else { t2.a=t1.a-(vc-t1.c); t2.b=t1.b; t2.c=vc; } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } if(t1.c!=vc && t1.b) //b往c中倒水 { if(t1.b<=vc-t1.c) { t2.a=t1.a; t2.b=0; t2.c=t1.c+t1.b; } else { t2.a=t1.a; t2.b=t1.b-(vc-t1.c); t2.c=vc; } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } if(t1.a!=va && t1.b) // b往a中倒水 { if(t1.b<=va-t1.a) { t2.a=t1.a+t1.b; t2.b=0; t2.c=t1.c; } else { t2.a=va; t2.b=t1.b-(va-t1.a); t2.c=t1.c; } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } if(t1.a!=va && t1.c)//c往a倒水 { if(t1.c<=va-t1.a) { t2.a=t1.a+t1.c; t2.b=t1.b; t2.c=0; } else { t2.a=va; t2.b=t1.b; t2.c=t1.c-(va-t1.a); } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } if(t1.b!=vb && t1.c) //c 往b中倒水 { if(t1.c<=vb-t1.b) { t2.a=t1.a; t2.b=t1.b+t1.c; t2.c=0; } else { t2.a=t1.a; t2.b=vb; t2.c=t1.c-(vb-t1.b); } if(!visit[t2.a][t2.b][t2.c]) //判断这个状态出现过没有,如果出现过的话,那么就不用入队列了 { t2.step=t1.step+1; if(check(t2)) return t2.step; visit[t2.a][t2.b][t2.c]=true; q.push(t2); } } } return -1; } int main() { int T; scanf("%d",&T); while(T--) { memset(visit,false,sizeof(visit)); scanf("%d%d%d",&va,&vb,&vc); scanf("%d%d%d",&ta,&tb,&tc); if(va==ta && 0==tb && 0==tc) { printf("0\n"); continue; } visit[va][0][0]=true; printf("%d\n",BFS(va,0,0)); } return 0; }
-
-
比较简洁的代码:
-
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int va,vb,vc; int ta,tb,tc; bool visit[205][205][205]; struct node{ int v[3]; int steps; }init,targ; int del(node cup,int start,int end)//因为这个思想考虑的是两个杯子倒水,倒一次,两个杯子懂动, 另一个杯子不动 { if(cup.v[start] && cup.v[end]!=init.v[end]) //能倒水 { int sum=cup.v[start]+cup.v[end]; if(sum>=init.v[end]) cup.v[end]=init.v[end]; else cup.v[end]=sum; cup.v[start]=sum-cup.v[end]; if(!visit[cup.v[0]][cup.v[1]][cup.v[2]]) return visit[cup.v[0]][cup.v[1]][cup.v[2]]=true; } return false; } int BFS() { node cup={init.v[0],0,0,0},temp; queue<node>q; memset(visit,0,sizeof(visit)); visit[init.v[0]][0][0]=true; q.push(cup); while(!q.empty()) { temp=cup=q.front(); if(temp.v[0]==targ.v[0] && temp.v[1]==targ.v[1] && temp.v[2]==targ.v[2]) return temp.steps; q.pop(); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { temp=cup; if(1!=j && del(temp,i,j)) { ++temp.steps; q.push(temp); } } } return -1; } int main() { int T; scanf("%d",&T); while(T--) { memset(visit,false,sizeof(visit)); scanf("%d%d%d",&init.v[0],&init.v[1],&init.v[2]); scanf("%d%d%d",&targ.v,&targ.v+1,&targ.v+2); if(init.v[0]=targ.v[0] && 0==targ.v+1 && 0==targ.v+2) { printf("0\n"); continue; } visit[init.v[0]][0][0]= true ; printf("%d",BFS()); } return 0; }
-
第一行一个整数N(0<N<50)表示N组测试数据