-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKth_path.cpp
47 lines (47 loc) · 932 Bytes
/
Kth_path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int N, M, K;
string str;
//char arr[100][100];
vector< string > arr;
vector< string > ans;
void printAllPath(char path[], int i, int j, int n, int m, int idx){
if(i == n - 1){
for(int k = j; k < m; ++k){
path[idx + k - j] = arr[i][k];
}
ans.push_back(path);
return;
}
if(j == m - 1){
for(int k = i; k < n; ++k){
path[idx + k - i] = arr[k][j];
}
ans.push_back(path);
return;
}
path[idx] = arr[i][j];
printAllPath(path, i + 1, j, n, m, idx + 1);
printAllPath(path, i, j + 1, n, m, idx + 1);
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < N; ++i){
cin >> str;
arr.push_back(str);
}
char *path = new char[N + M];
printAllPath(path, 0, 0, N, M, 0);
//scanf("%d\n", &K);
cin >> K;
/*
if(K > (N + M - 1)){
printf("-1\n");
return 0;
}
*/
sort(ans.begin(), ans.end());
cout << ans[K - 1] << "\n";
return 0;
}