蒟蒻求助,第一次写BFS爆零了

P2385 [USACO07FEB] Bronze Lilypad Pond B

Ryzen_9_7950X @ 2023-12-13 21:03:57

#include <bits/stdc++.h>
using namespace std;
int m,n,m1,m2;
#define f1(a) for(int i = 1;i <= a;i++)
#define f2(b) for(int j = 1;j <= b;j++)
#define dm(x) x > 0 && x <= m
#define dn(x) x > 0 && x <= n
int a[51][51];
queue<pair<int,int> > b;
int _round_ = 0;
int main()
{
    int x1,x2,y1,y2;
    scanf("%d %d %d %d",&m,&n,&m1,&m2);
    f1(m)
    {
        f2(n)
        {
            scanf("%d",&a[i][j]);
            if(a[i][j] == 3)
            {
                x1 = i;
                y1 = j;
            }
        }
    }
    b.push(make_pair(x1,y1));
    int dx[8] = {m2,m1,-m1,-m2,-m2,-m1,m1,m2};
    int dy[8] = {m1,m2,m2,m1,-m1,-m2,-m2,-m1};
    while(1)
    {
        _round_++;
        int kkksc03 = b.size();
        f1(kkksc03)
        {
            if(a[b.front().first][b.front().second] == 4)
            {
                printf("%d",_round_);
                return 0;
            }
            f2(8)
            {
                if((dm(b.front().first + dx[j - 1]) && dn(b.front().second + dy[j - 1])) && a[b.front().first + dx[j - 1]][b.front().second + dy[j - 1]] == 1) b.push(make_pair(b.front().first + dx[j - 1],b.front().second + dy[j - 1]));
            }
            a[b.front().first][b.front().second] = 0;
            b.pop();
        }
    }
    return 0;
}
本蒟蒻写了一个BFS,提交时爆零了,查遍题解无济于事,求各位神犇出手相助,谢各位!

|