Submission #716952

#TimeUsernameProblemLanguageResultExecution timeMemory
716952MakarooniTracks in the Snow (BOI13_tracks)C++17
100 / 100
870 ms119328 KiB
/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */

#include "bits/stdc++.h"
using namespace std;

#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define mr make_pair
//#define int long long
#define pii pair<int, int>
typedef long double ld;
typedef long long ll;

mt19937 rng (chrono::high_resolution_clock::now().time_since_epoch().count());

const int N = 4e3 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 1;

int dis[N][N], dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
char c[N][N];

int32_t main(){
	ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> c[i][j];
			dis[i][j] = inf;
		}
	}
	deque<pii> q;
	q.pb(mr(1, 1));
	dis[1][1] = 1;
	int ans = 0;
	while(!q.empty()){
		pii v = q.front();
		ans = max(ans, dis[v.F][v.S]);
		q.pop_front();
		for(int i = 0; i < 4; i++){
			if(v.F + dx[i] < 0 || v.F + dx[i] > n || v.S + dy[i] <= 0 || v.S + dy[i] > m)
				continue;
			if(c[v.F + dx[i]][v.S + dy[i]] == '.')
				continue;
			bool f = (c[v.F][v.S] == c[v.F + dx[i]][v.S + dy[i]]);
			if(f){
				if(dis[v.F][v.S] < dis[v.F + dx[i]][v.S + dy[i]]){
					dis[v.F + dx[i]][v.S + dy[i]] = dis[v.F][v.S];
					q.push_front(mr(v.F + dx[i], v.S + dy[i]));
				}
			}
			else{
				if(dis[v.F][v.S] + 1 < dis[v.F + dx[i]][v.S + dy[i]]){
					dis[v.F + dx[i]][v.S + dy[i]] = dis[v.F][v.S] + 1;
					q.pb(mr(v.F + dx[i], v.S + dy[i]));
				}
			}
		}
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...