Submission #672436

#TimeUsernameProblemLanguageResultExecution timeMemory
672436Magikarp4000Tracks in the Snow (BOI13_tracks)C++17
100 / 100
594 ms144408 KiB
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
const int MAXN = 4e3+1;
int h,w;
string g[MAXN];
bool z[MAXN][MAXN];
int d[MAXN][MAXN];

int main() {
  OPTM;
  cin >> h >> w;
  FOR(i,0,h) cin >> g[i];
  FOR(i,0,h) FOR(j,0,w) d[i][j] = INF;
  d[0][0] = 1;
  deque<PII> q;
  q.PB({0,0});
  while (!q.empty()) {
    int x = q.front().F, y = q.front().S;
    q.pop_front();
    if (z[y][x]) continue;
    z[y][x] = 1;
    FOR(i,0,4) {
      int nx = x+dx[i], ny = y+dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
      if (g[ny][nx] == '.') continue;
      int c = 0;
      if (g[ny][nx] != g[y][x]) c = 1;
      if (d[y][x]+c < d[ny][nx]) {
        d[ny][nx] = d[y][x]+c;
        if (c == 1) q.PB({nx,ny});
        else q.push_front({nx,ny});
      }
    }
  }
  int res = 0;
  FOR(i,0,h) {
    FOR(j,0,w) {
      if (g[i][j] != '.') res = max(res, d[i][j]);
    }
  }
  cout << res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...