Submission #334182

#TimeUsernameProblemLanguageResultExecution timeMemory
334182apostoldaniel854K-th path (IZhO11_kthpath)C++14
100 / 100
5 ms384 KiB
#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 timeMemoryGrader output
Fetching results...