硬控AT评测机5分钟
查看原帖
硬控AT评测机5分钟
768951
_Chronostatis_楼主2024/7/22 16:15

标题是吸引你进来的,但这个sb代码真的算得上卡评测了。

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 15e2 + 10;

struct Node {
  int x, y;
};

int n, fx, fy, sx, sy, dis[MAXN][MAXN], cnt;
bool a[MAXN][MAXN], vis[4];
queue<Node> q;

bool Check(int x, int y) {
  return (x >= 1 && x <= n && y >= 1 && y <= n);
}

void R(int x, int y, int step, int f) {
  if (Check(x, y)) {
    if (!vis[f]) {
      if (a[x][y]) {
        vis[f] = 1;
        return;
      }
      if (dis[x][y] != -1) {
        return;
      }
      q.push({x, y});
      dis[x][y] = step;
    }
  } else {
    cnt++;
  }
}

void bfs() {
  q.push({fx, fy});
  dis[fx][fy] = 0;
  while (!q.empty()) {
    Node now = q.front();
    q.pop();
    fill(vis, vis + 4, 0);
    int nx, ny;
    for (int d = 1; ; d++) {
      cnt = 0;
      R(now.x + d, now.y + d, dis[now.x][now.y] + 1, 0);
      R(now.x + d, now.y - d, dis[now.x][now.y] + 1, 1);
      R(now.x - d, now.y + d, dis[now.x][now.y] + 1, 2);
      R(now.x - d, now.y - d, dis[now.x][now.y] + 1, 3);
      if (cnt == 4) {
        break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> fx >> fy >> sx >> sy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '#');
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = -1;
    }
  }
  bfs();
  cout << dis[sx][sy];
  return 0;
}

记录

2024/7/22 16:15
加载中...