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.
The input 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 rst n lines will represent the n×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

题意:
给出一个矩阵和某些点坐标,输出与所给的点处相连续的W区域的面积(八个方向)

注意:
输入格式,用sscanf读取字符串中的整数


#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char map[100][100];
int marked[100][100];
int l,j,sum;
void dfs(int x,int y)
{
    //int nx,ny;
    if(x<0||y<0||x>j||y>l)
        return;
    if(marked[x][y]==1||map[x][y]!='W')
        return;
    sum++;
    marked[x][y]=1;
    for(int i=-1;i<=1;i++)//循环8个方向
    {
        for(int k=-1;k<=1;k++)
        {
                dfs(x+i,y+k);

        }
    }
}
int main()
{
    int N,n,r=0,c=0;
    scanf("%d",&N);
    getchar();
    char str[105];
    for(n=0;n<=N;n++)
    {
        j=0;
        memset(map,'\0',sizeof(map));
        l=0;
        while(gets(str))//读入字符串
        {
            if(str[0]=='\0')//若为空行,则推出
                break;
            if(str[0]!='W'&&str[0]!='L')//若为数字
            {
                sscanf(str, "%d %d", &r, &c);//用sscanf读取整数
                sum=0;
                memset(marked,0,sizeof(marked));
                dfs(r-1,c-1);
                printf("%d\n",sum);
            }
            if(str[0]=='W'||str[0]=='L')
            {
                sscanf(str,"%s",map[j++]);//按行读入map中
                l=strlen(str);
            }
        }
            if(n>0&&n!=N&&N>1)
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值