1 solutions

  • 0
    @ 2025-3-3 16:24:11

    C :

    #include <stdio.h>
    #include <string.h>
    char a[1005][85];
    int w, h;
    int dfs(int i, int j)
    {
    	if (i < 1 || j < 1 || i > h || j > w) return 0;
    	int cnt = 0;
    	if (a[i][j] == '*')
    	{
    		cnt = 1;
    		a[i][j] = '.';
    		cnt += dfs(i, j + 1);
    		cnt += dfs(i, j - 1);
    		cnt += dfs(i + 1, j);
    		cnt += dfs(i - 1, j);
    	}
    	return cnt;
    }
    int main(int argc, char *argv[])
    {
    	memset(a, '\0', sizeof(a));
    	scanf("%d%d", &w, &h);
    	getchar();
    	int i, j;
    	for (i = 1; i <= h; i++)
    	{
    		for (j = 1; j <= w; j++)
    			scanf("%c", &a[i][j]);
    		getchar();
    	}
    	int t = 0, s = 0;
    	for (i = 1; i <= h; i++)
    	{
    		for (j = 1; j <= w; j++)
    		{
    			t = dfs(i, j);
    			s = s > t ? s : t;
    		}
    	}
    	printf("%d\n", s);
    	return 0;
    }
    

    C++ :

    #include <iostream> 
    #include <iomanip> 
    #include <cmath> 
    #include <string> 
    #include <cstring> 
    using namespace std; 
    int w,h,ph[1002][82],q1[80004],q2[80004],f,r,s,maxs=0; 
    char ch; 
    int main() 
    { 
        cin>>w>>h; 
        for (int i=1;i<=h;i++)  
            for (int j=1;j<=w;j++) {cin>>ch; if (ch=='*'){ph[i][j]=1;}}  
              
         for (int i=1;i<=h;i++)  
            for (int j=1;j<=w;j++) 
            { 
             if (ph[i][j]==1) 
             { 
                r=1; q1[r]=i; q2[r]=j; f=0; s=0; 
                while (f<=r) 
                { 
                  f++; s++; 
                  ph[q1[f]][q2[f]]=0; 
                  if (ph[q1[f]-1][q2[f]]==1) 
                   {r++; q1[r]=q1[f]-1; q2[r]=q2[f]; ph[q1[f]-1][q2[f]]=0;} 
                  if (ph[q1[f]][q2[f]-1]==1)  
                   {r++; q1[r]=q1[f]; q2[r]=q2[f]-1; ph[q1[f]][q2[f]-1]=0;} 
                  if (ph[q1[f]][q2[f]+1]==1)  
                   {r++; q1[r]=q1[f]; q2[r]=q2[f]+1; ph[q1[f]][q2[f]+1]=0;} 
                  if (ph[q1[f]+1][q2[f]]==1) 
                   {r++; q1[r]=q1[f]+1; q2[r]=q2[f]; ph[q1[f]+1][q2[f]]=0;} 
                } 
    //            for (int i=1;i<=20;i++)  cout<<setw(3)<<q1[i];cout<<endl; 
    //            for (int i=1;i<=20;i++)  cout<<setw(3)<<q2[i];cout<<endl; 
                if (s>maxs) maxs=s; 
             }    
            } 
            if (maxs>0) maxs--; 
        cout<<maxs<<endl; 
    //    system ("pause"); 
        return 0; 
    } 
    
    
    • 1

    Information

    ID
    10055
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By