Submission #748427

#TimeUsernameProblemLanguageResultExecution timeMemory
748427mariowongTracks in the Snow (BOI13_tracks)C++14
100 / 100
1999 ms136380 KiB
#include <bits/stdc++.h>
using namespace std;
 
struct dir{
	int vt,hz;
}a[8]={{0,1},{0,-1},{1,0},{-1,0}};
 
int h,w,ct,mv,ans,dis[4005][4005],len,x,y,newx,newy;
char s[4005][4005],c;
bool vis[4005][4005];
queue <pair<int,pair<pair<int,int>,char>  >  > q1,q2; 
int main(){
	ios::sync_with_stdio(false);
	cin >> h >> w;
	for (int i=1;i<=h;i++){
		for (int j=1;j<=w;j++){
			cin >> s[i][j];
			if (s[i][j] != '.')
			ct++;
		}
	}
	q1.push(make_pair(-1,make_pair(make_pair(1,1),s[1][1])));
	while (!q1.empty() || !q2.empty()){
		if (q2.empty() || (!q1.empty() && -q2.front().first >= -q1.front().first)){
			x=q1.front().second.first.first; y=q1.front().second.first.second;
			len=-q1.front().first; c=q1.front().second.second; 
			q1.pop();
		}
		else
		{
			x=q2.front().second.first.first; y=q2.front().second.first.second;
			len=-q2.front().first; c=q2.front().second.second; 
			q2.pop();
		}
		if (!vis[x][y]){
			vis[x][y]=true; dis[x][y]=len;
			for (int i=0;i<4;i++){
				newx=x+a[i].vt; newy=y+a[i].hz;
				if (newx > 0 && newx <= h && newy > 0 && newy <= w && !vis[newx][newy] && s[newx][newy] != '.'){
					if (c != s[newx][newy])
					q1.push(make_pair(-len-1,make_pair(make_pair(newx,newy),s[newx][newy])));
					else
					q2.push(make_pair(-len,make_pair(make_pair(newx,newy),s[newx][newy])));
				}
			}
		}
	}
	for (int i=1;i<=h;i++){
		for (int j=1;j<=w;j++){
			ans=max(ans,dis[i][j]);
		}
	}
	cout << ans << "\n";
	return 0;
}	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...