제출 #715613

#제출 시각아이디문제언어결과실행 시간메모리
715613Iliya_Tracks in the Snow (BOI13_tracks)C++14
100 / 100
803 ms160216 KiB
// IN THE NAME OF GOD
#include<bits/stdc++.h>
#define endl '\n'
#define file_reading freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define flush cout.flush();
using namespace std;
typedef long long ll;
typedef long double dll;
typedef unsigned long long ull;
 
const int N = 4e3+5, inf = 1e9; 
int dat[N][N], dist[N][N], ans, must; 
deque<pair<int,int>> q; 
 
void dij(int n, int m){
	for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) dist[i][j] = inf; 
	dist[n][m] = 1;
	q.push_front({n,m}); 
	while(q.size()){
		int x = q.front().first, y = q.front().second; q.pop_front();
		ans = max(ans,dist[x][y]);
		if (x-1 >= 1 && dat[x][y] == dat[x-1][y] && dist[x-1][y] >= inf) dist[x-1][y] = dist[x][y], q.push_front({x-1,y}); 
		if (x+1 <= n && dat[x][y] == dat[x+1][y] && dist[x+1][y] >= inf) dist[x+1][y] = dist[x][y], q.push_front({x+1,y}); 
		if (y-1 >= 1 && dat[x][y] == dat[x][y-1] && dist[x][y-1] >= inf) dist[x][y-1] = dist[x][y], q.push_front({x,y-1}); 
		if (y+1 <= m && dat[x][y] == dat[x][y+1] && dist[x][y+1] >= inf) dist[x][y+1] = dist[x][y], q.push_front({x,y+1}); 
		if (x-1 >= 1 && dat[x][y] != dat[x-1][y] && dat[x-1][y] && dist[x-1][y] >= inf) dist[x-1][y] = dist[x][y]+1, q.push_back({x-1,y}); 
		if (x+1 <= n && dat[x][y] != dat[x+1][y] && dat[x+1][y] && dist[x+1][y] >= inf) dist[x+1][y] = dist[x][y]+1, q.push_back({x+1,y}); 
		if (y-1 >= 1 && dat[x][y] != dat[x][y-1] && dat[x][y-1] && dist[x][y-1] >= inf) dist[x][y-1] = dist[x][y]+1, q.push_back({x,y-1}); 
		if (y+1 <= m && dat[x][y] != dat[x][y+1] && dat[x][y+1] && dist[x][y+1] >= inf) dist[x][y+1] = dist[x][y]+1, q.push_back({x,y+1}); 
	}
}
 
void solve(){  
	int n,m; cin >> n >> m; 
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			char g; cin >> g; 
			if (g == '.') dat[i][j] = 0; 
			else if (g == 'R') dat[i][j] = 1, must++; 
			else dat[i][j] = 2, must++; 
		}
	}
	if (must == 0){cout << 0 << endl; return;}
	dij(n,m); 
	cout << ans << endl; 
}
 
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t; t=1; //cin >> t;
	while(t--){solve();}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...