Advertisement
royalsflush

UVa 102 CE

Mar 22nd, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. char order[]="BGC";
  6. int mat[3][3];
  7. char bestBot[5] = "GCB";
  8. int bot[3] = {0,1,2};
  9. long long sum=(1LL<<32);
  10.  
  11. void lowerString(int qtd) {
  12.     char tmp[5]="";
  13.    
  14.     for (int i=0; i<3; i++)
  15.         tmp[i]=order[bot[i]];
  16.     tmp[3]='\0';
  17.  
  18.     if (strcmp(tmp,bestBot)<0 || qtd<sum)
  19.         strcpy(bestBot,tmp);
  20. }
  21.  
  22. long long countExc() {
  23.     long long s=0;
  24.  
  25.     for (int i=0; i<3; i++)
  26.         for (int j=0; j<3; j++)
  27.             if (j!=bot[i]) s+=mat[i][j];
  28.     return s;
  29. }
  30.  
  31. int main() {
  32.     while (scanf("%d", &mat[0][0])!=EOF) {
  33.         sum=(1LL<<32);
  34.         strcpy(bestBot,"GCB");
  35.  
  36.         for (int i=0; i<3; i++)
  37.             for (int j=0; j<3; j++)
  38.                 if (i || j) scanf("%d", &mat[i][j]);
  39.  
  40.         do {
  41.             long long tmp=countExc();
  42.  
  43.             if (tmp<=sum) {
  44.                 lowerString(tmp);
  45.                 sum=tmp;
  46.             }
  47.         } while (next_permutation(bot, bot+3));
  48.  
  49.         printf("%s %lld\n", bestBot,sum);
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement