算法题:三个瓶子(各个瓶子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";
}
}
}
}
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";
}
}
}
}
相关文章推荐
- 【算法】输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。
- 设计一个算法,求输入正整数A和正整数B的最小公倍数。
- 算法:输入一个十进制数,根据输入的数输出所对应的英文星期单词,若输入的数小于1或大于7,则输出ERROR。
- 输入一个整数数组,返回所有元素两两之差绝对值最小的值,O(n)算法
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。剑指Offer(32)
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 输入一个正整数数组,输入一个正整数数组,把数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 整数数组的定义,然后输入一个整数X,假定X不在这个数组,返回小于X位置的最大数目i而超过X位置的最小数目j
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 设计一个最优算法来查找一n个元素数组中的最大值和最小值。已知一种需要比较2n次的方法,请给一个更优的算法。
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 每天学习一算法系列(23)(写一个程序,要求功能,求出用1、2、5这三个数不同个数组合的和为100的组合数)
- 微软算法100道题-------输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。
- 算法例题-求输入一个整数,输出对应二进制中1的个数。
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- java编程基础篇--> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。
- 有如图所示的七巧板,试设计算法,使用至多4种不同颜色对七巧板进行涂色(每块七巧板一种颜色),要求相邻区域的颜色互补相同,打印输出所有可能的涂色方案。
- 输入一个高精度的正整数n,去掉其中任意s个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对所给的n和s,寻找一种方案使得剩下的数字组成的新数最小。
- ACM:每行输入一个正整数n,找出与它对应的比它大的最小的且它们对应的二进制中1的个数一样多的正整数.