bfs求助,感觉木有问题
查看原帖
bfs求助,感觉木有问题
580135
docxjun楼主2022/7/4 20:52

rt


#include<bits/stdc++.h>
using namespace std;
struct cp
{
	int x,y;
};
const int N = 105;
string a[N];
int walk[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{1,-1},{-1,1},{1,1},{-1,-1}};
int main()
{
	int n,m,ans = 0;
	cin>>n>>m;
	for(int i = 1; i <= n; i++)
	{
		cin>>a[i];
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			if(a[i][j] == 'W')
			{
				queue<cp> q;
				cp wasd = {i,j};
				q.push(wasd);
				a[i][j] = '.';
				while(!q.empty())
				{
					cp dsaw = q.front();
					q.pop();
					int nx = dsaw.x;
					int ny = dsaw.y;
					a[nx][ny] = '.';
					for(int i = 0 ; i< 8; i++)
					{
						int tx = nx+walk[i][0];
						int ty = ny+walk[i][1];
						if(tx<1||tx>n||ty<1|ty>m||a[tx][ty] != 'W')
						{
							continue;
						}			
						cp esdf = {tx,ty};
						q.push(esdf);
						a[tx][ty] = '.';		
					}
				}
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

求巨佬帮看

2022/7/4 20:52
加载中...