fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class VegetableField {
  7. public: int biggestPlot(vector <string>);
  8. };
  9.  
  10. template <class T> inline void checkmax( T &a, T b) { if(a<b) a=b; }
  11.  
  12. int cnt[256];
  13.  
  14. int VegetableField::biggestPlot(vector <string> field) {
  15. /*
  16.   In this problem we are asked to find a rectangle, which is
  17.   formed with (at most two) different characters and also have
  18.   the biggest area. As the given plot is relatively small (20x20),
  19.   we can just generate all possible rectangles and choose the
  20.   biggest one.
  21.  
  22.   This bruteforce solution has time complexity of O(R^3+C^3), where
  23.   R and C are the numbers of rows and columns respectively.
  24.   */
  25. int i, j, k, l, m, n;
  26. int R = field.size(), C = field[0].size();
  27. int answer = 0, diff;
  28. for(int i=0; i<R; i++) for(int j=0; j<C; j++) {
  29. /*
  30.   These two loops will get all the top-left possible corners
  31.   of all rectangles.
  32.   */
  33. for(int k=i; k<R; k++) for(int l=j; l<C; l++) {
  34. /*
  35.   These two loops will get all the down-right possible
  36.   corners of all rectangles.
  37.  
  38.   Now the rectangle is (i, j), (k, l), where the first
  39.   pair is top-left corner and the other one -
  40.   the bottom-right.
  41.   */
  42. diff=0;
  43. memset(cnt, 0, sizeof(cnt));
  44. for(m=i; m<=k; ++m) for(int n=j; n<=l; ++n) {
  45. /*
  46.   Counting the different number of characters is easy
  47.   through the global array cnt[].
  48.   */
  49. if(++cnt[(int)field[m][n]]==1) {
  50. if(++diff>2) break;
  51. }
  52. }
  53. /*
  54.   The rectangle is proper iff the number of different
  55.   chars is at most 2. If so update if it is bigger than
  56.   the last one.
  57.   */
  58. if(diff<=2) {
  59. checkmax(answer, (k-i+1)*(l-j+1));
  60. }
  61. }
  62. }
  63. return answer;
  64. }
  65.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘int VegetableField::biggestPlot(std::vector<std::basic_string<char> >)’:
prog.cpp:25:9: warning: unused variable ‘i’ [-Wunused-variable]
     int i, j, k, l, m, n;
         ^
prog.cpp:25:12: warning: unused variable ‘j’ [-Wunused-variable]
     int i, j, k, l, m, n;
            ^
prog.cpp:25:15: warning: unused variable ‘k’ [-Wunused-variable]
     int i, j, k, l, m, n;
               ^
prog.cpp:25:18: warning: unused variable ‘l’ [-Wunused-variable]
     int i, j, k, l, m, n;
                  ^
prog.cpp:25:24: warning: unused variable ‘n’ [-Wunused-variable]
     int i, j, k, l, m, n;
                        ^
/usr/lib/gcc/i486-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty