ac了但是还是有疑问
  • 板块P1162 填涂颜色
  • 楼主Zeeed
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/3/11 21:51
  • 上次更新2024/3/12 12:38:14
查看原帖
ac了但是还是有疑问
1134175
Zeeed楼主2024/3/11 21:51
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
int n, a[35][35], vis[35][35];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int flag;
queue <PII> q;
void bfs(int u, int v)
{
	flag = 0;
	q.push({u, v});
	memset(vis, 0, sizeof vis);
	vis[u][v] = 1;
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			int x = t.first + dx[i], y = t.second + dy[i];
			if(a[x][y] == 1) continue;
			if(vis[x][y]) continue;
			if(x < 0 || x > n - 1||y < 0 || y > n - 1)
			{
				flag = 1;
				return ;//不在闭合圈
			}
			q.push({x, y});
			vis[x][y] = 1;
		}
	}
	if(!flag)
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if(vis[i][j]) a[i][j] = 2;
	return ;
}
int main ()
{
	cin >> n;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			cin >> a[i][j];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
		{
			if(!vis[i][j] && a[i][j] != 1) bfs(i, j); 
		}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
			cout << a[i][j] << " ";
		cout << "\n";
	}
}

上面的代码有一个测试点没过 但是我将27行的判断边界x<0||y<0 改为x<1||y<1就过了...一直没想明白为啥

2024/3/11 21:51
加载中...