Editorial for Chú thỏ đi tìm cà rốt
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Vấn đề này có thể được giải quyết bằng Dynamic time warping
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define fi first
#define se second
const ll MOD = 1000000007;
ll memo[1010][1010];
vector<pll> F, B;
ll solve(int fi, int bi) {
ll base = (F[fi].fi - B[bi].fi) * (F[fi].fi - B[bi].fi) + (F[fi].se - B[bi].se) * (F[fi].se - B[bi].se);
if (fi + 1 == F.size() && bi + 1 == B.size()) return base;
ll &ref = memo[fi][bi];
if (ref != -1) return ref;
if (fi == 0 && bi == 0) base = 0;
ref = LLONG_MAX;
if (fi + 1 < F.size()) ref = min(ref, base + solve(fi + 1, bi));
if (bi + 1 < B.size()) ref = min(ref, base + solve(fi, bi + 1));
if (fi + 1 < F.size() && bi + 1 < B.size()) ref = min(ref, base + solve(fi + 1, bi + 1));
return ref;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
map<char, int> dx, dy;
dx['E'] = 1; dx['W'] = -1; dy['N'] = 1; dy['S'] = -1;
int N, M; cin >> N >> M;
int fx, fy, bx, by; cin >> fx >> fy >> bx >> by;
string SF, SB; cin >> SF >> SB;
F.emplace_back(fx, fy);
for (char c : SF) {
fx += dx[c];
fy += dy[c];
F.emplace_back(fx, fy);
}
B.emplace_back(bx, by);
for (char c : SB) {
bx += dx[c];
by += dy[c];
B.emplace_back(bx, by);
}
memset(memo, -1, sizeof(memo));
cout << solve(0, 0) << endl;
return 0;
}
Comments