제출 #1348033

#제출 시각아이디문제언어결과실행 시간메모리
1348033killerzaluuZoo (COCI19_zoo)C++20
110 / 110
29 ms5372 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;
const int INF = 1e9;

int r, c;
char a[N][N];
int d[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> r >> c;
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            cin >> a[i][j];
            d[i][j] = INF;
        }
    }

    deque<pair<int,int>> q;
    d[1][1] = 0;
    q.push_front({1, 1});

    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop_front();

        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (nx < 1 || nx > r || ny < 1 || ny > c) continue;
            if (a[nx][ny] == '*') continue;

            int w = (a[nx][ny] != a[x][y]);
            if (d[nx][ny] > d[x][y] + w) {
                d[nx][ny] = d[x][y] + w;
                if (w == 0) q.push_front({nx, ny});
                else q.push_back({nx, ny});
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            if (a[i][j] != '*') ans = max(ans, d[i][j]);
        }
    }

    cout << ans + 1 << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...