Advertisement
royalsflush

UVa 102 AC

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