# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
334182 | apostoldaniel854 | K-th path (IZhO11_kthpath) | C++14 | 5 ms | 384 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 30;
string grid[N];
ll dp[N][N][2];
int n, m;
const ll INF = 1e18;
void add (ll &a, ll b) {
a += b;
if (a > INF)
a = INF;
}
bool check (string &s, ll k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < 2; k++)
dp[i][j][k] = 0;
if (grid[0][0] < s[0])
dp[0][0][1] = 1;
if (grid[0][0] == s[0])
dp[0][0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (s[i + j] > grid[i][j]) {
if (i)
add (dp[i][j][1], dp[i - 1][j][0]), add (dp[i][j][1], dp[i - 1][j][1]);
if (j)
add (dp[i][j][1], dp[i][j - 1][0]), add (dp[i][j][1], dp[i][j - 1][1]);
}
else {
if (i) {
if (s[i + j] == grid[i][j])
add (dp[i][j][0], dp[i - 1][j][0]);
add (dp[i][j][1], dp[i - 1][j][1]);
}
if (j) {
if (s[i + j] == grid[i][j])
add (dp[i][j][0], dp[i][j - 1][0]);
add (dp[i][j][1], dp[i][j - 1][1]);
}
}
}
return dp[n - 1][m - 1][0] + dp[n - 1][m - 1][1] >= k;
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> grid[i];
ll k;
cin >> k;
int sz = n + m - 1;
string ans (sz, 'z');
for (int i = 0; i < sz; i++) {
ans[i] = 'a';
while (not check (ans, k))
ans[i]++;
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |