社区讨论

help me !!!

P1451求细胞数量参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo7ij12h
此快照首次捕获于
2023/10/27 02:23
2 年前
此快照最后确认于
2023/10/27 02:23
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y;
};
char maps[101][101];
int vis[101][101];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
queue<node> q;
int main()
{
	int n,m;
	int tot=0;
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<m;j++)
		{
			cin>>maps[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(maps[i][j]=='0')
			{
				continue;
			}
			node t=node{i,j};
			//666
			q.push(t);
			while(!q.empty())
			{
				node c=q.front();
				q.pop();
				for(int k=0;k<4;k++)
				{
					int nx=c.x+dx[k];
					int ny=c.y+dy[k];
					if(nx<1||nx>n||ny<1||ny>m||maps[i][j]=='0'||vis[nx][ny]==1)
					{
						continue;
					}
					tot++;
					vis[i][j]=1;
					if(vis[nx][ny]==0)
					{
						vis[nx][ny]=1;
						node y=node{nx,ny};
						q.push(y);
					}	}
			}
		}
	}
	cout<<tot<<endl;
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...