Submission #1172368

#TimeUsernameProblemLanguageResultExecution timeMemory
1172368nolqfTracks in the Snow (BOI13_tracks)C++20
100 / 100
425 ms117532 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;
  int res = 1;
  while ( !dq.empty() ) {
	auto [i, j] = dq.front();
	dq.pop_front();
	res = max(res, dist[i][j]);
    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});
		}
	  }
	}
  }
  cout << res << "\n";
  return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...