이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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>> dist(n,vector<int>(m,0));
vector<vector<int>> dir = {{1,0},{-1,0},{0,1},{0,-1}};
q.push_back({0,0});
dist[0][0] = 1;
int ans = 1;
while(!q.empty())
{
auto curr = q.front();
q.pop_front();
int x = curr.first;
int y = curr.second;
ans = max(ans,dist[x][y]);
for(auto d : dir)
{
int nx = x + d[0];
int ny = y + d[1];
if(nx < 0 || nx >= n || ny < 0 || ny >= m
||snow[nx][ny]=='.' ||dist[nx][ny]!=0)continue;
if (snow[x][y] == snow[nx][ny]) {
dist[nx][ny] = dist[x][y];
q.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
q.push_back({nx, ny});
}
}
}
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... |