이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"bits/stdc++.h"
#ifndef LOCAL
#define LINE
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#else
#define LINE cerr << "----------" << nl;
#define fastio
#endif
using namespace std;
using ll = long long; using ld = long double;
#define nl '\n'
#define arr array
#define pb push_back
#define S second
#define F first
#define int long long
const ll inf = 1e9+5;
void solve() {
int n,m; cin >> n >> m;
vector<string> a(n);
for (int i=0; i<n; i++) {
cin >> a[i];
}
deque<arr<int, 2>> q;
vector dis(n, vector<int>(m, -1));
int ans = 1;
auto ok = [&] (int x, int y) {
return x >=0 && x < n && y>=0 && y<m && dis[x][y] == -1 && a[x][y] != '.';
};
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
dis[0][0] = 1;
q.push_front({0,0});
while (q.size()) {
auto [x, y] = q.front();
q.pop_front();
for (int k=0; k<4; k++) {
int xx = x+dx[k], yy = y+dy[k];
if (ok(xx, yy)) {
if (a[xx][yy]!=a[x][y]) {
dis[xx][yy] = dis[x][y]+1;
q.push_back({xx, yy});
} else {
dis[xx][yy] = dis[x][y];
q.push_front({xx, yy});
}
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
ans = max(ans, dis[i][j]);
}
}
cout << ans << nl;
}
int32_t main() {
/*
freopen("swap.in", "r", stdin);
freopen("swap.out", "w", stdout);
*/
int t = 1;
//cin >> t;
while (t--) {
solve();
LINE
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |