Submission #307087

#TimeUsernameProblemLanguageResultExecution timeMemory
307087RainbowbunnyTracks in the Snow (BOI13_tracks)C++17
100 / 100
1492 ms89212 KiB
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;
 
const long long INF = 1e15;
const int mod = 998244353;//1e9 + 7;//786433;
const double Pi = acos(-1);
 
void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}
 
int h, w;
char g;
char a[4005][4005];
queue <pair <int, int> > BFS, Wait;
int cnt = 0;
bool Vis[4005][4005], Marked[4005][4005];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
 
signed main()
{
	Fastio();
	cin >> h >> w;
	for(int i = 1; i <= h; i++)
	{
		for(int j = 1; j <= w; j++)
		{
			cin >> a[i][j];
		}
	}
	Wait.push(mp(1, 1));
	while(Wait.empty() == false)
	{
		++cnt;
		g = a[Wait.front().fi][Wait.front().se];
		while(Wait.empty() == false)
		{
			BFS.push(Wait.front());
			Wait.pop();
		}
		while(BFS.empty() == false)
		{
			int x = BFS.front().fi, y = BFS.front().se;
			Vis[x][y] = true;
			BFS.pop();
			for(int i = 0; i < 4; i++)
			{
				int tx = x + dx[i], ty = y + dy[i];
				if(!Vis[tx][ty] && a[tx][ty] == g)
				{
					BFS.push(mp(tx, ty));
					Vis[tx][ty] = true;
				}
				else if(!Vis[tx][ty] && a[tx][ty] != '.' && a[tx][ty] != 0 && !Marked[tx][ty])
				{
					Marked[tx][ty] = true;
					Wait.push(mp(tx, ty));
				}
			}
		}
	}
	cout << cnt;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...