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 <vector>
#include <map>
#include <queue>
#include <algorithm>
#include<stack>
#include <set>
using namespace std;
using ll = long long;
const int INF = 1e9;
const int MAXN = 4e3+111;
const int MOD = 1e9 + 7;
const int MAXS = 250*1000+123;
int n, m;
char t[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dist[MAXN][MAXN];
int ans = 0;
pair<int, int> dd[]{ {0,1},{0,-1},{1,0},{-1,0} };
bool isIn(int i, int j)
{
if (i < n && j < n && i >= 0 && j >= 0 && t[i][j] != '.')
return true;
else
return false;
}
void bfs(int i, int j)
{
deque<pair<int,int>> q;
q.push_back({ i,j });
dist[i][j] = 1;
while (!q.empty())
{
pair<int,int> s = q.front();
q.pop_front();
ans = max(ans, dist[s.first][s.second]);
for (pair<int, int> d : dd)
{
int newI = s.first + d.first;
int newJ = s.second + d.second;
if (isIn(newI, newJ) && dist[newI][newJ] == 0)
{
int w = (t[i][j] != t[newI][newJ]);
dist[newI][newJ] = dist[i][j] + w;
if (w == 1)
q.push_back({ newI,newJ });
else
q.push_front({ newI,newJ });
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> t[i][j];
bfs(0, 0);
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |