# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
101457 | rocketninja7 | Tracks in the Snow (BOI13_tracks) | C++14 | 1643 ms | 88968 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
pair<int, int> dir[]={make_pair(0, -1), make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0)};
int main(){
int H, W;
scanf("%d%d", &H, &W);
char grid[H][W+1];
for(int i=0;i<H;i++){
scanf("%s", &grid[i]);
}
int dist[H][W];
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
dist[i][j]=-1;
}
}
dist[0][0]=1;
queue<pair<int, int> > procA;
queue<pair<int, int> > procB;
procA.push(make_pair(0, 0));
while(!procA.empty()||!procB.empty()){
while(!procA.empty()){
int x=procA.front().first, y=procA.front().second;
procA.pop();
for(int i=0;i<4;i++){
if(x+dir[i].first>-1&&x+dir[i].first<H&&y+dir[i].second>-1&&y+dir[i].second<W){
if(grid[x+dir[i].first][y+dir[i].second]!='.'){
if(dist[x+dir[i].first][y+dir[i].second]==-1){
if(grid[x+dir[i].first][y+dir[i].second]==grid[x][y]){
dist[x+dir[i].first][y+dir[i].second]=dist[x][y];
procA.push(make_pair(x+dir[i].first, y+dir[i].second));
}
else{
dist[x+dir[i].first][y+dir[i].second]=dist[x][y]+1;
procB.push(make_pair(x+dir[i].first, y+dir[i].second));
}
}
}
}
}
}
while(!procB.empty()){
int x=procB.front().first, y=procB.front().second;
procB.pop();
for(int i=0;i<4;i++){
if(x+dir[i].first>-1&&x+dir[i].first<H&&y+dir[i].second>-1&&y+dir[i].second<W){
if(grid[x+dir[i].first][y+dir[i].second]!='.'){
if(dist[x+dir[i].first][y+dir[i].second]==-1){
if(grid[x+dir[i].first][y+dir[i].second]==grid[x][y]){
dist[x+dir[i].first][y+dir[i].second]=dist[x][y];
procB.push(make_pair(x+dir[i].first, y+dir[i].second));
}
else{
dist[x+dir[i].first][y+dir[i].second]=dist[x][y]+1;
procA.push(make_pair(x+dir[i].first, y+dir[i].second));
}
}
}
}
}
}
}
int ans=1;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
ans=max(ans, dist[i][j]);
}
}
printf("%d", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |