이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int h,w, mxH = 4e3;
vector<vector<char>> grid(mxH, vector<char> (mxH));
vector<vector<int>> dis(mxH, vector<int> (mxH, 1e9));
vector<int> dx = {0,1,0,-1}, dy = {1,0,-1,0};
vector<vector<bool>> vis(mxH, vector<bool> (mxH));
bool check(int x, int y){
return x >= 0 && x < h && y >= 0 && y < w && grid[x][y] != '.';
}
bool calcW(int x, int y, int nx ,int ny){
return grid[x][y] != grid[nx][ny];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> h >> w;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++) cin >> grid[i][j];
deque<pair<int,int>> dq;
dq.push_front({0,0});
dis[0][0] = 1;
while(dq.size()){
int x = dq.front().first, y = dq.front().second; dq.pop_front();
if(vis[x][y]) continue;
vis[x][y] = true;
for(int i=0; i<4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(check(nx,ny) && dis[nx][ny] > dis[x][y] + calcW(x,y,nx,ny)){
dis[nx][ny] = dis[x][y] + calcW(x,y,nx,ny);
if(calcW(x,y,nx,ny))
dq.push_back({nx,ny});
else dq.push_front({nx,ny});
}
}
}
int ans = 0;
for(int i=0; i<h; i++)
for(int j=0; j<w; j++) if(dis[i][j] != 1e9) ans = max(dis[i][j], ans);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |