제출 #556874

#제출 시각아이디문제언어결과실행 시간메모리
556874HanksburgerTracks in the Snow (BOI13_tracks)C++17
100 / 100
894 ms984636 KiB
#include <bits/stdc++.h>
using namespace std;
pair<int, int> dir[4]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pair<int, int> > vec[2];
string a[4000];
int n, m, ans;
bool ok(pair<int, int> u)
{
	return (u.first>=0 && u.first<n && u.second>=0 && u.second<m && a[u.first][u.second]!='.');
}
void dfs(pair<int, int> u)
{
	char x=a[u.first][u.second];
	a[u.first][u.second]='.';
	for (pair<int, int> i:dir)
	{
		pair<int, int> v=u;
		v.first+=i.first;
		v.second+=i.second;
		if (ok(v))
		{
			if (a[v.first][v.second]==x)
				dfs(v);
			else
				vec[(ans&1)^1].push_back(v);
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i=0; i<n; i++)
		cin >> a[i];
	vec[0].push_back({0, 0});
	while (vec[ans&1].size())
	{
		for (pair<int, int> u:vec[ans&1])
			if (a[u.first][u.second]!='.')
				dfs(u);
		vec[ans&1].clear();
		ans++;
	}
	cout << ans;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...