제출 #1172344

#제출 시각아이디문제언어결과실행 시간메모리
1172344nolqfTracks in the Snow (BOI13_tracks)C++20
11.04 / 100
427 ms117400 KiB
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <queue> #include <cmath> using namespace std; using ll = long long; const int MAXN = 4e3; const int INF = 2e9; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; string T[1 + MAXN]; int dist[1 + MAXN][1 + MAXN]; int n, m; bool out( int i, int j ) { return 1 > i || 1 > j || n < i || m < j; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for ( int i = 1; i <= n; ++i ) { cin >> T[i]; T[i] = '#' + T[i]; for ( int j = 1; j <= m; ++j ) { dist[i][j] = INF; } } deque<pair<int, int>> dq; dq.push_back({1, 1}); dist[1][1] = 1; while ( !dq.empty() ) { auto [i, j] = dq.front(); dq.pop_front(); for ( int d = 0; d < 4; ++d ) { int ni = i + dx[d], nj = j + dy[d]; if ( out(ni, nj) && T[ni][nj] != '.' ) continue; int cost = T[ni][nj] != T[i][j]; if ( dist[ni][nj] > dist[i][j] + cost ) { dist[ni][nj] = dist[i][j] + cost; if ( cost == 0 ) { dq.push_front({ni, nj}); } else { dq.push_back({ni, nj}); } } } } int res = 0; for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= m; ++j ) { if ( T[i][j] != '.' ) { res = max(res, dist[i][j]); } } } cout << res << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...