Lightoj 1012 Guilty Princ Solution


 

Solution in C++: 

///******Bismillahir-Rahmanir-Rahim******///

///AH Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    #include<bits/stdc++.h>
    using namespace std;
    #define mx 50
    char c[mx][mx];
    int  color[mx][mx];
    int x[4]={-1,0,0,1};
    int y[4]={0,-1,1,0};
    int cn=0,v,e;
    void dfs(int i,int j)
    {
        color[i][j]=1;
        int k,vx,vy;
        for(k=0; k<4; k++)
        {
            vx=x[k]+i;
            vy=y[k]+j;
            if(vx<v&&vx>=0&&vy<e&&vy>=0)
            {
                if((color[vx][vy]==0)&&(c[vx][vy]=='.'))
                {
                    cn++;
                    dfs(vx,vy);
                }
            }
        }
    }
    int main()
    {
        int t,ct=0,i,j;
        cin>>t;
        while(t--)
        {
            cin>>e>>v;
            ct++;
            for(i=0; i<v; i++)
            {
                for(j=0; j<e; j++)
                {
                    cin>>c[i][j];
                }
            }
            memset(color,0,sizeof(color));
            for(i=0; i<v; i++)
            {
                for(j=0; j<e; j++)
                {
                    if(c[i][j]=='@')
                        dfs(i,j);
                }
            }
            printf("Case %d: %d\n",ct,cn+1);
            cn=0;
        }
    }

    No comments

    Most View Post

    Recent post

    RESTful APIs with CRUD Operations in Laravel 12| (2025)

      RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

    Powered by Blogger.