您的位置:首页 > 其它

算法题:三个瓶子(各个瓶子R\G\B数目不同)+输入一行9个数(三三对应瓶子),求一个瓶子对应一种颜色的最小转移数

2017-03-13 17:18 696 查看
package com.example.chantest;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Scanner;

public class ArrayBottle {
private static Map<Integer,List<Integer>> bottle = new HashMap<>();
private static Map<Integer,List<Integer>> resultBottle = new HashMap<>();
//private static List<String> mark = new ArrayList<String>();//标志检验瓶子是否已被存放
private static String king[] = new String[3];
private static List<Integer> test = new ArrayList<Integer>();
private static Scanner input = new Scanner(System.in);
private static int steps;
public static void main(String[] args){
init();
login();
for(int k =0;k<3;k++){
System.out.print(king[k]);
}
System.out.print(" "+steps);

}

//把三个瓶子的数目初始化到链表,Bule代表蓝,green代表绿,clear代表透明,i代表第i个瓶子,j代表颜色,0代表B,1代表G,2代表C
private static void init(){
final int bottles = 3;
final int num = 3;
for(int i = 0;i<bottles;i++){
bottle.put(i,new ArrayList<Integer>());
resultBottle.put(i,new ArrayList<Integer>());
//mark.add("A");

for(int j =0;j<num;j++){
int kk= input.nextInt();
bottle.get(i).add(kk);
}
}
System.out.println(bottle.toString());
}

private static void login(){
for(int j=0;j<3;j++){
if(bottle.get(0).get(j)>=bottle.get(1).get(j) && bottle.get(0).get(j)>=bottle.get(2).get(j)){//遍历比较同颜色不同瓶子的数目
//System.out.print("第一种情况");
if(MaxNum(0,j) && !test.contains(0)){//比较同瓶子不同颜色的数目,
for(int i=0;i<3;i++){
resultBottle.get(0).add(bottle.get(i).get(j));//转移不同瓶子相同颜色
}
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(0,j);
test.add(0);//标志位设为0,代表瓶子还没装
}
else if(bottle.get(1).get(j)>=bottle.get(2).get(j)){//如果不是该瓶子最多的颜色一种,移到第二多的瓶子
boolean aa = MaxNum(1,j) && !test.contains(1);
boolean bb = !MaxNum(1,j) && test.contains(2) && !test.contains(1);
if(aa || bb){
for(int i=0;i<3;i++){
resultBottle.get(1).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(2).get(j)+steps;
index(1,j);
test.add(1);
}else if(!test.contains(2)){
for(int i=0;i<3;i++){
resultBottle.get(2).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(1).get(j)+steps;
index(2,j);
test.add(2);
}

}else if(bottle.get(1).get(j)<=bottle.get(2).get(j)){
boolean a1 = MaxNum(1,j) && !test.contains(1);
boolean b1 = !MaxNum(1,j) && test.contains(2) && !test.contains(1);
if(a1 || b1){
for(int i=0;i<3;i++){
resultBottle.get(2).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(1).get(j)+steps;
index(2,j);
test.add(2);
}else if(!test.contains(1)){
for(int i=0;i<3;i++){
resultBottle.get(1).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(2).get(j)+steps;
index(1,j);
test.add(1);
}
}
}else if(bottle.get(1).get(j)>=bottle.get(0).get(j) && bottle.get(1).get(j)>=bottle.get(2).get(j)){
//System.out.print("第二种情况");
if(MaxNum(1,j) && !test.contains(1)){
for(int i=0;i<3;i++){
resultBottle.get(1).add(bottle.get(i).get(j));//转移不同瓶子相同颜色
}
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(1,j);
test.add(1);
}else if(bottle.get(0).get(j)>=bottle.get(2).get(j)){
boolean kk = MaxNum(0,j) && !test.contains(0);
boolean pp = !MaxNum(0,j) && test.contains(2) && !test.contains(0);
if(kk || pp){
for(int i=0;i<3;i++){
resultBottle.get(0).add(bottle.get(i).get(j));
}
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(0,j);
test.add(0);
}else if(!test.contains(2)){
for(int i=0;i<3;i++){
resultBottle.get(2).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(1).get(j)+steps;
index(2,j);
test.add(2);
}
}else if(bottle.get(0).get(j)<=bottle.get(2).get(j)){
boolean k1 = MaxNum(2,j) && !test.contains(2);
boolean p1 = !MaxNum(2,j) && test.contains(0) && !test.contains(2);
if(k1 || p1){
for(int i=0;i<3;i++){
resultBottle.get(2).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(1).get(j)+steps;
index(2,j);
test.add(2);
}else if(!test.contains(0)){
for(int i=0;i<3;i++){
resultBottle.get(0).add(bottle.get(i).get(j));
}
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(0,j);
test.add(0);
}
}
}else if(bottle.get(2).get(j)>bottle.get(0).get(j) && bottle.get(2).get(j)>bottle.get(1).get(j)){
//System.out.print("第三种情况");
//System.out.println("MaxNum(2,j): "+MaxNum(2,j));
if(MaxNum(2,j) && !test.contains(2)){
for(int i=0;i<3;i++){
resultBottle.get(2).add(bottle.get(i).get(j));//转移不同瓶子相同颜色
}
System.out.print("resultBottle>>>"+resultBottle.get(2).toString());
steps=bottle.get(0).get(j)+bottle.get(1).get(j)+steps;
index(2,j);
test.add(2);
}else if(bottle.get(0).get(j)>=bottle.get(1).get(j)){
boolean tt = MaxNum(0,j) && !test.contains(0);
boolean rr = !MaxNum(0,j) && test.contains(1) && !test.contains(0);
if(tt || rr){
for(int i=0;i<3;i++){
resultBottle.get(0).add(bottle.get(i).get(j));
}
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(0,j);
test.add(0);
}else if(!test.contains(1)){
for(int i=0;i<3;i++){
resultBottle.get(1).add(bottle.get(i).get(j));
}
steps=bottle.get(0).get(j)+bottle.get(2).get(j)+steps;
index(1,j);
test.add(1);
}
}else if(bottle.get(0).get(j)<=bottle.get(1).get(j)){
boolean t1 = MaxNum(1,j) && !test.contains(1);
boolean r1 = !MaxNum(1,j) && test.contains(0) && !test.contains(1);
if(t1 || r1){
for(int i=0;i<3;i++){
resultBottle.get(1).add(bottle.get(i).get(j));
}
System.out.print("resultBottle>>>"+resultBottle.get(1).toString());
steps=bottle.get(0).get(j)+bottle.get(2).get(j)+steps;
index(1,j);
test.add(1);
}else if(!test.contains(0)){
for(int i=0;i<3;i++){
resultBottle.get(0).add(bottle.get(i).get(j));
}
System.out.print("resultBottle>>>"+resultBottle.get(0).toString());
steps=bottle.get(1).get(j)+bottle.get(2).get(j)+steps;
index(0,j);
test.add(0);
}
}
}
}//2

}//1

private static boolean MaxNum(int i,int j){
if(i==0){
if(j==0){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j+2)){
return true;
}
}
if(j==1){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j-1)){
return true;
}
}
if(j==2){
if(bottle.get(i).get(j)>=bottle.get(i).get(j-1) && bottle.get(i).get(j)>=bottle.get(i).get(j-2)){
return true;
}

   }

}
if(i==1){
if(j==0){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j+2)){
return true;
}
}
if(j==1){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j-1)){
return true;
}
}
if(j==2){
if(bottle.get(i).get(j)>=bottle.get(i).get(j-1) && bottle.get(i).get(j)>=bottle.get(i).get(j-2)){
return true;
}

   }

}
if(i==2){
if(j==0){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j+2)){
return true;
}
}
if(j==1){
if(bottle.get(i).get(j)>=bottle.get(i).get(j+1) && bottle.get(i).get(j)>=bottle.get(i).get(j-1)){
return true;
}
}
if(j==2){
if(bottle.get(i).get(j)>=bottle.get(i).get(j-1) && bottle.get(i).get(j)>=bottle.get(i).get(j-2)){
return true;
}

   }

}
return false;
}

private static void index(int i,int j){
if(i==0){
if(j==0){
king[0]="B";
}else if(j==1){
king[0]="G";
}else{
king[0]="C";
}
}else if(i == 1){
if(j==0){
king[1]="B";
}else if(j==1){
king[1]="G";
}else{
king[1]="C";
}
}else{
if(j==0){
king[2]="B";
}else if(j==1){
king[2]="G";
}else{
king[2]="C";
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法
相关文章推荐