제출 #769507

#제출 시각아이디문제언어결과실행 시간메모리
769507EllinorTracks in the Snow (BOI13_tracks)C++14
39.58 / 100
2112 ms484352 KiB
#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef long long ll;

int H, W;
vector<string> grid;

vector<vector<ll>> visited; // bitset ?

priority_queue<vector<ll>> q;

void dfs(int hat, int wat, ll num)
{
    visited[hat][wat] = num;

    char ch = grid[hat][wat];
    if (hat + 1 < H) {
        if (visited[hat + 1][wat] == -1 && grid[hat + 1][wat] != '.') {
            if (grid[hat + 1][wat] == ch) q.push({-num, hat + 1, wat});
            else q.push({-(num + 1), hat + 1, wat});
        }
    }
    if (wat + 1 < W) {
        if (visited[hat][wat + 1] == -1 && grid[hat][wat + 1] != '.') {
            if (grid[hat][wat + 1] == ch) q.push({-num, hat, wat + 1});
            else q.push({-(num + 1), hat, wat + 1});
        }
    }
    if (hat - 1 >= 0) {
        if (visited[hat - 1][wat] == -1 && grid[hat - 1][wat] != '.') {
            if (grid[hat - 1][wat] == ch) q.push({-num, hat - 1, wat});
            else q.push({-(num + 1), hat - 1, wat});
        }
    }
    if (wat - 1 >= 0) {
        if (visited[hat][wat - 1] == -1 && grid[hat][wat - 1] != '.') {
            if (grid[hat][wat - 1] == ch) q.push({-num, hat, wat - 1});
            else q.push({-(num + 1), hat, wat - 1});
        }
    }
}

int32_t main()
{
    cin>>H>>W;
    grid.assign(H, "");
    rep(i,0,H){
        cin>>grid[i];
    }

    visited.assign(H, vector<ll>(W, -1));
    q.push({-1,0,0});
    while(!q.empty())
    {
        vector<ll> x = q.top();
        q.pop();
        dfs(x[1], x[2], -(x[0]));
    }

    ll ans = 1;
    rep(i,0,H){
        rep(j,0,W){
            ans = max(ans, visited[i][j]);
        }
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...