题目的意思是给你三个箱子,1号,2号,3号每个箱子装了三种颜色的瓶子(假设为球,便于记忆)其中颜色分别为brown,green, clear;
比如:1 2 3 4 5 6 7 8 9
就意味着:
1号 2号 3号
B G C B G C B G C
1 2 3 4 5 6 7 8 9
做法就是你要让移动次数最少,多种方案时按照字母顺序求解输出
无论你怎么移动最优的算法中有一种颜色是不会移动的,让这三个箱子不动的部分的和最大时,就是移动的部分最少的情况.
/*
Name: uva 102 ebp-ac
Copyright: angieeve
Author: zj
Date: 07-04-13 13:01
Description: greedy
*/
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define num 9
#define way 6
#define max 11
#define inf 0xffffff
int data[max];
struct ways{
int a,b,c;
string color;
}ways[]={
{1,5,9,"BGC"},{1,8,6,"BCG"},{4,2,9,"GBC"},
{4,8,3,"CBG"},{7,2,6,"GCB"},{7,5,3,"CGB"}
};
int main(){
int maxv,count,sum;
string str;
while(scanf("%d",&data[1])!=EOF){
maxv=-inf;
count=data[1];
for(int i=2;i<=num;i++){
scanf("%d",&data[i]);
count+=data[i];
}
//count 用来存放所有的bottle的总数
//for(int i=1;i<=num;i++) cout<<"datai="<<data[i]<<endl;
//data[i]存放输入数据
for(int i=0;i<way;i++){
sum=data[ways[i].a]+data[ways[i].b]+data[ways[i].c];
//
//cout<<"sum"<<sum<<endl;sum是用来求解每种方式下的所移动的步数
//
if(maxv<sum){maxv=sum;str=ways[i].color;}
else if (maxv==sum&&str>ways[i].color) str=ways[i].color;
//cout<<maxv<<"maxv"<<endl;maxv更新最多的步数
// cout<<count<<"count"<<endl;
}
cout<<str<<' '<<count-maxv<<endl;
}
return 0;
}