bfs优化70求助!
  • 板块P1141 01迷宫
  • 楼主BZHZS
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/9/30 21:26
  • 上次更新2023/10/27 09:26:06
查看原帖
bfs优化70求助!
359492
BZHZS楼主2022/9/30 21:26
#include <bits/stdc++.h>
using namespace std;

struct node
{
	int x, y;
};

const int N = 1010;
const int dx[5] = {0, 0, 0, 1, -1},
          dy[5] = {0, 1, -1, 0, 0};

int n, m, ans[N], xx, yy;
int a[N][N], rmb[N][N], mark[N][N];
queue<node> q;
node tt, ttt;

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			scanf("%1d", &a[i][j]);
	}

	for (int i = 1; i <= m; i++)
	{
		memset(rmb, 0, sizeof(rmb));
		scanf("%d%d", &xx, &yy);
		if (mark[xx][yy])
		{
			printf("%d\n", ans[mark[xx][yy]]);
			continue;
		}
		rmb[xx][yy] = 1;
		mark[xx][yy] = i;
		tt.x = xx;
		tt.y = yy;
		ans[i] = 1;
		q.push(tt);
		while(!q.empty())
		{
			tt = q.front();
			q.pop();
			for (int j = 1; j <= 4; j++)
			{
				xx = tt.x + dx[j];
				yy = tt.y + dy[j];
				if (xx == 0 || xx > n || yy == 0 || yy > n || rmb[xx][yy] == 1 || a[xx][yy] == a[tt.x][tt.y])
					continue;
				ans[i]++;
				mark[xx][yy] = i;
				rmb[xx][yy] = 1;
				ttt.x = xx;
				ttt.y = yy;
				q.push(ttt);
			}
		}
		printf("%d\n", ans[i]);
	}

	return 0;
}
2022/9/30 21:26
加载中...