Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- int n, m;
- vector<string> s;
- int x, y;
- int lb[256], rb[256];
- vector<pair<int, int>> pt;
- bool visited[1001][1001];
- void floodfill(int r, int c, char ch, int lb, int rb, int u, int d) {
- static int dx[] = {0, -1, 0, 1};
- static int dy[] = {1, 0, -1, 0};
- //fprintf(stderr, "floodfilling %d %d with %c , lr = (%d, %d), ud = (%d, %d)\n",
- //r, c, ch, lb, rb, u, d);
- if (visited[r][c] || (isalpha(s[r][c]) && tolower(s[r][c]) != ch)) return;
- visited[r][c] = 1;
- if (s[r][c] == '.') s[r][c] = ch;
- for (int i = 0; i < 4; i++) {
- int xx = r + dx[i], yy = c + dy[i];
- if (u <= xx && xx <= d && lb <= yy && yy <= rb) {
- floodfill(xx, yy, ch, lb, rb, u, d);
- }
- }
- }
- void fillbound(int u, int d, int l, int r) {
- // for each point. let it fill its own row , then 'floodfill' according to its own width
- for (auto p : pt) {
- int xx = p.second, yy = p.first;
- if (u <= xx && xx <= d && l <= yy && yy <= r) {
- int ty = yy - 1;
- char ch = tolower(s[xx][yy]);
- while (ty >= l && !isalpha(s[xx][ty])) {
- s[xx][ty] = ch;
- ty--;
- }
- int ly = ty + 1;
- ty = yy + 1;
- while (ty <= r && !isalpha(s[xx][ty])) {
- s[xx][ty] = ch;
- ty++;
- }
- int ry = ty - 1;
- lb[ch] = ly;
- rb[ch] = ry;
- }
- }
- // now floodfill
- for (auto p : pt) {
- int xx = p.second, yy = p.first;
- if (u <= xx && xx <= d && l <= yy && yy <= r) {
- char ch = tolower(s[xx][yy]);
- floodfill(xx, yy, ch, lb[ch], rb[ch], u, d);
- }
- }
- }
- int main() {
- cin >> n;
- cin >> m;
- s.resize(n);
- cin.ignore(1000, '\n');
- for (int i = 0 ; i < n; i++) {
- cin >> s[i];
- for (int j = 0; j < m; j++) {
- if (isalpha(s[i][j])) {
- if (s[i][j] == 'A') {
- x= i, y = j;
- } else {
- pt.push_back({j, i});
- }
- }
- }
- }
- sort(pt.begin(), pt.end());
- // fix a base
- int maxarea = 1;
- int ansl = y;
- int ansr = y;
- int anstop = x;
- int ansbase = x;
- for (int base = x; base < n; base++) {
- // find l and r range of base
- int lrange = 0, rrange = m - 1;
- for (auto p : pt) {
- int xx = p.second, yy = p.first;
- if (x <= xx && xx <= base) {
- if (yy <= y) lrange = max(yy + 1, lrange);
- else rrange = min(yy - 1, rrange);
- }
- }
- if (lrange > rrange) continue;
- // find largest on top within the range
- vector<pair<int, int> > above;
- for (auto p : pt) {
- int xx = p.second, yy = p.first;
- if (xx >= x) continue;
- if (yy < lrange || yy > rrange) continue;
- above.push_back({yy,xx});
- }
- above.push_back({0, -1});
- //sort(q.begin(), q.end());
- //reverse(pright.begin(), pright.end());
- for (auto p : above) {
- int x1 = p.second, y1 = p.first;
- int top = x1 + 1;
- for (auto q : above) {
- int x2 = q.second, y2 = q.first;
- if (x2 >= top) {
- if (y2 <= y) lrange = max(y2 + 1, lrange);
- else rrange = min(y2 - 1, rrange);
- }
- }
- if (lrange > rrange) continue; // cannot attain
- int area = (rrange - lrange + 1) * (base - top + 1);
- if (area > maxarea) {
- maxarea = area;
- ansbase = base;
- anstop = top;
- ansl = lrange;
- ansr = rrange;
- }
- }
- }
- pt.push_back({y, x});
- fillbound(anstop, ansbase, ansl, ansr);
- fillbound(anstop, ansbase, 0, ansl - 1);
- fillbound(anstop, ansbase, ansr + 1, m - 1);
- fillbound(0, anstop - 1, 0, m - 1);
- fillbound(ansbase + 1, n - 1, 0, m - 1);
- //fprintf(stderr, "area = %d, row: %d, %d, col: %d, %d\n", maxarea, anstop, ansbase, ansl, ansr);
- for (int i = 0; i < n; i++) {
- for (int j= 0; j < m ; j++) {
- cout << s[i][j];
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement