#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define MX (int)3e5+5
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
signed main()
{
fast();
int n, m;
cin >> n >> m;
char grid[n+1][m+1];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
cin >> grid[i][j];
}
string curr = "";
curr.pb(grid[1][1]);
set<in> opt;
opt.insert({1,1});
int stages = n+m-2;
while(stages--)
{
vector<pair<in, char>> v;
char val = '{';
for(in ok: opt)
{
if(ok.f < n)
{
v.pb({{ok.f+1, ok.s}, grid[ok.f+1][ok.s]});
val = min(val, grid[ok.f+1][ok.s]);
}
if(ok.s < m)
{
v.pb({{ok.f, ok.s+1}, grid[ok.f][ok.s+1]});
val = min(val, grid[ok.f][ok.s+1]);
}
}
curr.pb(val);
opt.clear();
for(auto aaa: v)
{
if(aaa.s != val)
continue;
opt.insert(aaa.f);
}
}
cout << curr;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
4 ms |
724 KB |
Output is correct |
7 |
Correct |
18 ms |
2892 KB |
Output is correct |
8 |
Correct |
51 ms |
8044 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
5 ms |
968 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
52 ms |
8040 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
84 ms |
3604 KB |
Output is correct |