Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bits/stdc++.h>
  4.  
  5. #define ll long long
  6. using namespace std;
  7.  
  8. int n, m;
  9. vector<string> s;
  10. int x, y;
  11.  
  12. int lb[256], rb[256];
  13. vector<pair<int, int>> pt;
  14.  
  15. bool visited[1001][1001];
  16.  
  17. void floodfill(int r, int c, char ch, int lb, int rb, int u, int d) {
  18.   static int dx[] = {0, -1, 0, 1};
  19.   static int dy[] = {1, 0, -1, 0};
  20.   //fprintf(stderr, "floodfilling %d %d with %c , lr = (%d, %d), ud = (%d, %d)\n",
  21.   //r, c, ch, lb, rb, u, d);
  22.  
  23.   if (visited[r][c] || (isalpha(s[r][c]) && tolower(s[r][c]) != ch)) return;
  24.   visited[r][c] = 1;
  25.   if (s[r][c] == '.') s[r][c] = ch;
  26.  
  27.   for (int i = 0; i < 4; i++) {
  28.     int xx = r + dx[i], yy = c + dy[i];
  29.     if (u <= xx && xx <= d && lb <= yy && yy <= rb) {
  30.       floodfill(xx, yy, ch, lb, rb, u, d);
  31.     }
  32.   }
  33. }
  34. void fillbound(int u, int d, int l, int r) {
  35.   // for each point. let it fill its own row , then 'floodfill' according to its own width
  36.   for (auto p : pt) {
  37.       int xx = p.second, yy = p.first;
  38.       if (u <= xx && xx <= d && l <= yy && yy <= r) {
  39.           int ty = yy - 1;
  40.           char ch = tolower(s[xx][yy]);
  41.           while (ty >= l && !isalpha(s[xx][ty])) {
  42.             s[xx][ty] = ch;
  43.             ty--;
  44.           }
  45.           int ly = ty + 1;
  46.           ty = yy + 1;
  47.           while (ty <= r && !isalpha(s[xx][ty])) {
  48.             s[xx][ty] = ch;
  49.             ty++;
  50.           }
  51.           int ry = ty - 1;
  52.           lb[ch] = ly;
  53.           rb[ch] = ry;
  54.       }
  55.   }
  56.   // now floodfill
  57.   for (auto p : pt) {
  58.       int xx = p.second, yy = p.first;
  59.  
  60.       if (u <= xx && xx <= d && l <= yy && yy <= r) {
  61.         char ch = tolower(s[xx][yy]);
  62.         floodfill(xx, yy, ch, lb[ch], rb[ch], u, d);
  63.       }
  64.   }
  65.  
  66. }
  67. int main() {
  68.   cin >> n;
  69.   cin >> m;
  70.   s.resize(n);
  71.   cin.ignore(1000, '\n');
  72.   for (int i = 0 ; i < n; i++) {
  73.     cin >> s[i];
  74.     for (int j = 0; j < m; j++) {
  75.       if (isalpha(s[i][j])) {
  76.         if (s[i][j] == 'A') {
  77.           x= i, y = j;
  78.         } else {
  79.           pt.push_back({j, i});
  80.         }
  81.       }
  82.     }
  83.   }
  84.   sort(pt.begin(), pt.end());
  85.   // fix a base
  86.  
  87.   int maxarea = 1;
  88.   int ansl = y;
  89.   int ansr = y;
  90.   int anstop = x;
  91.   int ansbase = x;
  92.  
  93.   for (int base = x; base < n; base++) {
  94.     // find l and r range of base
  95.     int lrange = 0, rrange = m - 1;
  96.     for (auto p : pt) {
  97.       int xx = p.second, yy = p.first;
  98.       if (x <= xx && xx <= base) {
  99.         if (yy <= y) lrange = max(yy + 1, lrange);
  100.         else rrange = min(yy - 1, rrange);
  101.       }
  102.     }
  103.     if (lrange > rrange) continue;
  104.    
  105.     // find largest on top within the range
  106.    
  107.     vector<pair<int, int> > above;
  108.     for (auto p : pt) {
  109.       int xx = p.second, yy = p.first;
  110.       if (xx >= x) continue;
  111.       if (yy < lrange || yy > rrange) continue;
  112.       above.push_back({yy,xx});
  113.     }
  114.     above.push_back({0, -1});
  115.     //sort(q.begin(), q.end());
  116.     //reverse(pright.begin(), pright.end());
  117.    
  118.    
  119.     for (auto p : above) {
  120.       int x1 = p.second, y1 = p.first;
  121.       int top = x1 + 1;
  122.       for (auto q : above) {
  123.         int x2 = q.second, y2 = q.first;
  124.         if (x2 >= top) {
  125.           if (y2 <= y) lrange = max(y2 + 1, lrange);
  126.           else rrange = min(y2 - 1, rrange);
  127.         }
  128.       }
  129.       if (lrange > rrange) continue; // cannot attain
  130.      
  131.       int area = (rrange - lrange + 1) * (base - top + 1);
  132.      
  133.       if (area > maxarea) {
  134.         maxarea = area;
  135.         ansbase = base;
  136.         anstop = top;
  137.         ansl = lrange;
  138.         ansr = rrange;
  139.       }
  140.  
  141.     }
  142.   }
  143.   pt.push_back({y, x});
  144.   fillbound(anstop, ansbase, ansl, ansr);
  145.   fillbound(anstop, ansbase, 0, ansl - 1);
  146.   fillbound(anstop, ansbase, ansr + 1, m - 1);
  147.   fillbound(0, anstop - 1, 0, m - 1);
  148.   fillbound(ansbase + 1, n - 1, 0, m - 1);
  149.   //fprintf(stderr, "area = %d, row: %d, %d, col: %d, %d\n", maxarea, anstop, ansbase, ansl, ansr);
  150.  
  151.   for (int i = 0; i < n; i++) {
  152.     for (int j= 0; j < m ; j++) {
  153.       cout << s[i][j];
  154.     }
  155.     cout << endl;
  156.   }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement