Saturday, July 7, 2018

UVA 469-Wetlands of Florida

A construction company owns a large piece of real estate within the state of Florida. Recently, the company decided to develop this property. Upon inspection of the property, however, it was revealed that the land, at various locations, contained bodies of water. This came as a shock to the owners of the company, for they were from out of state and not familiar with wetlands of Florida. The situation was very grave and the owners not knowing that such bodies of water can be converted to beautiful lakes that will increase the value of the land around them, were about to abandon the construction project. Fortunately, this fact was brought to the owners' attention by a smart FIU graduate who worked for the company and consequently the construction project started.

The engineers divided the construction site by a grid into uniform square cells such that each square cell entirely contained either water or land. (How they did it, of course, is anybody's guess.) Now, the question that the engineers are to answer is the following: ``Given the row and column number of a grid cell that contains water, what is the area of the lake containing that cell.'' (an area is measured by number of grid cells it contains. Diagonal cells are considered to be adjacent.)
You are to write a program to answer this question!

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each input set consists of 0<n<=99 lines each containing 0<m<=99 character long sequence of ``L''s and ``W''s followed by k > 0 lines each containing a pair of integers i and j. The first n lines will represent the n X m grid covering the land where a ``W''/``L'' at the c th character of the r th line indicates water/land within the cell at row r and column c of the grid. The pairs of integers on the last k lines, each represent the row and column numbers of some grid cell that contains water.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
The output for each pair of integers, i and j, on the last k lines of input, consists of an integer, on a separate line, indicating the area of the lake containing the grid cell, at row i and column j of the grid.

 

Sample Input

1
LLLLLLLLL
LLWWLLWLL
LWWLLLLLL
LWWWLWWLL
LLLWWWLLL
LLLLLLLLL
LLLWWLLWL
LLWLWLLLL
LLLLLLLLL
3 2
7 5

Sample Output

12
4


Analysis  

As we have to find out number of wet land from certain position, we can apply DFS algorithm here. According to DFS algorithm we can traverse all the reachable nodes from a given node.  

Solution

#include <bits/stdc++.h>
using namespace std;
int xx[]= {0,0,1,1,-1,-1,1,-1}; //For 8 direction
int yy[]= {1,-1,1,-1,1,-1,0,0}; // For 8 direction

char s[105][105];
bool visit[105][105];
int ans;

int sx,sy,r,c;

void dfs(int ux,int uy)
{
    ans++;
    for(int i=0; i<8; i++)
    {
        int vx=ux+xx[i];
        int vy=uy+yy[i];

        if(vx<0 || vy<0 || vx>98 || vy>98) continue;

        if(visit[vx][vy]==1 || s[vx][vy]=='L') continue;

        visit[vx][vy]=1;

        dfs(vx,vy);
    }
}


int main()
{
    int tc, len, ro, co;
    scanf("%d", &tc);
    while(tc--)
    {
        scanf("%s", s[0]);
        len=strlen(s[0]);
        for(int i=1; i<len; i++)
        {
            scanf("%s", s[i]);
        }
        while(scanf("%d%d", &ro, &co)==2)
        {
            memset(visit,0,sizeof(visit));
            ans=0;
            if(s[ro-1][co-1]=='W')
            {
                visit[ro-1][co-1]=1;
                dfs(ro-1, co-1);
            }
            printf("%d\n", ans);
        }
        if(tc)
            printf("\n");
    }
    return 0;
}

No comments:

Post a Comment