不知道为什么会MLE,只得了30分
查看原帖
不知道为什么会MLE,只得了30分
99452
BDICOMENOW楼主2022/6/14 20:21
#include<cstdio>
#include<iostream>
#include<cmath>//引入头文件 
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int m, n;
int x, y;

int mm[405][405];
int vis[405][405];

struct cor
{
    int x, y, depth;

};

int dir[8][2] = { {2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1} };

queue<cor> q;

void bfs(cor tmpdir)
{
    q.push(tmpdir);
    while (!q.empty()) {
        cor nextCor = q.front();
        mm[nextCor.x][nextCor.y] = nextCor.depth;
        vis[nextCor.x][nextCor.y] = 1;
        q.pop();
        for (int i = 0; i < 8; i++) {
            cor tmpcor;
            tmpcor.x = nextCor.x + dir[i][0];
            tmpcor.y = nextCor.y + dir[i][1];
            tmpcor.depth = nextCor.depth + 1;
            if (tmpcor.x >= 0 && tmpcor.x < n && tmpcor.y >= 0 && tmpcor.y < m&&!vis[tmpcor.x][tmpcor.y]) {
                q.push(tmpcor);
            }
        }
    }
}



int main()
{
    memset(mm,-1,sizeof(mm));
    cin >> n >> m >> x >> y;
    cor tmp;
    tmp.x = x-1;
    tmp.y = y-1;
    tmp.depth = 0;

    bfs(tmp);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%-5d",mm[i][j]);
        }
        cout << endl;
    }
    
    return 0;//结束程序 
}
2022/6/14 20:21
加载中...