【DFS】 [NOI Online #3 入门组]观星(luogu)

题目:

【DFS】 [NOI Online #3 入门组]观星(luogu)_第1张图片


思路:

在这里插入图片描述在这里插入图片描述

emmm,DFS造他就完事


C o d e Code Code:

#include 
#include 
using namespace std;
char ch;
int n,m,star,ans;
int number[1501*1501],map[1501][1501];//number是星系数量,map是整张图
int d[8][2] = {
     {
     0,1} ,{
     0,-1} ,{
     1, 0}, {
     -1, 0}, {
     1, 1}, {
     1, -1}, {
     -1, 1}, {
     -1, -1}};//八个方向
void dfs(int x,int y)//dfs
{
     
	map[x][y] = 0;
	star++;//星星数++
	for (int i = 0; i < 8; ++i)
	{
     
		int x2 = x + d[i][0], y2 = y + d[i][1];
		if(x2 > 0 && x2 <= n && y2 > 0 && y2 <= m && map[x2][y2])//边界 
		dfs(x2,y2);//搜
	}
}
int main()
{
     
  	scanf ("%d%d", &n, &m);
  	for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
	  {
     
	     cin>>ch  ;
		 if(ch == '.') map[i][j] = 0;
		 if(ch == '*') map[i][j] = 1;//看是不是星星	
	  } 
	  for (int i = 1; i <= n; ++i)
	    for (int j = 1; j <= m; ++j)//遍历整张图
		{
     
			if(map[i][j] == 1)
			{
     
			  dfs(i,j);//搜
			  number[star]++;
			  if(star * number[star] > ans)  ans = star * number[star];//更新当前星系
			  star = 0;//
			}
		}    
		int maxx = 0;
		for (int i = 0; i <= m * n; ++i)
		if(number[i]) maxx++;
		printf("%d %d",maxx,ans);
	  
}

你可能感兴趣的:(深搜)