求改!!!为什么会MLE!!!
  • 板块P1443 马的遍历
  • 楼主YZZCDS
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/7/12 17:07
  • 上次更新2024/7/12 18:58:25
查看原帖
求改!!!为什么会MLE!!!
1112264
YZZCDS楼主2024/7/12 17:07

#3 #4 #5 #6 #7 #8 #9 全部MLE,请dalao能帮我改改吗?

#include <bits/stdc++.h>
using namespace std;
typedef long long l;
l n, m, m_x, m_y, ans[405][405];
l dx[8] = {-2, -2, 2, 2, 1, -1, 1, -1};
l dy[8] = {-1, 1, -1, 1, 2, -2, -2, 2};
bool visited[405][405];

void bfs(l x, l y) {
    memset(ans, -1, sizeof(ans));
    queue<pair<l, l>> q;
    q.push(pair<l, l>{x, y});
    ans[x][y] = 0;
    while (!q.empty()) {
        pair<l, l> front = q.front();
        visited[front.first][front.second] = true;
        for (int i = 0; i < 8; i ++) {
            l tx, ty;
            tx = front.first + dx[i];
            ty = front.second + dy[i];
            if (tx > 0 && tx <= n && ty > 0 && ty <= m) {
                if (!visited[tx][ty]) {
                    ans[tx][ty] = ans[front.first][front.second] + 1;
                    q.push(pair<l, l>{tx, ty});
                }
            }
        }
        q.pop();
    }
}



int main () {
    cin >> n >> m >> m_x >> m_y;
    bfs(m_x, m_y);
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= m; j ++) {
            printf("%-4d", ans[i][j]);
        }
        cout << endl;
    }
    return 0;
}
2024/7/12 17:07
加载中...