-
描述
-
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
-
输入
-
第一行一个整数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
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class node { int v1,v2,v3,step; public node(int v1,int v2,int v3,int step) { this.v1=v1; this.v2=v2; this.v3=v3; this.step=step; } } public class Main { static int v1,v2,v3,vv1,vv2,vv3; static boolean vis[][][]; public static int bfs() { vis=new boolean[110][110][110]; Queue<node>queue=new LinkedList<node>(); node first=new node(v1,0,0,0); queue.add(first); while(!queue.isEmpty()) { first=queue.remove(); vis[first.v1][first.v2][first.v3]=true; if(first.v1==vv1&&first.v2==vv2&&first.v3==vv3) { return first.step; } if(first.v1>0&&first.v2<v2) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v1>(v2-next.v2)?(v2-next.v2):next.v1; next.v1-=x; next.v2+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } if(first.v1>0&&first.v3<v3) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v1>(v3-next.v3)?(v3-next.v3):next.v1; next.v1-=x; next.v3+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } if(first.v2>0&&first.v3<v3) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v2>(v3-next.v3)?(v3-next.v3):next.v2; next.v2-=x; next.v3+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } if(first.v2>0&&first.v1<v1) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v2>(v1-next.v1)?(v1-next.v1):next.v2; next.v2-=x; next.v1+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } if(first.v3>0&&first.v1<v1) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v3>(v1-next.v1)?(v1-next.v1):next.v3; next.v3-=x; next.v1+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } if(first.v3>0&&first.v2<v2) { node next=new node(first.v1, first.v2, first.v3, first.step); int x=next.v3>(v2-next.v2)?(v2-next.v2):next.v3; next.v3-=x; next.v2+=x; if(!vis[next.v1][next.v2][next.v3]) { vis[next.v1][next.v2][next.v3]=true; next.step++; queue.add(next); } } } return -1; } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int cases=scanner.nextInt(); while(cases--!=0) { v1=scanner.nextInt(); v2=scanner.nextInt(); v3=scanner.nextInt(); vv1=scanner.nextInt(); vv2=scanner.nextInt(); vv3=scanner.nextInt(); if(v1<vv1+vv2+vv3) { System.out.println("-1");continue; } System.out.println(bfs()); } } }
-
第一行一个整数N(0<N<50)表示N组测试数据
隐式图搜索 三个水杯 典型题目
最新推荐文章于 2018-04-30 20:47:15 发布