제출 #382612

#제출 시각아이디문제언어결과실행 시간메모리
382612ritul_kr_singhTracks in the Snow (BOI13_tracks)C++17
82.50 / 100
2109 ms401012 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;
	priority_queue<array<int, 3>> q;
	q.push({-1, 0, 0});
	int ans = 0;
	while(!q.empty()){
		int x = q.top()[1], y = q.top()[2], d = -q.top()[0];
		q.pop();
		if(d!=dist[x][y]) continue;
		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;
				q.push({-dist[i][j], i, j});
			}
		}
		ans = d;
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...