10946 - You want what filled?

#include<bits/stdc++.h>

using namespace std;
char str[60][60],ch;
int counter,x,y;

struct data
{
    int letter;
    int koyta;
} arr[2500];

bool cmp(data lhs,data rhs)
{
    if(lhs.koyta==rhs.koyta)
    {
        return lhs.letter<rhs.letter;
    }
    return lhs.koyta>rhs.koyta;
}

void dfs(int i,int j)
{
    if(i<0||j<0||i>=x||j>=y)
    {
        return ;
    }
    if(str[i][j]!=ch)
    {
        return ;
    }
    if(str[i][j]==ch)
    {
        counter++;
    }
    str[i][j]='.';
    dfs(i,j+1);
    dfs(i,j-1);
    dfs(i+1,j);
    dfs(i-1,j);
}

int main()
{
    int i,j,k,test=0;
    while(scanf("%d%d",&x,&y)==2)
    {
        if(x==0 && y==0)
        {
            break;
        }
        getchar();

        for(i=0; i<x; i++)
        {
            for(j=0; j<y; j++)
            {
                scanf("%c",&str[i][j]);
            }

            getchar();
        }

        k=0;

        for(i=0; i<x; i++)
        {
            for(j=0; j<y; j++)
            {
                counter=0;
                if(str[i][j]!='.')
                {
                    ch = str[i][j];
                    dfs(i,j);
                    arr[k].letter = int(ch);
                    arr[k].koyta = counter;
                    k++;

                }
            }
        }

        sort(arr,arr+k,cmp);

        printf("Problem %d:\n",++test);

        for(i=0; i<k; i++)
        {
            printf("%c %d\n",arr[i].letter,arr[i].koyta);
        }
        memset(str,'\0',sizeof(str));
    }

    return 0;
}

0 comments: (+add yours?)