2 条题解

  • 1
    @ 2023-3-6 20:33:10

    /*****************************************
    ±¸×¢£º
    ******************************************/
    #include <queue>
    #include <set>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define LL long long
    const int N = 1e4 + 10;
    const int INF = 0x3f3f3f3f;
    struct node {
        int x, y;
    
        node(int xx, int yy) {
            x = xx, y = yy;
        }
    };
    int n, m, x, y;
    queue<node> q;
    int a[N][N];
    int vis[N][N];
    int dx[] = {-2, -2, -1, 1, 2, 2, 1, -1};
    int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2};
    void bfs() {
        q.push(node(x, y));
        a[x][y] = 0;
        vis[x][y] = 1;
        while (!q.empty()) {
            int x = q.front().x;
            int y = q.front().y;
            q.pop();
            for (int i = 0; i < 8; i++) {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && !vis[xx][yy]) {
                    a[xx][yy] = a[x][y] + 1;
                    q.push(node(xx, yy));
                    vis[xx][yy] = 1;
                }
            }
        }
    }
    int main() {
        cin >> n >> m >> x >> y;
        memset(a, -1, sizeof a);
        bfs();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                printf("%-5d", a[i][j]);
            }
            cout << endl;
        }
        return 0;
    }
    
    
    
    

    • 0
      @ 2024-3-10 18:01:43
      #include <queue>
      #include <cstring>
      using namespace std;
      int N,M,sn,sm;
      int a[500][500];
      int dx[] = {-1,-2,-2,-1,1,2,2,1};
      int dy[] = {-2,-1,1,2,2,1,-1,-2};
      bool vis[500][500] = {false};
      
      struct Node
      {
          int x;
          int y;
          int step;
      };
      queue<Node>q;
      
      bool ia(int x,int y)
      {
          if (x >= 1 && x <= N && y >= 1 && y <= M)
          {
              return true;
          }
          return false;
      }
      
      void bfs(int x,int y)
      {
          q.push((Node){x,y,0});
          vis[x][y] = true;
          a[x][y] = 0;
          while (!q.empty())
          {
              Node temp = q.front();
              q.pop();
              for (int i = 0;i < 8;i++)
              {
                  int nx = temp.x + dx[i];
                  int ny = temp.y + dy[i];
                  if (ia(nx,ny) == true && vis[nx][ny] == false)
                  {
                      q.push((Node){nx,ny,temp.step + 1});
                      a[nx][ny] = temp.step + 1;
                      vis[nx][ny] = true;
                  }
              }
          }
      }
      
      int main()
      {
      	memset(a,-1,sizeof a);
          cin >> N >> M >> sn >> sm;
          bfs(sn,sm);
          for (int i = 1;i <= N;i++)
          {
              for (int j = 1;j <= M;j++)
              {
                  printf("%-5d",a[i][j]);
              }
              cout << endl;
          }
          return 0;
      }
      
      • 1

      信息

      ID
      2495
      时间
      1000ms
      内存
      256MiB
      难度
      7
      标签
      递交数
      497
      已通过
      98
      上传者