제출 #769872

#제출 시각아이디문제언어결과실행 시간메모리
769872EllinorTracks in the Snow (BOI13_tracks)C++14
100 / 100
1480 ms877364 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;
vector<vector<pair<ll, ll>>> pq_vec;
    
void dfs(int hat, int wat, ll num)
{
    //cerr << hat << " " << wat << " " <<num << "\n";
    //if (visited[hat][wat]>0) return;
    //if (grid[hat][wat] == '.') return;
    //visited[hat][wat] = num;
    
    char ch = grid[hat][wat];
    if (hat + 1 < H) {
        if (grid[hat + 1][wat] != '.' && visited[hat + 1][wat] < 0)
        {
            if (grid[hat + 1][wat] == ch) {
                pq_vec[num].push_back({hat + 1, wat});
                visited[hat + 1][wat] = num;
            }
            else {
                pq_vec[num + 1].push_back({hat + 1, wat});
                visited[hat + 1][wat] = num + 1;
            }
        }
    }
    if (wat + 1 < W) {
        if (grid[hat][wat + 1] != '.' && visited[hat][wat + 1] < 0)
        {
            if (grid[hat][wat + 1] == ch) {
                pq_vec[num].push_back({hat, wat + 1});
                visited[hat][wat + 1] = num;
            }
            else {
                pq_vec[num + 1].push_back({hat, wat + 1});
                visited[hat][wat + 1] = num + 1;
            }
        }
    }
    if (hat - 1 >= 0) {
        if (grid[hat - 1][wat] != '.' && visited[hat - 1][wat] < 0)
        {
            if (grid[hat - 1][wat] == ch) {
                pq_vec[num].push_back({hat - 1, wat});
                visited[hat - 1][wat] = num;
            }
            else {
                pq_vec[num + 1].push_back({hat - 1, wat});
                visited[hat - 1][wat] = num + 1;
            }
        }
    }
    if (wat - 1 >= 0) {
        if (grid[hat][wat - 1] != '.' && visited[hat][wat - 1] < 0)
        {
            if (grid[hat][wat - 1] == ch) {
                pq_vec[num].push_back({hat, wat - 1});
                visited[hat][wat - 1] = num;
            }
            else {
                pq_vec[num + 1].push_back({hat, wat - 1});
                visited[hat][wat - 1] = num + 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});
    ll hw10 = H*W;
    hw10 += 10;
    pq_vec.assign(hw10, {});
    pq_vec[1].push_back({0,0});
    visited[0][0] = 1; // hm true ??

    // v

    ll i = 1;
    while(i < ll(pq_vec.size()))
    {
        ll j = 0;
        while (j < ll(pq_vec[i].size()))
        {
            pair<ll,ll> x = pq_vec[i][j];
            dfs(x.first, x.second, i);
            j++;
        }
        i++;
    }

    // ^
    
    ll ans = 1;
    rep(i,0,H){
        rep(j,0,W){
            ans = max(ans, visited[i][j]);
        }
    }
    cout << ans;
}

// pq too slow
// H*W , int * int no good, make ll = int * int
// MLE ?
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...