20pts求助
查看原帖
20pts求助
973359
liaoiming楼主2025/6/27 17:37

玄关

#include<bits/stdc++.h>
using namespace std;
bool a[1005][1005];
int num[1005][1005];
struct name{
	int x,y,value;
}sz[1005];
bool check(int i,int j,int n,int m)
{
	if(0==i &&0==j)if(false==a[i+1][j] && false==a[i][j+1])return true;//左上角 
	if((n-1)==i &&0==j)if(false==a[i-1][j] && false==a[i][j+1])return true;//右上 
	if(0==i &&(m-1)==j)if(false==a[i][j-1] && false==a[i+1][j])return true;//左下 
	if((n-1)==i &&(m-1)==j)if(false==a[i-1][j] && false==a[i][j-1])return true;//右下 
	if(0==i)if(false==a[i-1][j] && false==a[i+1][j] && false==a[i][j+1])return true;//第一行 
	if(0==j)if(false==a[i+1][j] && false==a[i][j+1] && false==a[i][j-1])return true;//第一列 
	if(false==a[i-1][j] && false==a[i+1][j] && false==a[i][j+1] && false==a[i][j-1])return true;//normal 
	return false;
}
bool cmp2(name a,name b)
{
	return a.value>b.value;
}
int main()
{
	int n,m,sum=0;
	bool flag=false;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			char c;
			cin>>c;
			if('#'==c)a[i][j]=true;
			else if('.'==c)a[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
	{
		if(true==flag)break;
		for(int j=0;j<m;j++)
		{
			if(true==a[i][j])
			{
				if(true==check(i,j,n,m))
				{
					a[i][j]=false;
					flag=true;
					break;
				}
				else
				{
					int v=0;
					if(false==a[i-1][j])v++;
					if(false==a[i+1][j])v++;
					if(false==a[i][j+1])v++;
					if(false==a[i][j-1])v++;
					sz[i].x=i;
					sz[i].y=j;
					sz[i].value=v; 
				}
			}
		}
	}
	if(false==flag)
	{
		sort(sz,sz+(n-1)*(m-1),cmp2);
		a[sz[0].x][sz[0].y]=false;
	}
	if(flag==true)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(true==check(i,j,n,m) && false==a[i][j])sum++;
			}
		}
	}
	cout<<sum<<endl;
	//以下为测试代码
	/*for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(check(i,j,n,m))cout<<0<<" ";else cout<<1<<" ";
		}
		cout<<endl;
	} */
	return 0;
}

记录

2025/6/27 17:37
加载中...