이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4000 + 7;
string s[N];
bool vis[N][N];
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int n, m, ret = 0;
void bfs(queue<pair<int, int>> &q1, queue<pair<int, int>> &q2, int d) {
if (q1.empty()) return;
ret = d;
while (!q1.empty()) {
pair<int, int> x = q1.front();
q1.pop();
for (int dir = 0; dir < 4; dir++) {
if (x.first + dx[dir] < 0 || x.first + dx[dir] >= n || x.second + dy[dir] < 0 || x.second + dy[dir] >= m) {
continue;
}
if (vis[x.first + dx[dir]][x.second + dy[dir]] == true || s[x.first + dx[dir]][x.second + dy[dir]] == '.') {
continue;
}
vis[x.first + dx[dir]][x.second + dy[dir]] = 1;
if (s[x.first + dx[dir]][x.second + dy[dir]] != s[x.first][x.second]) {
q2.push({x.first + dx[dir], x.second + dy[dir]});
} else {
q1.push({x.first + dx[dir], x.second + dy[dir]});
}
}
}
bfs(q2, q1, d + 1);
}
int main() {
#ifdef ONPC
freopen("input.txt", "r", stdin);
#endif // ONPC
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
queue<pair<int, int>> q1, q2;
q1.push({0, 0});
vis[0][0] = 1;
bfs(q1, q2, 1);
cout << ret << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |