WA了 BFS做法 求助(玄关)
查看原帖
WA了 BFS做法 求助(玄关)
975630
woyaoAKIOI0924PTY楼主2024/12/12 21:34
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,dx[]={0,1,-1,0},dy[]={1,0,0,-1},a[100][100],sum;
bool vis[100][100]; 
struct Node{
	int x,y;
};
bool check(int x,int y)
{
	if(x>=0&&x<=n+1&&y>=0&&y<=n+1)
		return true;
	return false; 	
}
void bfs(int x,int y)
{
	queue<Node> q;
	Node s;
	s.x=x;
	s.y=y;
	q.push(s);
	a[s.x][s.y]=true;
    vis[s.x][s.y]=true;
    while(!q.empty())
	{
        Node t=q.front();
        q.pop();
        for(int i=0;i<4;++i)
		{
            Node w;
            w.x=t.x+dx[i];
            w.y=t.y+dy[i];
            if(check(w.x,w.y))
			{
                if(a[w.x][w.y]==0&&!(vis[w.x][w.y]))
				{
                    vis[t.x][t.y]=true;
                    sum++;
                    q.push(t);
                }

            }

        }
    }

}
signed main()
{
	cin>>n>>m;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            cin>>a[i][j];
    bfs(0,0);
	return 0;
}
2024/12/12 21:34
加载中...