이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define MOD (ll) (1e9+7)
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int n,m; string board[4000];
bool inBounds(int x, int y) {
return (x>=0 && x<n && y>=0 && y<m && board[x][y]!='.');
}
int main() {
ios_base::sync_with_stdio(false);
cin>>n>>m; //height n width m
for(int i=0; i<n;i++) cin>>board[i];
vector<vector<int>> dist(n, vector<int>(m, 0));
int ans = 1;
deque<pair<int,int>> q;
dist[0][0] = 1;
q.push_back({0,0});
while(!q.empty()) {
pair<int,int> curr = q.front();
q.pop_front();
ans = max(ans, dist[curr.first][curr.second]);
for(int i=0; i<4; i++) {
int x = curr.first + dx[i];
int y = curr.second + dy[i];
if(inBounds(x,y) && dist[x][y] == 0) {
if(board[curr.first][curr.second] == board[x][y]) {
dist[x][y] = dist[curr.first][curr.second];
q.push_front({x,y});
} else {
dist[x][y] = dist[curr.first][curr.second]+1;
q.push_back({x,y});
}
}
}
}
cout<<ans<<endl; return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |