이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 4001;
int a[N][N];
int n, m;
int dx[4]={0, 0, 1, -1};
int dy[4]={1, -1, 0, 0};
bool mark[N][N];
int bfs(int x, int y){
int ans=1;
deque<pair<pair<int, int>, int>> q;
q.push_back({{x, y}, 1});
mark[x][y]=1;
while(!q.empty()){
pair<int, int> xy=q.front().F;
ans=max(ans, q.front().S);
q.pop_front();
x=xy.F;
y=xy.S;
for(int i=0; i< 4; i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0 && nx<n && ny>=0 && ny<m && !mark[nx][ny] && a[nx][ny]!=2){
if(a[x][y]==a[nx][ny]){
q.push_front({{nx, ny}, ans});
}
else{
q.push_back({{nx, ny}, ans+1});
}
mark[nx][ny]=1;
}
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i< n; i++){
for(int j=0; j<m; j++){
char c;
cin >> c;
if(c=='.')a[i][j]=2;
else if(c=='R')a[i][j]=1;
}
}
cout << bfs(0, 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |