bfs的毛病(Merge_all勿扰)
查看原帖
bfs的毛病(Merge_all勿扰)
1562347
yjj13621355890楼主2025/8/4 10:56
#include<iostream>
#include<queue>
using namespace std;
int n,m,a,b,cnt;
struct node
{
    int x,y;
};
queue<node>q;
void bfs(node start)
{
    q.push(start);
    while(!q.empty())
    {
        node u=q.front();
        q.pop();
        int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
        for(int i=0;i<4;i++)
        {
            node v={u.x+dx[i],u.y+dy[i]};
            if(v.x>=0&&v.x<n&&v.y>=0&&v.y>=n)
            {
                int horse[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
                bool flag=1;
                for(int i=0;i<8;i++)
                    if(v.x==horse[i][0]+a&&v.y==horse[i][1]+b)
                        flag=0;
                if(flag)
                {
                    q.push(v);
                    cnt++;
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m>>a>>b;
    bfs({0,0});
    cout<<cnt;
    return 0;
}
2025/8/4 10:56
加载中...