제출 #475813

#제출 시각아이디문제언어결과실행 시간메모리
475813clamsTracks in the Snow (BOI13_tracks)C++17
100 / 100
760 ms112816 KiB
/*
 * Author: bubu2006
**/

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    int H, W;
    cin >> H >> W;
    
    vector<string> m(H);
    for(int i = 0; i < H; i++) cin >> m[i];
    
    vector<int> dy = {0, 0, -1, 1};
    vector<int> dx = {1, -1, 0, 0};
    
    const int INF = 1e8;
    
    vector<vector<int>> d(H, vector<int>(W, INF));
    deque<pair<int, int>> q;
    q.push_back({0, 0});
    d[0][0] = 1;
    
    int ans = 1;
    while(!q.empty()) {
        pair<int, int> cur = q.front();
        int y = cur.first;
        int x = cur.second;
        ans = max(ans, d[y][x]);
        q.pop_front();
        
        
        for(int k = 0; k < 4; k++) {
            int ny = y + dy[k];
            int nx = x + dx[k];
            
            if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
            if(m[ny][nx] == '.') continue;
            
            int w = (m[y][x] != m[ny][nx]);
            if(d[ny][nx] > d[y][x] + w) {
                d[ny][nx] = d[y][x] + w;
                if(w) q.push_back({ny, nx});
                else q.push_front({ny, nx});
            }
        }
    } 
    
    cout << ans << '\n';
}

// ........ RRR..... FFR.....
// ........ ..RRR... .FRRR...
// ........ ..R..... .FFFFF..
// ........ ..RRRR.R ..RRRFFR
// ........ .....RRR .....FFF

// i had a greedy that always would work but the 0/1 bfs is just too clean
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...