#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k;
char a[35][35];
vector<string> all;
void dfs(int x, int y, string c) {
if(x == n && y == m) {
all.push_back(c);
return;
}
if(x + 1 <= n ) {
dfs(x + 1, y, c + a[x + 1][y]);
}
if(y + 1 <= m) {
dfs(x, y + 1, c + a[x][y + 1]);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> k;
string u = "";
u += a[1][1];
dfs(1, 1, u);
sort(all.begin(), all.end());
cout << all[k - 1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |