제출 #785681

#제출 시각아이디문제언어결과실행 시간메모리
785681lucas_joel_hanTracks in the Snow (BOI13_tracks)C++14
100 / 100
622 ms122648 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; #define fi first #define se second const int INF = 1e9; int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int main() { cin.tie(0)->sync_with_stdio(0); int N, M; cin >> N >> M; vector<vector<char>> v(N, vector<char>(M)); for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < M; j++) { v[i][j] = s[j]; } } vector<vector<int>> d(N, vector<int>(M, INF)); d[0][0] = 1; deque<pii> q; q.push_front(pii(0, 0)); while (!q.empty()) { pii cur = q.front(); q.pop_front(); for (int i = 0; i < 4; i++) { int x = cur.fi + dx[i], y = cur.se + dy[i]; if (x < 0 || x >= N || y < 0 || y >= M || v[x][y] == '.') continue; bool weight = (v[x][y] != v[cur.fi][cur.se]); if (d[cur.fi][cur.se] + weight < d[x][y]) { d[x][y] = d[cur.fi][cur.se] + weight; if (weight) q.push_back(pii(x, y)); else q.push_front(pii(x, y)); } } } int sol = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (v[i][j] != '.') sol = max(sol, d[i][j]); } } cout << sol << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...