이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> ii;
const int maxN = 4000;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int n, m;
char a[maxN + 5][maxN + 5];
int d[maxN + 5][maxN + 5];
bool isInside(int i, int j){
return (i >= 1 && j >= 1 && i <= n && j <= m && a[i][j] != '.');
}
int bfs(){
int ret = 0;
deque<ii> q;
q.push_back({1, 1});
d[1][1] = 1;
while (!q.empty()){
ii t = q.front();
q.pop_front();
ret = max(d[t.x][t.y], ret);
for (int k = 0; k < 4; ++k){
int u = t.x + di[k];
int v = t.y + dj[k];
if (!isInside(u, v)) continue;
if (!d[u][v]){
if (a[u][v] == a[t.x][t.y]){
d[u][v] = d[t.x][t.y];
q.push_front({u, v});
}
else{
d[u][v] = d[t.x][t.y] + 1;
q.push_back({u, v});
}
}
}
}
return ret;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
cout << bfs();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |