#pragma GCC optimize("unroll-loops,Ofast,O3")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define inf 1000000009
#define MOD 998244353
#define lim 200005
using namespace std;
void solve(){
int n,m; cin >> n >> m;
string ans="";
char arr[n+1][m+1];
for(int i=1; i<=n; i++){
string s; cin >> s;
for(int j=1; j<=m; j++){
arr[i][j] = s[j-1];
}
}
ans += arr[1][1];
set<ii> bfs, bfs2;
bfs.insert({1,1});
for(int i=1; i<=n+m-2; i++){
int mini=inf;
for(auto p:bfs){
if(p.st<n){
mini = min(mini, (long long) arr[p.st+1][p.nd]-'a');
}
if(p.nd<m){
mini = min(mini, (long long) arr[p.st][p.nd+1]-'a');
}
}
for(auto p:bfs){
if(p.st<n){
if(arr[p.st+1][p.nd]-'a' == mini) bfs2.insert({p.st+1, p.nd});
}
if(p.nd<m){
if(arr[p.st][p.nd+1]-'a' == mini) bfs2.insert({p.st, p.nd+1});
}
}
ans += (mini+'a');
swap(bfs, bfs2);
bfs2.clear();
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
/*freopen("fcolor.in","r",stdin);
freopen("fcolor.out","w",stdout);*/
int t=1;
//cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
2912 KB |
Output is correct |
8 |
Correct |
9 ms |
8228 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
7 ms |
8284 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
50 ms |
3668 KB |
Output is correct |