AcWing 5015. 寻宝图
原题链接
中等
作者:
算式鱼
,
2024-03-14 21:24:58
,
所有人可见
,
阅读 32
#include<bits/stdc++.h>
using namespace std;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int n,m,res = 0,ans = 0,flag;
typedef pair<int,int> PII;
queue<PII> q;
int main()
{
scanf("%d %d",&n,&m);
char a[n][m];
int b[n][m]={0};
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin >> a[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j] - '0' >= 1 && b[i][j] == 0){
flag = 1;
res ++;
q.push({i,j});
b[i][j] = 1;
while(!q.empty())
{
auto t = q.front();
q.pop();
if(a[t.first][t.second] - '0' >= 2 && flag == 1){
ans ++;
flag = 0;
}
// printf("%d %d\n",t.first,t.second);
for(int k=0;k<4;k++)
{
int l = t.first + dx[k] ,r = t.second + dy[k];
if(l >= 0 && l < n && r >= 0 && r < m && b[l][r] == 0 && a[l][r] - '0' >= 1)
{
q.push({l,r});
b[l][r] = 1;
}
}
}
}
}
}
printf("%d %d\n",res,ans);
return 0;
}