제출 #860696

#제출 시각아이디문제언어결과실행 시간메모리
860696aroradevTracks in the Snow (BOI13_tracks)C++17
100 / 100
645 ms135452 KiB
#include <bits/stdc++.h>
using namespace std;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int h, w;
vector<string> tracks;

bool inside(int x, int y) {
	return (x > -1 && x < h && y > -1 && y < w && tracks[x][y] != '.');
}

int main() {
	cin >> h >> w;
	tracks.resize(h);
	for (int i = 0; i < h; i++) {
		cin >> tracks[i];
	}
	
	deque<pair<int, int>> edges;
	vector<vector<int>> depth(h, vector<int>(w));
	
	edges.push_back({0, 0});
	depth[0][0] = 1;
	
	int ans = 0;
	while (!edges.empty()) {
		auto c = edges.front();
		edges.pop_front();
		
		ans = max(ans, depth[c.first][c.second]);
		
		for (int i = 0; i < 4; i++) {
			int x = c.first + dx[i], y = c.second + dy[i];
			if (!inside(x, y) || depth[x][y] != 0) continue;
			
			if (tracks[x][y] == tracks[c.first][c.second]) {
				depth[x][y] = depth[c.first][c.second];
				edges.push_front({x, y});
			}
			else {
				depth[x][y] = depth[c.first][c.second] + 1;
				edges.push_back({x, y});
			}
		}
	}
	
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...