Dmaxiya

P8693 [蓝桥杯 2019 国 AC] 大胖子走迷宫 参考代码

Feb 6th, 2026
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 300 + 100;
  8. const int INF = 0x3f3f3f3f;
  9. struct Node {
  10.     int x, y, a, dis;
  11.     Node() {}
  12.     Node(int x, int y, int a, int dis) : x(x), y(y), a(a), dis(dis) {}
  13. };
  14.  
  15. bool operator<(const Node &a, const Node &b) {
  16.     return a.dis > b.dis;
  17. }
  18.  
  19. int n, k, ans;
  20. char str[maxn][maxn];
  21. int dis[maxn][maxn][3];
  22. bool vis[maxn][maxn][3];
  23. priority_queue<Node> que;
  24. const int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
  25.  
  26. bool in(int x, int y) {
  27.     return x >= 0 && x < n && y >= 0 && y < n;
  28. }
  29.  
  30. bool canGo(int x, int y, int a) {
  31.     for (int i = -a; i <= a; ++i) {
  32.         for (int j = -a; j <= a; ++j) {
  33.             int xx = x + i;
  34.             int yy = y + j;
  35.             if (!in(xx, yy) || str[xx][yy] == '*') {
  36.                 return false;
  37.             }
  38.         }
  39.     }
  40.     return true;
  41. }
  42.  
  43. int main() {
  44. #ifdef ExRoc
  45.     freopen("test.txt", "r", stdin);
  46. #endif // ExRoc
  47.     ios::sync_with_stdio(false);
  48.  
  49.     cin >> n >> k;
  50.     for (int i = 0; i < n; ++i) {
  51.         cin >> str[i];
  52.     }
  53.     memset(dis, 0x3f, sizeof(dis));
  54.     que.push(Node(2, 2, 2, 0));
  55.     dis[2][2][2] = 0;
  56.     while (!que.empty()) {
  57.         Node tmp = que.top();
  58.         que.pop();
  59.         if (vis[tmp.x][tmp.y][tmp.a]) {
  60.             continue;
  61.         }
  62.         vis[tmp.x][tmp.y][tmp.a] = true;
  63.         for (int i = 0; i < 4; ++i) {
  64.             int x = tmp.x + dir[i][0];
  65.             int y = tmp.y + dir[i][1];
  66.             if (!in(x, y) || str[x][y] == '*') {
  67.                 continue;
  68.             }
  69.             for (int a = tmp.a; a >= 0; --a) {
  70.                 if (!canGo(x, y, a)) {
  71.                     continue;
  72.                 }
  73.                 int d = max(tmp.dis + 1, (2 - a) * k + 1);
  74.                 if (d < dis[x][y][a]) {
  75.                     dis[x][y][a] = d;
  76.                     que.push(Node(x, y, a, d));
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     ans = INF;
  82.     for (int i = 0; i < 3; ++i) {
  83.         ans = min(ans, dis[n - 3][n - 3][i]);
  84.     }
  85.     cout << ans << endl;
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment