이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pii pair<int, int>
const int MX = 4005;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int INF;
char a[MX][MX];
int d[MX][MX];
int n, m;
bool valid(int x, int y){
return (x >= 1 && x <= n && y >= 1 && y <= m);
}
int bfs(int x, int y){
memset(d, 63, sizeof d);
INF = d[0][0];
d[1][1] = 1;
deque<pii> q;
q.push_front({1, 1});
int ans = 1;
while(q.size()){
int x, y;
tie(x, y) = q.front();
q.pop_front();
ans = max(ans, d[x][y]);
for(int i=0; i<4; ++i){
int u = x + dx[i];
int v = y + dy[i];
int c = (a[x][y] != a[u][v]);
if(valid(u, v) && a[u][v] != '.' && d[u][v] == INF){
d[u][v] = d[x][y] + c;
if(c == 0){
q.push_front({u, v});
}
else{
q.push_back({u, v});
}
}
}
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("main.inp","r",stdin);
// freopen("main.out","w",stdout);
cin>>n>>m;
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
cin>>a[i][j];
}
}
cout<<bfs(1, 1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |