Question

In: Computer Science

Any java solution for this question ? A state is divided into R*C cities.The government has...

Any java solution for this question ?

A state is divided into R*C cities.The government has launched an initiative to find the cities which are dominated by coders. Each city may or may not have coders residing in it. If the city is dominated by coders, it is marked with 1 else it is marked with 0. Two cities are termed as connected cities if they both are dominated by coders and can be reached by moving vertically, horizontally, or diagonally. Example: The given is the state with 3*3 cities and their dominance representation. City[1, 1] is directly connected to City[1, 2]. City[1, 2] is directly connected to City[1, 1], City[1, 3] and City[2, 3]. City[1, 3] is directly connected to City[1, 2] and City[2, 3]. City[2, 3] is directly connected to City[1, 2] and City[1, 3]. City[3, 1] is not connected to any of the other coder dominant cities. One or more coder dominant connected cities form the Coding belt. In a belt, there may be coder dominant cities which are not directly connected but can be reached by moving through other dominant cities. It is possible that there are multiple coding belts in the state.

Example: The given is the state with 3*3 cities and their dominance representation. For the given example, there are two coding belts. C1 represents Coding Belt 1 and C2 represents Coding Belt 2. The government wants to find the number of coder dominant cities in the largest coding belt. The government will give you the credits in the initiative. Can you help the government?

Note: For the given example, there are 4 coder dominant cities in the largest coding belt.

Input Format The first line of input consists of two space-separated integers, number of rows, R, and number of columns, C. Next R lines each consist of C space-separated integers.

Constraints 1<= R, C <=10

Output Format Print the number of coder dominant cities in the largest Coding belt.

Sample TestCase 1 Input 5 51 1 1 0 00 1 1 0 00 0 0 0 11 0 0 1 11 1 0 0 1

Output 5 Explanation There are three belts in the given 5*5 cities. Coding Belt 1 (C1): Number of Coder Dominant Cities = 5 Coding Belt 2 (C2): Number of Coder Dominant Cities = 4 Coding Belt 3 (C3): Number of Coder Dominant Cities = 3

Solutions

Expert Solution

ANSWER :

This question is similar to the problem of finding the number of lands using the dfs approach. Here we just need to find the maximum size of the land. that's why we are using a global variable mx which will be our result

#include<bits/stdc++.h>

using namespace std;

int mx=0;

int r,c;

pair<int,int>moved[]={ {-1,-1}, {-1,0} , {-1,1} , {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1} };

bool issafe(int x,int y)

{

    if(x>=0&&x<r &&y>=0 &&y<c)

    {

        return true;

    }

    return false;

}

void dfs(int i,int j,int &cnt,vector<vector<int>>&adj,vector<vector<bool>>&vis)

{

    vis[i][j]=true;

    for(int k=0;k<8;k++)

    {

        int n_i=i+moved[k].first;

        int n_j=j+moved[k].second;

        if(issafe(n_i,n_j) && adj[n_i][n_j]==1 && vis[n_i][n_j]==false)

        {

            cnt++;

            dfs(n_i,n_j,cnt,adj,vis);

        }

    }

}

int main()

{

    cin>>r>>c;

    vector<vector<int>>adj(r,vector<int>(c));

    for(int i=0;i<r;i++)

    {

        for(int j=0;j<c;j++)

        {

            cin>>adj[i][j];

        }

    }

    vector<vector<bool>>vis(r,vector<bool>(c,false));

    for(int i=0;i<r;i++)

    {

        for(int j=0;j<c;j++)

        {

            if(vis[i][j]==false && adj[i][j]==1)

            {

                int cnt=1;

                dfs(i,j,cnt,adj,vis);

                mx=max(mx,cnt);

            }

        }

    }

    cout<<mx<<"\n";

}

Sample output

( PLEASE VOTE FOR THIS ANSWER )

I THINK IT WILL BE USEFULL TO YOU .......

PLZZZZZ COMMENT IF YOU HAVE ANY PROBLEM I WILL TRY TO SOLVE IT ..........,,

THANK YOU .......


Related Solutions

A state is divided into R*C cities.The government has launched an initiative to find the cities...
A state is divided into R*C cities.The government has launched an initiative to find the cities which are dominated by coders. Each city may or may not have coders residing in it. If the city is dominated by coders, it is marked with 1 else it is marked with 0. Two cities are termed as connected cities if they both are dominated by coders and can be reached by moving vertically, horizontally, or diagonally. Example: The given is the state...
Please Provide the solution in java, already have a question which is answer in C++. Language:...
Please Provide the solution in java, already have a question which is answer in C++. Language: java. Please don't provide your email for private answer. Q1. Implement a program which allows the user to find the shortest path between two nodes in a graph possibly passing through a third node. I.e. the user should be able to ask questions like: Which is the shortest path from A to B passing through C? The program should output an ordered list of...
Using Java The C expression m % n yields the remainders of m divided by n....
Using Java The C expression m % n yields the remainders of m divided by n. Define the greatest common divisor (GCD) of two integers x and y by: gcd(x, y) = y if (y <= x && x % y == 0) gcd(x, y) = gcd(y, x) if (x < y) gcd(x, y) = gcd(y, x % y) otherwise You will submit a program listing (properly commented) and the output using the following data sets: x y 32 8...
THIS QUESTION REQUIRES THE USE OF R STUDIO. ANY ANSWERS GIVEN THAT ARE NOT IN R...
THIS QUESTION REQUIRES THE USE OF R STUDIO. ANY ANSWERS GIVEN THAT ARE NOT IN R STUDIO CODE WILL NOT SUFFICE. SOLVING WITHOUT THE USE OF R STUDIO IS NOT ACCEPTABLE. The previous question was: Annual salaries for a large company are approximately normally distributed with a mean of 49000 dollars and a standard deviation of 2000 dollars. One manager claims that all of his direct reports are paid "above the 75th percentile" for the company. What is the minimum...
Obtain the general solution of the following equations: r Utt − c^2 r Urr − 2...
Obtain the general solution of the following equations: r Utt − c^2 r Urr − 2 c^2 Ur = 0, c = constant,
Robinson has a utility of u(c, r) = cr where c = coconuts and r =...
Robinson has a utility of u(c, r) = cr where c = coconuts and r = leisure. In order to produce coconuts, technology dictates the production, given by c = a(L^1/2) where a = 8, and L = labour. The time constraint is r+L = 12. How many units of labour will be used?
c) Let R be any ring and let ??(?) be the set of all n by...
c) Let R be any ring and let ??(?) be the set of all n by n matrices. Show that ??(?) is a ring with identity under standard rules for adding and multiplying matrices. Under what conditions is ??(?) commutative?
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required -...
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for...
Government, including federal, state and local spent over $6.5 trillion in 2015. Choose any government program...
Government, including federal, state and local spent over $6.5 trillion in 2015. Choose any government program and amount spent; and state your opinion on the value of the program (dollars spent). You can choose any one from large (Medicare) to small (National Endowment for the Arts).
Dudley has a utility function U(C, R) = CR, where R is leisure and C is...
Dudley has a utility function U(C, R) = CR, where R is leisure and C is consumption per day. He has 16 hours per day to divide between work and leisure. Dudley has a non-labor income of $48 per day. (a) If Dudley is paid a wage of $6 per hour, how many hours of leisure will he choose per day? (b) As a result of a promotion, Dudley is now paid $ 8 per hour. How will his leisure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT