全WA了【哭】
查看原帖
全WA了【哭】
1214348
Wardin楼主2024/1/6 22:41
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x, y;	
};
node q[10000];
int n, m, sum=0;
int a[102][102];
int vis[102][102], dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
void bfs(int sx, int sy)
{
	int h, t;
	h = t = 1; 
	q[h].x = sx;
	q[h].y = sy;
	while (h <= t)
	{
		for (int i = 0; i < 4; i ++)
		{
			int nx = q[h].x + dx[i], ny = q[h].y + dy[i];
			if (a[nx-1][ny] == 0 && a[nx+1][ny] == 0 && a[nx][ny-1] == 0 && a[nx][ny+1] == 0)
			{
				sum ++;
				return ;
			}
			else
			{
				if (a[nx][ny] != 0 && nx >= 1 && nx <= n && ny >= 1 && ny <= m)
				{
					t ++;
					q[t].x = nx;
					q[t].y = ny;
					a[nx][ny] = 0;
				}
			}
		}
		h ++;
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i ++)
	{
		for (int j = 1; j <= m; j ++)
		{
			scanf("%1d", &a[i][j]);
		}
	}
	for (int i = 1; i <= n; i ++)
	{
		for (int j = 1; j <= m; j ++)
		{
			if (a[i][j] != 0) bfs(i, j);
		}
	}
	cout << sum;
}

思路:读入矩阵,外面围一圈0。

然后循环,只要 a[i][j] 不是0,就开始 bfs,如果当前位置上下左右都是0,计数器加1,返回。 否则继续搜索。

最后输出。

为啥样例都过不了,很难受?,研究好几天了。

2024/1/6 22:41
加载中...