This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <unordered_set>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <random>
#include <queue>
//#include <fstream>
/*#include <cstdio>
#include <ctime>*/
#include <complex>
using namespace std;
/*Problem: */
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m));
vector <vector<int>> depth(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> grid[i][j];
/*cout << "grid" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << grid[i][j];
cout << endl;
}*/
int x[] = {-1, 1, 0, 0 };
int y[] = { 0, 0, -1, 1 };
deque<pair<int, int>> deck;
deck.push_front({ 0, 0 });
depth[0][0] = 1;
pair<int, int> trace, neig;
char ani; int ani_depth;
int ans = -1;
while (!deck.empty())
{
trace = deck.front(); deck.pop_front();
ani = grid[trace.first][trace.second];
ani_depth = depth[trace.first][trace.second];
ans = max(ans, ani_depth);
for (int i = 0; i < 4; i++)
{
neig.first = trace.first + x[i];
neig.second = trace.second + y[i];
if (neig.first < 0 || neig.first >= n
|| neig.second < 0 || neig.second >= m
|| depth[neig.first][neig.second] != 0
|| grid[neig.first][neig.second] == '.')
continue;
if (grid[neig.first][neig.second] == ani)
{
depth[neig.first][neig.second] = ani_depth;
deck.push_front(neig);
}
else
{
depth[neig.first][neig.second] = ani_depth + 1;
deck.push_back(neig);
}
}
}
/*cout << "depths" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << depth[i][j];
cout << endl;
}
*/
cout << ans << endl;
}
int main() {
//freopen("revegetate.in", "r", stdin);
//freopen("revegetate.out", "w", stdout);
cin.tie(0);
ios::sync_with_stdio(0);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |