Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define mp make_pair
- #define x first
- #define y second
- using namespace std;
- void cass(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- freopen("sknight.inp", "r", stdin);
- freopen("sknight.out", "w", stdout);
- }
- const int B = 1e6 + 1;
- const int HB = 1e3 + 1;
- const int N = 1001;
- long long tx[10] = {0, -1, 1, 1, -1, -1, -1, 1, 1};
- //int tx[10] = {0, -2, -2, 2, 2, -1, -1, 1, 1};
- //int ty[10] = {0, -1, 1, -1, 1, -2, 2, -2, 2};
- long long ty[10] = {0, -1, 1, -1, 1, -1, 1, -1, 1};
- long long k, l;
- int xt, yt;
- int n;
- pair<int, int > ban[N];
- void init(){
- k--; l--;
- for (int i = 1; i <= 4; i++){
- tx[i] *= k;
- ty[i] *= l;
- }
- for (int i = 5; i <= 8; i++){
- tx[i] *= l;
- ty[i] *= k;
- }
- }
- int deg[2 * HB][2 * HB];
- int tab[2 * HB][2 * HB];
- void sub2(){
- for (int i = 1; i <= n; i++) tab[ban[i].x + HB][ban[i].y + HB] = 1;
- queue<pair<int, int > > Q;
- Q.push(mp(xt + HB, yt + HB));
- memset(deg, -1, sizeof(deg));
- deg[xt + HB][yt + HB] = 0;
- while (!Q.empty()){
- pair<int, int > pre = Q.front(); Q.pop();
- for (int t = 1; t <= 8; t++){
- int nx = pre.x + tx[t];
- int ny = pre.y + ty[t];
- if (1 <= nx && nx <= 2001 && 1 <= ny && ny <= 2001 && deg[nx][ny] == -1 && tab[nx][ny] == 0){
- deg[nx][ny] = deg[pre.x][pre.y] + 1;
- Q.push(mp(nx, ny));
- if (nx == HB && ny == HB){
- cout << deg[nx][ny];
- exit(0);
- }
- }
- }
- }
- cout << -1;
- }
- int naive(int fixedx, int fixedy){
- queue<pair<int, int > > Q;
- Q.push(mp(fixedx , fixedy ));
- memset(deg, -1, sizeof(deg));
- deg[fixedx ][fixedy ] = 0;
- while (!Q.empty()){
- pair<int, int > pre = Q.front(); Q.pop();
- for (int t = 1; t <= 8; t++){
- int nx = pre.x + tx[t];
- int ny = pre.y + ty[t];
- if (0 <= nx && nx <= 2 * HB && 0 <= ny && ny <= 2 * HB && deg[nx][ny] == -1){
- deg[nx ][ny] = deg[pre.x][pre.y ] + 1;
- Q.push(mp(nx, ny));
- if (nx == HB && ny == HB){
- return deg[nx][ny];
- }
- }
- }
- }
- return -1;
- }
- int numPre = 0;
- pair<int, int > prepare(){
- long long inf = 1e15 + 6;
- // go from xt, yt (haved fixed) to the 1..2001 x 1.. 2001 field
- long long curX = abs(xt), curY = abs(yt);
- while (1){
- if ( - HB <= curX && curX <= HB && - HB <= curY && curY <= HB) return mp(curX + HB, curY + HB);
- long long minDis = inf;
- long long minIdX {}, minIdY{};
- for (int t = 1; t <= 8; t++){
- long long nx = curX + tx[t];
- long long ny = curY + ty[t];
- if ((nx) * (nx) + (ny) * (ny) < minDis){
- minDis = (nx) * (nx) + (ny) * (ny);
- minIdX = nx;
- minIdY = ny;
- }
- }
- curX = minIdX;
- curY = minIdY;
- numPre++;
- }
- }
- void full(){
- pair<int, int > stpoint = prepare();
- int tmp = naive(stpoint.x, stpoint.y);
- //if (tmp != -1)
- cout << numPre + tmp;
- }
- int main(){
- cass();
- cin >> k >> l;
- cin >> xt >> yt;
- cin >> n;
- for (int i = 1; i <= n; i++) cin >> ban[i].first >> ban[i].second;
- init();
- if (n){
- sub2();
- }
- else full();
- }
Add Comment
Please, Sign In to add comment