Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <queue>
- #include <stack>
- #include <cmath>
- #include <iomanip>
- #include <fstream>
- #include <map>
- #include <set>
- using namespace std;
- struct ver
- {
- vector<int> np;
- ver() {
- np.resize(4, -1);
- }
- };
- bool fl = false;
- vector<ver> g1, g2;
- vector<pair<int, int>> go = { { -1, 0 } ,{ 0, 1 },{ 1, 0 } ,{ 0, -1 } };
- vector<vector<char>> mat(100, vector <char>(100, '.'));
- void dfs1(int x, int y, int v, char cv, int pr)
- {
- mat[x][y] = cv;
- for (int i = 0; i != 4; i++)
- {
- if (g1[v].np[i] == -1)
- {
- continue;
- }
- pair<int, int> e = go[(i + pr) % 4];
- int xx = x + e.first;
- int yy = y + e.second;
- if (mat[xx][yy] == '.')
- {
- dfs1(xx, yy, g1[v].np[i], cv, pr);
- }
- else
- {
- if (mat[xx][yy] == '2')
- {
- fl = true;
- return;
- }
- }
- }
- return;
- }
- void dfs2(int x, int y, int v, char cv, int pr)
- {
- mat[x][y] = cv;
- for (int i = 0; i != 4; i++)
- {
- if (g2[v].np[i] == -1)
- {
- continue;
- }
- pair<int, int> e = go[(i + pr) % 4];
- int xx = x + e.first;
- int yy = y + e.second;
- if (mat[xx][yy] == '.')
- {
- dfs2(xx, yy, g2[v].np[i], cv, pr);
- }
- }
- return;
- }
- int kol = 0;
- bool gr = false;
- vector<vector<bool>> used(100, vector<bool>(100, false));
- void dfs(int x, int y)
- {
- kol++;
- used[x][y] = true;
- for (int i = 0; i != 4; i++)
- {
- pair<int, int> e = go[i];
- int xx = x + e.first;
- int yy = y + e.second;
- if (xx > -1 && xx < 100 && yy > -1 && yy < 100)
- {
- if (mat[xx][yy] == '.' && used[xx][yy] == false)
- {
- dfs(xx, yy);
- }
- }
- else
- {
- gr = true;
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- ifstream cin("input.txt");
- //ifstream cin("program.in");
- //ofstream cout("program.out");
- int cur = 0;
- int h1, h2;
- cin >> h1 >> h2;
- vector<vector<char>> col1(h1, vector<char>(h2));
- vector<vector<int>> num1(h1, vector<int>(h2, -1));
- for (int i = 0; i != h1; i++)
- {
- for (int j = 0; j != h2; j++)
- {
- cin >> col1[i][j];
- if (col1[i][j] == '*')
- {
- num1[i][j] = cur;
- cur++;
- }
- }
- }
- g1.resize(cur);
- for (int i = 0; i != h1; i++)
- {
- for (int j = 0; j != h2; j++)
- {
- if (col1[i][j] == '*')
- {
- for (int k = 0; k != 4; k++)
- {
- pair<int, int> e = go[k];
- int x = i + e.first;
- int y = j + e.second;
- if (x > -1 && x < h1 && y > -1 && y < h2)
- {
- if (col1[x][y] == '*')
- {
- g1[num1[i][j]].np[k] = num1[x][y];
- }
- }
- }
- }
- }
- }
- cur = 0;
- cin >> h1 >> h2;
- vector<vector<char>> col2(h1, vector<char>(h2));
- vector<vector<int>> num2(h1, vector<int>(h2, -1));
- for (int i = 0; i != h1; i++)
- {
- for (int j = 0; j != h2; j++)
- {
- cin >> col2[i][j];
- if (col2[i][j] == '*')
- {
- num2[i][j] = cur;
- cur++;
- }
- }
- }
- g2.resize(cur);
- for (int i = 0; i != h1; i++)
- {
- for (int j = 0; j != h2; j++)
- {
- if (col2[i][j] == '*')
- {
- for (int k = 0; k != 4; k++)
- {
- pair<int, int> e = go[k];
- int x = i + e.first;
- int y = j + e.second;
- if (x > -1 && x < h1 && y > -1 && y < h2)
- {
- if (col2[x][y] == '*')
- {
- g2[num2[i][j]].np[k] = num2[x][y];
- }
- }
- }
- }
- }
- }
- dfs2(50, 50, 0, '2', 0);
- int mx = -1;
- for (int x1 = 40; x1 != 60; x1++)
- {
- for (int y1 = 40; y1 != 60; y1++)
- {
- for (int p = 0; p != 4; p++)
- {
- fl = false;
- if (mat[x1][y1] == '.')
- {
- dfs1(x1, y1, 0, '1', p);
- }
- if (fl)
- {
- continue;
- }
- for (int i = 0; i != 100; i++)
- {
- for (int j = 0; j != 100; j++)
- {
- if (used[i][j] == false)
- {
- gr = false;
- kol = 0;
- dfs(i, j);
- if (gr)
- {
- continue;
- }
- mx = max(mx, kol);
- }
- }
- }
- used.resize(100, vector<bool>(100, false));
- mat.resize(100, vector<char>(100, '.'));
- dfs2(50, 50, 0, '2', 0);
- }
- }
- }
- cout << mx;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement