제출 #378205

#제출 시각아이디문제언어결과실행 시간메모리
378205penguin133Tracks in the Snow (BOI13_tracks)C++14
100 / 100
1778 ms332536 KiB
#include <bits/stdc++.h>
using namespace std;
char g[4005][4005];
int ans[4005][4005];
int visited[4005][4005];
int dx[] = {0,1,0,-1}, dy[] = {1, 0, -1, 0};
inline int ReadInt() {
    int x = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9'){
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar();
	}
    return x;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int h,w,maxi = 0;
	cin >> h >> w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin >> g[i][j];
		}
	}
	for(int i=1;i<=4000;i++)fill(ans[i] , ans[i] + 4005, 2e9  +5);
	deque<pair<int, int> >q;
	q.push_back(make_pair(1, 1));
	ans[1][1] = 0;
	while(!q.empty()){
		int x = q.front().first;
		int y = q.front().second;
		q.pop_front();
		if(visited[x][y] == true)continue;
		visited[x][y] = true;
		for(int i=0;i<4;i++){
			int x1 = x + dx[i];
			int y1 = y + dy[i];
			if(x1 < 1 || x1 > h || y1 < 1 ||y1 > w)continue;
			if(g[x1][y1] == '.')continue;
			int ans1 = 0;
			if(g[x1][y1] != g[x][y])ans1++;
			ans[x1][y1] = min(ans[x1][y1], ans[x][y] + ans1);
			if(ans1 == 0)q.push_front(make_pair(x1, y1));
			else q.push_back(make_pair(x1 , y1));
		}
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(g[i][j] == '.')continue;
			maxi = max(maxi, ans[i][j]);
		}
	}
	cout << maxi + 1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...