Submission #389854

#TimeUsernameProblemLanguageResultExecution timeMemory
389854Alex_tz307Tracks in the Snow (BOI13_tracks)C++17
100 / 100
623 ms118996 KiB
#include <bits/stdc++.h>

using namespace std;

void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}

const int NMAX = 4e3 + 3;
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1};
int N, M, dp[NMAX][NMAX];
char a[NMAX][NMAX];

bool inside(int lin, int col) {
  return lin > 0 && col > 0 && lin <= N && col <= M;
}

void solve() {
  cin >> N >> M;
  for (int i = 1; i <= N; ++i)
    cin >> (a[i] + 1);
  dp[1][1] = 1;
  deque<pair<int,int>> dq{make_pair(1, 1)};
  int ans = 1;
  while (!dq.empty()) {
    int i, j;
    tie(i, j) = dq.front();
    dq.pop_front();
    if (dp[i][j] > ans)
      ans = dp[i][j];
    for (int k = 0; k < 4; ++k) {
      int iv = i + di[k], jv = j + dj[k];
      if (!inside(iv, jv) || a[iv][jv] == '.')
        continue;
      int cost = a[i][j] != a[iv][jv];
      if (dp[iv][jv] == 0) {
        dp[iv][jv] = dp[i][j] + cost;
        if (cost)
          dq.push_back(make_pair(iv, jv));
        else dq.push_front(make_pair(iv, jv));
      }
    }
  }
  cout << ans << '\n';
}

int main() {
  fastIO();
  solve();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...