Guest User

naive.cpp

a guest
May 19th, 2018
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #pragma GCC diagnostic ignored "-Wunused-result"
  2. #include <stdio.h>
  3. #include <bits/stdc++.h>
  4.  
  5. // Generate random test
  6. std::vector<std::string> gen_test(const int nRows, const int nCols, const int nRects) {
  7.     std::vector<std::string> arr(nRows, std::string(nCols, '.'));
  8.     for (int id = 0; id < nRects; ++id) {
  9.         int r1 = std::rand() % nRows;
  10.         int r2 = std::rand() % nRows;
  11.         int c1 = std::rand() % nCols;
  12.         int c2 = std::rand() % nCols;
  13.         if (r1 > r2) std::swap(r1, r2);
  14.         if (c1 > c2) std::swap(c1, c2);
  15.         for (int c = c1; c <= c2; ++c) arr[r1][c] = arr[r2][c] = '#';
  16.         for (int r = r1; r <= r2; ++r) arr[r][c1] = arr[r][c2] = '#';
  17.     }
  18.     return arr;
  19. }
  20.  
  21. // Generate random number in [a, b]
  22. int rand_number(int a, int b) {
  23.     return std::rand() % (b - a + 1) + a;
  24. }
  25.  
  26. // Generate set of random tests
  27. void gen(int nTests) {
  28.     std::srand(std::time(0));
  29.     for (int i = 0; i < nTests; ++i) {
  30.         const int nRows = rand_number(5, 20);
  31.         const int nCols = rand_number(5, 20);
  32.         const int nRects = rand_number(0, 20);
  33.         auto arr = gen_test(nRows, nCols, nRects);
  34.         std::cout << nRows << " " << nCols << "\n";
  35.         for (auto& row : arr) {
  36.             std::cout << row << "\n";
  37.         }
  38.     }
  39.     std::exit(0);
  40. }
  41.  
  42. int main() {
  43.     //gen(1000);
  44.     int nRows, nCols;
  45.     while (scanf("%d %d", &nRows, &nCols) == 2) {
  46.         std::vector<std::string> arr(nRows);
  47.         for (int i = 0; i < nRows; ++i) {
  48.             char buf[351];
  49.             scanf("%350s", buf);
  50.             arr[i] = buf;
  51.         }
  52.        
  53.         std::vector<std::vector<int>> sum(nRows+1, std::vector<int>(nCols+1));
  54.         for (int row = nRows-1; row >= 0; --row) {
  55.             for (int col = nCols-1; col >= 0; --col) {
  56.                 sum[row][col] = (arr[row][col] == '#') + sum[row+1][col] + sum[row][col+1] - sum[row+1][col+1];
  57.             }
  58.         }
  59.        
  60.         std::function<int(int, int, int, int)> sum_in_rect = [&](const int r1, const int c1, const int r2, const int c2) {
  61.             return sum[r1][c1] - sum[r2+1][c1] - sum[r1][c2+1] + sum[r2+1][c2+1];
  62.         };
  63.        
  64.         std::function<bool(int, int, int, int)> is_rect = [&](const int r1, const int c1, const int r2, const int c2) {
  65.             return sum_in_rect(r1, c1, r2, c2) - sum_in_rect(r1+1, c1+1, r2-1, c2-1) == 2*(r2-r1+c2-c1);
  66.         };
  67.        
  68.         long long answ = 0;
  69.         for (int r1 = 0; r1 < nRows-2; ++r1) {
  70.             for (int c1 = 0; c1 < nCols-2; ++c1) {
  71.                 for (int r2 = r1+2; r2 < nRows; ++r2) {
  72.                     for (int c2 = c1+2; c2 < nCols; ++c2) {
  73.                         answ += is_rect(r1, c1, r2, c2);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         printf("%lld\n", answ);
  79.     }
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment