제출 #69494

#제출 시각아이디문제언어결과실행 시간메모리
69494MatheusLealVTracks in the Snow (BOI13_tracks)C++17
100 / 100
1389 ms502976 KiB
#include <bits/stdc++.h>
#define N 4003
#define NN 16000002
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;

int n, m, ans, cnt, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

int dist[N][N];

char mat[N][N], T;

vector<int> grafo[NN];

int bfs01()
{
	deque < pii > bfs;

	bfs.push_back({1, 1});

	memset(dist, 0x3f3f3f3f, sizeof dist);

	dist[1][1] = 1;

	while(!bfs.empty())
	{
		int x = bfs.front().f, y = bfs.front().s;

		bfs.pop_front();

		ans = max(ans, dist[x][y]);

		for(int p = 0; p < 4; p++)
		{
			int a = x + dx[p], b = y + dy[p];

			if(!a or !b or a > n or b > m or mat[a][b] == '.') continue;

			int custo = (mat[a][b] == mat[x][y] ? 0 : 1);

			if(dist[a][b] > dist[x][y] + custo)
			{
				dist[a][b] = dist[x][y] + custo;

				if(!custo) bfs.push_front({a, b});

				else bfs.push_back({a, b});
			} 
		}
	}

	return ans;
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n>>m;

	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) cin>>mat[i][j];

	cout<<bfs01()<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...