Submission #382614

#TimeUsernameProblemLanguageResultExecution timeMemory
382614ritul_kr_singhTracks in the Snow (BOI13_tracks)C++17
100 / 100
1151 ms411224 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"

vector<int> e;
int find(int u){
	return e[u] < 0 ? u : e[u] = find(e[u]);
}
void unite(int u, int v){
	u = find(u), v = find(v);
	if(u==v) return;
	if(e[u] > e[v]) swap(u, v);
	e[u] += e[v], e[v] = u;
}

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int h, w; cin >> h >> w;
	int g[h][w];
	e.assign(h*w, -1);
	for(int i=0; i<h; ++i){
		for(int j=0; j<w; ++j){
			char c; cin >> c;
			if(c=='.') g[i][j] = 0;
			if(c=='F') g[i][j] = 1;
			if(c=='R') g[i][j] = 2;
		}
	}
	int dist[h][w];
	for(int i=0; i<h; ++i)
		fill(dist[i], dist[i]+w, 1e18);
	dist[0][0] = 1;
	deque<array<int, 2>> q;
	q.push_back({0, 0});
	int ans = 0;
	while(!q.empty()){
		int x = q.front()[0], y = q.front()[1], d = dist[x][y];
		q.pop_front();
		for(int k=0; k<4; ++k){
			int i = x+dx[k], j = y+dy[k];
			if(i<0 or j<0 or i==h or j==w or !g[i][j]) continue;
			int w = g[i][j]!=g[x][y];
			if(dist[i][j] > dist[x][y] + w){
				dist[i][j] = dist[x][y] + w;
				if(w) q.push_back({i, j});
				else q.push_front({i, j});
			}
		}
		ans = d;
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...