Submission #482109

#TimeUsernameProblemLanguageResultExecution timeMemory
482109nehasaneTracks in the Snow (BOI13_tracks)C++14
84.69 / 100
2103 ms133604 KiB
#include <bits/stdc++.h>
using namespace std;
const int MAX_H = 4000;
const int MAX_W = 4000;
vector <string> meadow(MAX_H);
bool diff_animal(int x, int y, int diffx, int diffy){
    if (meadow[x][y] != meadow[x + diffx][y + diffy])
        return true;
    return false;
}
int main()
{
    int h, w;
    cin >> h >> w;
    for (int i = 0; i < h; i++)
        cin >> meadow[i];
    priority_queue <tuple <int, int, int>> q;
    int dis[MAX_H][MAX_W];
    bool visited[MAX_H][MAX_W];
    for (int i = 0; i < MAX_H; i++){
        for (int j = 0; j < MAX_W; j++){
            dis[i][j] = INT_MAX;
            visited[i][j] = false;
        }
    }
    q.push({0, 0, 0});
    dis[0][0] = 0;
    int ans = 0;
    while (!q.empty()){
        int x = get<1>(q.top()), y = get<2>(q.top());
        q.pop();
        //check up
        if (x > 0 && meadow[x-1][y] != '.' && !visited[x-1][y]){
            int d = diff_animal(x, y, -1, 0);
            if (dis[x-1][y] > dis[x][y] + d){
                dis[x-1][y] = dis[x][y] + d;
                ans = max(ans, dis[x-1][y]);
                q.push({-dis[x-1][y], x-1, y});
                visited[x-1][y] = true;
            }
        }
        //check down
        if (x < h-1 && meadow[x+1][y] != '.' && !visited[x+1][y]){
            int d = diff_animal(x, y, 1, 0);
            if (dis[x+1][y] > dis[x][y] + d){
                dis[x+1][y] = dis[x][y] + d;
                ans = max(ans, dis[x+1][y]);
                q.push({-dis[x+1][y], x+1, y});
                visited[x+1][y] = true;
            }
        }
        //check left
        if (y > 0 && meadow[x][y-1] != '.' && !visited[x][y-1]){
            int d = diff_animal(x, y, 0, -1);
            if (dis[x][y-1] > dis[x][y] + d){
                dis[x][y-1] = dis[x][y] + d;
                ans = max(ans, dis[x][y-1]);
                q.push({-dis[x][y-1], x, y-1});
                visited[x][y-1] = true;
            }
        }
        //check right
        if (y < w-1 && meadow[x][y+1] != '.' && !visited[x][y+1]){
            int d = diff_animal(x, y, 0, 1);
            if (dis[x][y+1] > dis[x][y] + d){
                dis[x][y+1] = dis[x][y] + d;
                ans = max(ans, dis[x][y+1]);
                q.push({-dis[x][y+1], x, y+1});
                visited[x][y+1] = true;
            }
        }
    }
    cout << ans+ 1 << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...