이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 4e3+4;
char matriz[MAX][MAX];
int dist[MAX][MAX];
int n,m;
bool inBound(pair<int,int> a){
int x = a.first, y = a.second;
if(x < 0 or x >= n or y < 0 or y >= m or matriz[x][y] == '.' or dist[x][y] > 0){
return false;
}
return true;
}
int main(){ios_base::sync_with_stdio(false); cin.tie(NULL);
int ans = 1;
cin >> n >> m;
for(int i =0;i < n;i++){
for(int j = 0; j < m;j++){
cin >> matriz[i][j];
}
}
deque<pair<int,int>> fila;
dist[0][0] =1;
fila.push_front({0,0});
while(!fila.empty()){
pair<int,int> atual = fila.front();
fila.pop_front();
int x = atual.first, y = atual.second;
ans = max(ans,dist[x][y]);
if(inBound({x+1,y})){
if(matriz[x+1][y] == matriz[x][y]){
dist[x+1][y] = dist[x][y];
fila.push_front({x+1,y});
}
else{
dist[x+1][y] = dist[x][y]+1;
fila.push_back({x+1,y});
}
}
if(inBound({x-1,y})){
if(matriz[x-1][y] == matriz[x][y]){
dist[x-1][y] = dist[x][y];
fila.push_front({x-1,y});
}
else{
dist[x-1][y] = dist[x][y]+1;
fila.push_back({x-1,y});
}
}
if(inBound({x,y+1})){
if(matriz[x][y+1] == matriz[x][y]){
dist[x][y+1] = dist[x][y];
fila.push_front({x,y+1});
}
else{
dist[x][y+1] = dist[x][y]+1;
fila.push_back({x,y+1});
}
}
if(inBound({x,y-1})){
if(matriz[x][y-1] == matriz[x][y]){
dist[x][y-1] = dist[x][y];
fila.push_front({x,y-1});
}
else{
dist[x][y-1] = dist[x][y]+1;
fila.push_back({x,y-1});
}
}
}
cout << max(ans,dist[n-1][m-1]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |