UVa 10946 - You want what filled?

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

// Accepted

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 60;

struct my
{
    char mychar;
    int frq;
}arr[3010];

int R , C , cnt=0;
char adj[high][high];
int visited[high][high];

void DFS(int i , int  j, char ch)
{
    if(i<0 || i>=R || j<0 || j>=C) return;

    if(adj[i][j] != ch) return;

    if(adj[i][j] == ch) adj[i][j] = '.';

    if(!visited[i][j])
    {
        visited[i][j] = 1;
        cnt+=1;
    }

    DFS(i+1,j,ch);
    DFS(i-1,j,ch);
    DFS(i,j+1,ch);
    DFS(i,j-1,ch);
}

bool cmp(my a , my b)
{
    if(a.frq > b.frq) return true;

    else if(a.frq == b.frq)
    {
        return a.mychar < b.mychar;
    }

    return false;
}

int main()
{
    fast;
    int i , j , test=0;
    while(cin >> R >> C && R && C)
    {
        mset(visited , 0);

        for(i=0; i<R; i++)
        {
            for(j=0; j<C; j++)
            {
                cin >> adj[i][j];
            }
        }

        int len = 0;

        for(i=0; i<R; i++)
        {
            for(j=0; j<C; j++)
            {
                cnt = 0;

                if((adj[i][j]>='A' && adj[i][j]<='Z') && !visited[i][j])
                {
                    char ch = adj[i][j];

                    //cnt+=1;

                    DFS(i , j , ch);
                    //cout << ch << "-" << cnt << "; ";

                    arr[len].mychar = ch;
                    arr[len].frq = cnt;
                    len++;
                }
            }
        }

        sort(arr, arr+len, cmp);

        cout << "Problem " << ++test << ":\n";

        for(i=0; i<len; i++)
        {
            cout << arr[i].mychar << " "  << arr[i].frq << "\n";
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number