이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif
#define mp make_pair
#define pb push_back
#define sz(x) (int((x).size()))
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;
const int oo = 0x7f7f7f7f;
int h, w, d[4001][4001], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
string grid[4001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ms(d, 0x7f);
cin >> h >> w;
for (int i = 0; i < h; ++i) {
cin >> grid[i];
}
d[0][0] = 1;
deque<pii> q;
q.push_back({0, 0});
auto valid = [](int x, int y) {
return x >= 0 && x < h && y >= 0 && y < w && grid[x][y] != '.';
};
int ans = 0;
while (!q.empty()) {
auto u = q.front(); q.pop_front();
ans = max(ans, d[u.first][u.second]);
for (int i = 0; i < 4; ++i) {
int x = u.first + dx[i], y = u.second + dy[i];
if (valid(x, y) && d[x][y] == oo) {
if (grid[x][y] == grid[u.first][u.second]) {
d[x][y] = d[u.first][u.second];
q.push_front({x, y});
} else {
d[x][y] = 1 + d[u.first][u.second];
q.push_back({x, y});
}
}
}
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |