求助
查看原帖
求助
740610
Gospodaru楼主2023/9/10 20:41

WA了最后一个点

#include<bits/stdc++.h>
using namespace std;
long long n,a[50][50],f[50][50];
bool flag=0;
void dfs(int x,int y)
{
	if(flag == 1) return;
	f[x][y] = 1;
	if(a[x + 1][y]==-1) flag = 1;
	else if(a[x - 1][y] == -1) flag = 1;
	else if(a[x][y + 1] == -1) flag = 1;
	else if(a[x][y - 1] == -1) flag = 1;
	else if(a[x + 1][y] == 0 && !f[x + 1][y]) dfs(x + 1,y);
	else if(a[x - 1][y] == 0 && !f[x - 1][y]) dfs(x - 1,y);
	else if(a[x][y + 1] == 0 && !f[x][y + 1]) dfs(x,y + 1);
	else if(a[x][y - 1] == 0 && !f[x][y - 1]) dfs(x,y - 1);
	f[x][y]=0;
}
void dfs2(int x,int y)
{
	a[x][y] = 2;
	if(a[x + 1][y] == 0) dfs(x + 1,y);
	if(a[x - 1][y] == 0) dfs(x - 1,y);
	if(a[x][y + 1] == 0) dfs(x,y + 1);
	if(a[x][y - 1] == 0) dfs(x,y - 1);
}
int main()
{
	cin>>n;
 	memset(a,-1,sizeof(a));
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			cin>>a[i][j];
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			if(a[i][j] == 0)
			{
				flag = 0;
				dfs(i,j);
				if(flag == 0)
					dfs2(i,j);
			}
		}
	}
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
			cout<<a[i][j]<<" ";
		cout<<"\n";
	}
	
	return 0;
}
2023/9/10 20:41
加载中...