马的遍历 为什么0分???

666…………
老师都用struct你不用

3 个赞

你拿这个改啊
@鲁子欣

2 个赞

老师这道题没用

2 个赞

在这个后面加个ans[xx][yy]=q.front().e;

2 个赞

ans是记录步数的

2 个赞

然后呢 :question: :question: :question:

3 个赞

输出
没了

4 个赞

钉钉

2 个赞

[quote=“萌新, post:1, topic:22008, username:鲁子欣”]

#include<bits/stdc++.h>
using namespace std;
int maxa = 0, fx[8]={2,1,1,2,-2,-1,-1,-2 }, fy[8]={-1,-2,2,1,-1,-2,2,1}, a, s,  l[410][410] = {},x,y;
int o[410][410];
queue<int>q;
void bfs() {
    while (!q.empty()) {
        x = q.front();
        q.pop();
        y = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int xx = x + fx[i], yy = y + fy[i];
            if (xx>=1&&yy>=1&&xx<a&&yy<s&&o[xx][yy]==-1) {//这里应该是xx <= a yy <=s
                o[xx][yy] = o[xx][yy]+1;//这里应该是o[xx][yy] = o[x][y] + 1;吧
                q.push(xx);
                q.push(yy);
            }
        }
    }
}
int main()
{
    cin >> a >> s >> x >> y;
    memset(o, -1, sizeof o);
    o[x][y]=0;
    q.push(x);
    q.push(y);
    bfs();
    for (int i = 1; i <= a; i++) {
        for (int j = 1; j <= s; j++) {
            cout << o[i][j]<<" ";
        }
        cout << endl;
    }
    return 0;
}
2 个赞

改完是可以ac的

2 个赞

bfs不一定要结构体

2 个赞

是的,但 dijkstra 求最短路要用 priority\_queue

2 个赞