이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int n, m;
string s[5000];
int dp[5000][5000];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i=0; i<n; i++) {
cin >> s[i];
}
deque<pair<int, int>> q;
q.push_back({0, 0});
dp[0][0] = 1;
int ans = 0;
while(!q.empty()) {
auto me = q.front();
q.pop_front();
ans = max(ans, dp[me.first][me.second]);
for(int to=0; to<4; to++) {
int i = me.first +dx[to], j = me.second + dy[to];
if(i>=0&&i<n&&j>=0&&j<m&&s[i][j]!='.') {
if(dp[i][j]==0) {
if(s[i][j]!=s[me.first][me.second]) {
dp[i][j] = dp[me.first][me.second] + 1;
q.push_back({i, j});
} else {
dp[i][j] = dp[me.first][me.second];
q.push_front({i, j});
}
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |