제출 #973340

#제출 시각아이디문제언어결과실행 시간메모리
973340MvKaioPohlepko (COCI16_pohlepko)C++17
65 / 80
1016 ms65536 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);

typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

int main() {
	fast_io;

	int n, m;
	cin >> n >> m;

	vector<string> grid(n);
	for (auto &x : grid) cin >> x;

	string ans(1, grid[0][0]);
	vector<pair<int, int>> layer = {{0, 0}};
	for (int it = 1; it < n + m - 1; it++) {
		char mn = 'z';
		for (auto [i, j] : layer) {
			if (i + 1 < n) {
				mn = min(mn, grid[i + 1][j]);
			}
			if (j + 1 < m) {
				mn = min(mn, grid[i][j + 1]);
			}
		}
		ans += mn;
		vector<pair<int, int>> next_layer;
		for (auto [i, j] : layer) {
			if (i + 1 < n && grid[i + 1][j] == mn) {
				next_layer.emplace_back(i + 1, j);
			}
			if (j + 1 < m && grid[i][j + 1] == mn) {
				next_layer.emplace_back(i, j + 1);
			}
		}
		layer = next_layer;
	}
	cout << ans << endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...