活动 - AcWing (1097.池塘计数)
题意:找到以W点为起始的连通块
思路:多方向dfs,更新走过的点,统计联通块的数量
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>
const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;
int n,m;
int cnt;
char mp[N][N];
//int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
void dfs(int x,int y)
{
mp[x][y] = '.';
for(int i = -1; i <= 1; i ++)
for(int j = -1; j <= 1; j ++)
{
int a = x + i;
int b = y + j;
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(mp[a][b] == '.') continue;
dfs(a,b);
}
}
void solve()
{
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(mp[i][j] == 'W')
{
dfs(i,j);
cnt ++;
}
}
}
printf("%d\n",cnt);
}
int main()
{
solve();
system("pause");
return 0;
}
活动 - AcWing(1098.城堡问题)
题意:找到连通块最大值和个数
思路:多方向bfs,更新走过的点,统计联通块的数量,并且更新最大值
注意 8421 在二进制更新条件下的方向数组要与对应方向一致的问题
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>
const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;
int n,m;
int cnt;
char mp[N][N];
//int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
void dfs(int x,int y)
{
mp[x][y] = '.';
for(int i = -1; i <= 1; i ++)
for(int j = -1; j <= 1; j ++)
{
int a = x + i;
int b = y + j;
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(mp[a][b] == '.') continue;
dfs(a,b);
}
}
void solve()
{
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(mp[i][j] == 'W')
{
dfs(i,j);
cnt ++;
}
}
}
printf("%d\n",cnt);
}
int main()
{
solve();
system("pause");
return 0;
}
活动 - AcWing(1106. 山峰和山谷)
题意:找到八连通的最高的区块山和最矮的区块谷
思路:bfs爆搜即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>
const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;
int n;
int hr[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int g[N][N],st[N][N];
void bfs(int x,int y,bool &high, bool &low)
{
int stand = g[x][y];
st[x][y] = 1;
queue<PII>q;
q.push({x,y});
while(q.size())
{
auto t = q.front();
q.pop();
for(int i = 0; i < 8; i ++)
{
int a = t.first + hr[i][0];
int b = t.second + hr[i][1];
if(a < 0 || a >= n || b < 0 || b >= n) continue;
if(g[a][b] > stand) low = 1;
if(g[a][b] < stand) high = 1;
if(st[a][b]) continue;
if(g[a][b] == stand)
{
q.push({a,b});
st[a][b] = 1;
}
}
}
}
void solve()
{
scanf("%d",&n);
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
scanf("%d",&g[i][j]);
int peak = 0,valley = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
if(!st[i][j])
{
bool high = 0,low = 0;
bfs(i,j,high,low);
//cout << i << " " << j << " " << high << " " << low << endl;
if(!high) valley ++;
if(!low) peak ++;
}
printf("%d %d\n",peak,valley);
}
int main()
{
solve();
system("pause");
return 0;
}