64分求助
查看原帖
64分求助
1179906
hhy8399楼主2024/5/19 11:50
#include<bits/stdc++.h>
using namespace std;

int n,cnt;
int a[40][40];
int b[40][40];

void dfs(int x,int y)
{
	if (x <= 0 || x >= n + 1 || y <= 0|| y >= n+1|| a[x][y] != 0) return ;
	a[x][y] = 2;
	dfs(x + 1,y);
	dfs(x - 1,y);
	dfs(x,y + 1);
	dfs(x,y - 1);
}

void dfs2(int x,int y)//写一个标记外围不用涂色的部分主函数开始时运行,循环上色时跳过已标记区域
{
	if (x < 0 || x > n + 1 || y < 0|| y > n+1|| b[x][y] != 0) return ;
	b[x][y] = 2;
	dfs2(x + 1,y);
	dfs2(x - 1,y);
	dfs2(x,y + 1);
	dfs2(x,y - 1);
}


int main()
{
	cin >> n;
	for(int i = 1;i <= n; i++)
	{
		for(int j = 1;j <= n;j++)
		{
			cin >> a[i][j];
			b[i][j] = a[i][j];
		}
	}
	dfs2(1,1);//标记外围
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			if(b[i][j] == 2)//标记过的外围跳过
			{
				continue;
			}
			if(a[i][j] == 0)
			{
				dfs(i,j);
			}
		}
		}
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

#3 && #6 WA,求调

2024/5/19 11:50
加载中...