Untitled
unknown
plain_text
a year ago
1.4 kB
2
Indexable
Never
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ui = uint32_t; using pii = pair<int, int>; #define fi first #define se second #define mp make_pair #define all(x) (x).begin(),(x).end() const int INF = 1e9; vector<pii> moves = { mp(2, 1), mp(2, -1), mp(1, 2), mp(1, -2), mp(-1, 2), mp(-1, -2), mp(-2, 1), mp(-2, -1) }; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int n, m, s, t, queries; cin >> n >> m >> s >> t >> queries; s--, t--; queue<pii> q; vector<vector<int>> dist(n, vector<int>(m, INF)); dist[s][t] = 0, q.push(mp(s, t)); auto isValid = [&](int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (pii& mv: moves) { int dx = x + mv.fi; int dy = y + mv.se; if (isValid(dx, dy) && dist[dx][dy] == INF) { q.push(mp(dx, dy)); dist[dx][dy] = dist[x][y] + 1; } } } ll ans = 0; for (int i = 0; i < queries; ++i) { int x, y; cin >> x >> y; x--, y--; if (dist[x][y] == INF) { cout << -1; return 0; } ans += dist[x][y]; } cout << ans; return 0; }