# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
593765 | messiuuuuu | Pohlepko (COCI16_pohlepko) | C++14 | 27 ms | 19912 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 2e3 + 5;
const ll INF = 1e18 + 5;
int m, n;
char c[MAXN][MAXN];
void Input()
{
cin >> m >> n;
for (int i = 1; i <= m; i++)
{
string s;
cin >> s;
for (int j = 0; j < n; j++)
c[i][j + 1] = s[j];
}
}
bool trace[MAXN][MAXN], ava[MAXN][MAXN], ava2[MAXN][MAXN];
pair<int, int> down(pair<int, int> u)
{
return {u.fi + 1, u.se};
}
pair<int, int> rght(pair<int, int> u)
{
return {u.fi, u.se + 1};
}
bool chk(pair<int, int> u)
{
return (u.fi >= 1 && u.se >= 1 && u.fi <= m && u.se <= n);
}
void Solve()
{
deque<pair<int, int>> Q;
Q.pb({1, 1});
ava[1][1] = 1;
while (true)
{
if (ava[m][n])
break;
vector<pair<int, int>> vc;
while (!Q.empty())
{
auto u = Q.front();
//cerr << u.fi << ' ' << u.se << '\n';
auto v = down(u);
if (chk(v) && !ava2[v.fi][v.se])
{
ava2[v.fi][v.se] = 1;
trace[v.fi][v.se] = 0;
vc.pb(v);
}
v = rght(u);
if (chk(v) && !ava2[v.fi][v.se])
{
ava2[v.fi][v.se] = 1;
trace[v.fi][v.se] = 1;
vc.pb(v);
}
Q.pop_front();
}
char minc = 'z';
for (auto p : vc)
minc = min(minc, c[p.fi][p.se]);
for (auto p : vc)
{
//cerr << p.fi << ' ' << p.se << '\n';
ava2[p.fi][p.se] = 0;
if (c[p.fi][p.se] == minc && !ava[p.fi][p.se])
{
Q.pb(p);
ava[p.fi][p.se] = 1;
}
}
}
vector<char> res;
pair<int, int> en = {m, n};
while (en != make_pair(1, 1))
{
res.pb(c[en.fi][en.se]);
if (trace[en.fi][en.se])
en.se--;
else
en.fi--;
}
res.pb(c[1][1]);
reverse(res.begin(), res.end());
for (char t : res)
cout << t;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".INP","r"))
{
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
Input();
Solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |