Submission #1316245

#TimeUsernameProblemLanguageResultExecution timeMemory
1316245tymusTracks in the Snow (BOI13_tracks)C++20
100 / 100
1091 ms208884 KiB
#include <bits/stdc++.h>

#define f first
#define s second

int main()
{
	int h, w;
	std::cin >> h >> w;
	
	std::vector<std::vector<char>> a(h, std::vector<char>(w));
	
	for (int i = 0; i < h; i++)
		for (int j = 0; j < w; j++)
			std::cin >> a[i][j];
			
	std::deque<std::pair<std::pair<int,int>, int>> dq;
	std::vector<std::vector<int>> dist(h, std::vector<int>(w, -1));
	
	dq.push_front({{0,0},1});
	dq.push_front({{0,0},1});
	
	int res = 0;
	
	while (!dq.empty())
	{
		auto curr = dq.front(); dq.pop_front();
		
		// lepszy juz zostal ustawiony
		if (dist[curr.f.f][curr.f.s] != -1)
			continue;
			
		dist[curr.f.f][curr.f.s] = curr.s;
		res = std::max(res, curr.s);
		
		const std::pair<int, int> dirs[4] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
		
		for (auto dir : dirs)
		{
			int x = curr.f.f + dir.f;
			int y = curr.f.s + dir.s;
			
			if (x >= 0 & y >= 0 && x < h && y < w)
			{
				if (dist[x][y] != -1)
					continue;
				if (a[x][y] == '.')
					continue;
				
				if (a[curr.f.f][curr.f.s] != a[x][y])
					dq.push_back({{x,y}, dist[curr.f.f][curr.f.s]+1});
				else
					dq.push_front({{x, y}, dist[curr.f.f][curr.f.s]});
			}
		}
	}
	
	std::cout << res << '\n';
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...