Given a character array of N rows and M columns consisting of * and # signs, please help me to count how many different crosses composed of "signs are contained in it. A cross is composed of at least 5 # signs, as follows The picture shows:

Problem Description

Insert picture description here
Insert picture description here

Code

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    
    


   int n,m,ans=0;
   cin >> n >> m;
   char **list = new char*[n];
   for(int i=0;i<n;i++)
   {
    
    
   	list[i] = new char[m];
   	for(int j=0;j<m;j++)
   	{
    
    
   		cin>>list[i][j];
	   }
   }
   
   for(int i=1;i<n-1;i++)
   for(int j=1;j<m-1;j++)
   {
    
    
   	    if(list[i][j]== '#')
   	    {
    
    

		  int t[4]={
    
    0,0,0,0};
		  int a=i,b=j;
		  while(a>0 && b>0 && a<n && b<m && list[a-1][b] == '#')
		  {
    
    
		  	t[0]++;
		  	a--;
		   } 
		  
		  a=i,b=j;
		  while(a>0 && b>0 && a<n-1 && b<m && list[a+1][b] == '#' )
		  {
    
    
		  	t[1]++;
		  	a++;
		   } 
		  a=i,b=j;
		  while(a>0 && b>0 && a<n && b<m && list[a][b-1] == '#')
		  {
    
    
		  	t[2]++;
		  	b--;
		   } 
		  
		   a=i,b=j;
		  while(a>0 && b>0 && a<n && b<m-1 &&list[a][b+1] == '#' )
		  {
    
    
		  	t[3]++;
		    b++;
		   } 
		   ans += t[0]*t[1]*t[2]*t[3];
		}
		
   }
   cout << ans;


}

Guess you like

Origin blog.csdn.net/qq_45768060/article/details/108257699