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 <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1000000007;
int n,m;
#define pb push_back
#define mp make_pair
vector<vector<pair<int,int>>> adj;
void solve()
{
cin >> n >>m ;
vector<string> snow(n);
for(int i = 0;i<n;i++)cin >> snow[i];
deque<pair<int,int>> q;
vector<vector<int>> vis(n,vector<int>(m,0));
vector<vector<int>> dist(n,vector<int>(m,1e9));
vector<vector<int>> parent(n,vector<int>(m,-1));
vector<vector<int>> dir = {{1,0},{-1,0},{0,1},{0,-1}};
q.push_back({0,0});
dist[0][0] = 1;
while(!q.empty())
{
auto curr = q.front();
q.pop_front();
int x = curr.first;
int y = curr.second;
if(vis[x][y])continue;
vis[x][y] = 1;
for(auto d : dir)
{
int nx = x + d[0];
int ny = y + d[1];
if(nx < 0 || nx >= n || ny < 0 || ny >= m)continue;
if(snow[nx][ny] == snow[x][y])
{
if(dist[nx][ny] > dist[x][y])
{
dist[nx][ny] = dist[x][y];
parent[nx][ny] = parent[x][y];
q.push_front({nx,ny});
}
}
else
{
if(dist[nx][ny] > dist[x][y] + 1)
{
dist[nx][ny] = dist[x][y] + 1;
parent[nx][ny] = parent[x][y];
q.push_back({nx,ny});
}
}
}
}
int ans = 0;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
{
if(dist[i][j] == 1e9)continue;
if(snow[i][j] == '1')continue;
ans = max(ans,dist[i][j]);
}
}
cout << ans << "\n";
return;
}
int main()
{
int tests = 1;
//cin >> tests;
int i = 1;
while (i <= tests)
{
//cout << "Case #"<< i<< ": ";
solve();
if(i!=tests)cout << "\n";
i++;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |