大佬救命
查看原帖
大佬救命
213465
zhurunlin68楼主2023/1/31 21:55

只对了一个点,奇怪的是我下载了第一个数据,就是样例,但样例我是过了的,不知为何交上去就没过(用了很多注释,抱歉)

#include<bits/stdc++.h>
//#include<windows.h>
using namespace std;
int n,m,ans,xx=1;
char a[1005][1005];
bool b[1005][1005],bl;
struct st{
	int x;
	int y;
};
queue<st>q;
const int fx[4]={1,0,-1,0};
const int fy[4]={0,1,0,-1};
st s;
void bfs(int x,int y)
{
	s.x=x;
	s.y=y;
	q.push(s);
	while(!q.empty())
	{
		int bb=0;
		s.x=q.front().x;
		s.y=q.front().y;
		//cout<<s.x<<" "<<s.y<<" "<<xx<<endl;
		for(int i=0;i<4;i++)
		{
			s.x+=fx[i];
			s.y+=fy[i];
			if(s.x>n||s.y>m||s.x<=0||s.y<=0||a[s.x][s.y]-'0'==0||b[s.x][s.y]==1) 
			{
				s.x-=fx[i];
		    	s.y-=fy[i];
				continue;
			}
			else
			{
				q.push(s);
				b[s.x][s.y]=1;
				bb++;
				//cout<<s.x<<" "<<s.y<<" "<<a[s.x][s.y]<<endl;
			}
			s.x-=fx[i];
			s.y-=fy[i];
		}
		/*for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
		        cout<<b[i][j];
		    cout<<endl;
		} */
		//Sleep(1000);
		//cout<<q.empty()<<endl; 
		//cout<<q.front().x<<" "<<q.front().y<<endl;
		q.pop();
    }
} 
int main()
{
	//freopen("cell.in","r",stdin);
	//freopen("cell.out","w",stdout);
	cin>>n>>m;
	bool bi=0;
	for(int i=1;i<=n;i++)
	    for(int j=1;j<=m+1;j++)
	    {
	    	scanf("%c",&a[i][j-1]);
		} 
	for(int i=1;i<=n;i++)
	    for(int j=1;j<=m;j++)
	    {
	    	if(b[i][j]==0&&a[i][j]!='0')
	    	{
            	bfs(i,j);
            	ans++;
		 	}
		} 
	cout<<ans;
	return 0;
}
2023/1/31 21:55
加载中...