daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct cut {
  2.     int r1, c1, r2, c2;
  3.     cut(int r1, int c1, int r2, int c2): r1(r1), c1(c1), r2(r2), c2(c2) {};
  4. };
  5.  
  6. vector<vector<char>> readField(ifstream & input, int rows, int columns) {
  7.    
  8.     vector<vector<char>> field = vector<vector<char>>(rows, vector<char>(columns, 0));
  9.    
  10.     for (int i = 0; i < rows; i++)
  11.     for (int j = 0; j < columns; j++) {
  12.         input >> field[i][j];
  13.     }
  14.    
  15.     return field;
  16.    
  17. };
  18.  
  19. // returns -1 if the cut is invalid, writes errors to standard output
  20. int score(vector<vector<char>> & field, vector<cut> & cuts, int l, int h) {
  21.  
  22.     int _score = 0;
  23.     vector<vector<bool>> isUsed(field.size(), vector<bool>(field[0].size(), false));
  24.     for (int k = 0; k < cuts.size(); k++) {
  25.        
  26.         cut cut = cuts[k];
  27.        
  28.         if ((cut.c2 - cut.c1) * (cut.r2 - cut.r1) > h) {
  29.             cout << "Slice #" << k << " is too big" << endl;
  30.             return -1;
  31.         }
  32.        
  33.         int t = 0, m = 0;
  34.         for (int i = cut.r1; i <= cut.r2; i++) {
  35.             for (int j = cut.c1; j <= cut.c2; j++) {
  36.                
  37.                 m += tolower(field[i][j]) == 'm';
  38.                 t += tolower(field[i][j]) == 't';
  39.                
  40.                
  41.                 if (isUsed[i][j]) {
  42.                     cout << "Slice #" << k << " caused intersection" << endl;
  43.                     return -1;
  44.                 } else {
  45.                     isUsed[i][j] = true;
  46.                     _score++;
  47.                 }
  48.             }
  49.         }
  50.        
  51.         if (t < l || m < l) {
  52.             cout << "Slice #" << k << " is not valid" << endl;
  53.             return -1;
  54.         }
  55.        
  56.     }
  57.    
  58.     return _score;
  59.    
  60. };
  61.  
  62. void testScorePositive() {
  63.    
  64.     /*
  65.      
  66.      3 5 1 6 TTTTT TMMMT TTTTT
  67.      
  68.      */
  69.    
  70.     int l = 1;
  71.     int h = 6;
  72.    
  73.     vector<vector<char>> field = {
  74.         {'T', 'T', 'T', 'T', 'T'},
  75.         {'T', 'M', 'M', 'M', 'T'},
  76.         {'T', 'T', 'T', 'T', 'T'}
  77.     };
  78.    
  79.     vector<cut> cuts = {cut(0, 0, 2, 1), cut(0, 2, 2, 2), cut(0, 3, 2, 4)};
  80.    
  81.     assert(score(field, cuts, l, h) == 15);
  82.    
  83. };
  84.  
  85. void testScoreNegative() {
  86.    
  87.     /*
  88.      
  89.      3 5 1 6 TTTTT TMMMT TTTTT
  90.      
  91.      */
  92.    
  93.     int l = 1;
  94.     int h = 6;
  95.    
  96.     vector<vector<char>> field = {
  97.         {'T', 'T', 'T', 'T', 'T'},
  98.         {'T', 'M', 'M', 'M', 'T'},
  99.         {'T', 'T', 'T', 'T', 'T'}
  100.     };
  101.    
  102.     vector<cut> cuts = {cut(0, 0, 2, 3), cut(0, 2, 2, 2), cut(0, 3, 2, 4)};
  103.    
  104.     score(field, cuts, l, h);
  105.    
  106. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top