이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int moves[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int main(){
int n, m;
cin >> n >> m;
char arr[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> arr[i][j];
}
}
int num[n][m];
memset(num, 0, sizeof(num));
num[0][0] = 1;
deque<pair<int, int>> q;
q.push_back({0, 0});
int ans = 0;
while(!q.empty()){
pair<int, int> x = q.front();
q.pop_front();
ans = max(num[x.first][x.second], ans);
for(int i = 0; i < 4; i++){
int x1 = x.first + moves[i][0], y1 = x.second + moves[i][1];
if(x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || num[x1][y1] != 0 || arr[x1][y1] == '.') continue;
if(arr[x1][y1] != arr[x.first][x.second]){
num[x1][y1] = num[x.first][x.second] + 1;
q.push_back({x1, y1});
} else {
num[x1][y1] = num[x.first][x.second];
q.push_front({x1, y1});
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |