이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
struct P{
ll x, y;
};
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int N = 4001;
char grid[N][N];
int dist[N][N];
int h, w;
int valid(int x, int y){
return (0 <= x && x < h) && (0 <= y && y < w);
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
signed main(){
fastio;
cin >> h >> w;
for (int i=0; i<h; i++){
for (int j=0; j<w; j++){
cin >> grid[i][j];
}
}
memset(dist, -1, sizeof(dist));
queue<pair<int, int>> q;
int ans=1;
dist[0][0] = 1;
q.push({0, 0});
while (!q.empty()){
int x, y;
tie(x, y) = q.front(); q.pop();
for (int i=0; i<4; i++){
int a, b;
a = dx[i]+x;
b = dy[i]+y;
if (!valid(a, b) || grid[a][b] == '.') continue;
int v = dist[x][y] + (grid[a][b] != grid[x][y]);
if (dist[a][b] == -1) {
dist[a][b] = v;
q.push({a, b});
}
else dist[a][b] = min(dist[a][b], v);
}
}
for (int i=0; i<h; i++){
for (int j=0; j<w; j++){
ans = max(ans, dist[i][j]);
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |