csansoon

P8.07 P92844 Bounding rectangle

Nov 29th, 2018
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<char> Row;
  6. typedef vector<Row> Rectangle;
  7.  
  8. void minimal_dimensions(char c, const Rectangle& r, int& fils, int& cols){
  9.     fils = 1;
  10.     cols = 1;
  11.     int imin, imax, jmin, jmax;
  12.     bool fora = false;
  13.     int k = 0;
  14.     while (not fora and k < r.size()){
  15.         int j = 0;
  16.         while (not fora and j < r[0].size()){
  17.             if (r[k][j] == c){
  18.                 imin = k;
  19.                 fora = true;
  20.             }
  21.             ++j;
  22.         }
  23.         ++k;
  24.     }
  25.     fora = false;
  26.     int m = 0;
  27.     while (not fora and m < r[0].size()){
  28.         int i = 0;
  29.         while (not fora and i < r.size()){
  30.             if (r[i][m] == c){
  31.                 jmin = m;
  32.                 fora = true;
  33.             }
  34.             ++i;
  35.         }
  36.         ++m;
  37.     }
  38.     fora = false;
  39.     int i = r.size()-1;
  40.     while (not fora and i >= 0){
  41.         int j = r[0].size()-1;
  42.         while (not fora and j >= 0){
  43.             if (r[i][j] == c){
  44.                 imax = i;
  45.                 fora = true;
  46.             }
  47.             --j;
  48.         }
  49.         --i;
  50.     }    
  51.     fora = false;
  52.     int j = r[0].size()-1;
  53.     while (not fora and j >= 0){
  54.         int l = r.size()-1;
  55.         while (not fora and l >= 0 ){
  56.             if (r[l][j] == c){
  57.                 jmax = j;
  58.                 fora = true;
  59.             }
  60.             --l;
  61.         }
  62.         --j;
  63.     }
  64.     cols += jmax - jmin;
  65.     fils += imax - imin;
  66. }
  67.  
  68. int main() {
  69.         int fils, cols;
  70.         cin >> fils >> cols;
  71.         char c;
  72.         Rectangle r(fils, vector<char>(cols));
  73.         for (int i = 0; i < fils; ++i) {
  74.                 for (int j = 0; j < cols; ++j) cin >> r[i][j];
  75.         }
  76.         cin >> c;
  77.         minimal_dimensions(c, r, fils, cols);
  78.         cout << "Columns: " << cols << ". Rows: " << fils << endl;
  79. }
  80.  
  81. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment