Submission #1352364

#TimeUsernameProblemLanguageResultExecution timeMemory
1352364t^t...Tracks in the Snow (BOI13_tracks)C++20
100 / 100
561 ms126904 KiB
/*=======================================
  Build    : LOCAL / Debug
  Compiler : g++ -std=gnu++17
  Note: read problem twice ฅ(^>⩊<^) ฅ
=======================================*/
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
  #define _GLIBCXX_DEBUG
  #include "debug.hpp"
#else
  #define debug(...) ((void)0)
#endif

#define sz(v) ((int)((v).size()))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

using ll = int64_t;
using db = long double;

const int mxN = (int)4e3+5;
const int xdir[4]{1,0,-1,0}, ydir[4]{0,1,0,-1};
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<typename T> int lwb(const vector<T>& a, const T& x){
  return lower_bound(a.begin(), a.end(), x) - a.begin(); }
template<typename T> int upb(const vector<T>& a, const T& x){
  return upper_bound(a.begin(), a.end(), x) - a.begin(); }

constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int ctz(int x) {  // assert(x >= 0); 
  return x == 0 ? -1 : __builtin_ctz(x); } // # of trailing zeros
constexpr int bits(int x) { // assert(x >= 0); 
  return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x)) 
  
template<typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } 
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

int n, m;
char g[mxN][mxN];
int dist[mxN][mxN];

bool valid(int x, int y) {
  return g[x][y] != '.';
}

void bfs(int sx, int sy) {
  deque<pair<int,int>> todo;
  todo.push_front({sx, sy}); 
  dist[sx][sy] = 1;
  
  while (sz(todo)) {
    auto [x, y] = todo.front(); todo.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = x + xdir[i];
      int ny = y + ydir[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
      if(!valid(nx, ny)) continue;
      if(g[nx][ny] == g[x][y]) {
        if(ckmin(dist[nx][ny], dist[x][y]))
          todo.push_front({nx, ny});
      } else {
        if(ckmin(dist[nx][ny], dist[x][y]+1))
          todo.push_back({nx, ny}); 
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  memset(dist, 0x3f, sizeof(dist));
  for(int i = 0; i < n; ++i)
    for(int j = 0; j < m; ++j) {
      cin >> g[i][j];
      if(g[i][j] == '.') 
        dist[i][j] = -1;
    }

  bfs(0, 0);
  
  int ans = 0;
  for(int i = 0; i < n; ++i)
    for(int j = 0; j < m; ++j) {
      ckmax(ans, dist[i][j]);
    }

  cout << ans;
  return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...