Advertisement
royalsflush

UVa 102 AC (2)

Mar 22nd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. long long qtd, minQtd;
  6. int p[3];
  7. int tmpP[3];
  8. char str[5] ="BCG";
  9. long long b[3][3];
  10.  
  11. long long count() {
  12.     long long sum=0;
  13.  
  14.     for (int i=0; i<3; i++)
  15.         for (int j=0; j<3; j++)
  16.             if (j!=tmpP[i]) sum+=b[i][j];
  17.     return sum;
  18. }
  19.  
  20. int main() {
  21.     while (scanf("%lld", &b[0][0])!=EOF) {
  22.         for (int i=0; i<3; i++)
  23.             for (int j=0; j<3; j++)
  24.                 if (i || j) scanf("%lld", &b[i][j]);
  25.        
  26.         for (int i=0; i<3; i++)
  27.             swap(b[i][1],b[i][2]);
  28.  
  29.         for (int i=0; i<3; i++)
  30.             tmpP[i]=i;
  31.         minQtd=-1;
  32.    
  33.         do {
  34.             qtd=count();
  35.  
  36.             if (qtd<minQtd || minQtd==-1) {
  37.                 for (int i=0; i<3; i++)
  38.                     p[i]=tmpP[i];
  39.                 minQtd=qtd;
  40.             }
  41.         } while (next_permutation(tmpP,tmpP+3));
  42.  
  43.         for (int i=0; i<3; i++)
  44.             printf("%c", str[p[i]]);
  45.         printf(" %lld\n", minQtd);
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement