Find Your Solution Here

UVA

UVA 469 – Wetlands of Florida Solutions

Problem Link–https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=410

Solution:

#include <stdio.h>
#include <string.h>

char a[105][105];
bool visited[110][110]= {false};
int ans=0;
void dfs(int i,int j)
{
    if(visited[i][j]) return;
    if(i<0 or j<0 or a[i][j] == 0 ) return;
    if(a[i][j]=='L') return;
    if(a[i][j]=='W') ans++,visited[i][j]=true;

    dfs(i+1,j);
    dfs(i-1,j);
    dfs(i,j+1);
    dfs(i,j-1);
    dfs(i+1,j+1);
    dfs(i+1,j-1);
    dfs(i-1,j-1);
    dfs(i-1,j+1);
}
int main() {
    int t, i, j;
    char str[105];
    scanf("%d ", &t);
    while(t--) {
        memset(a, 0, sizeof(a));
        int n = 0,m;
        while(gets(str)) {
            if(str[0] == '\0')
                break;
            if(str[0] != 'W' && str[0] != 'L') {
                sscanf(str, "%d %d", &i, &j);
                memset(visited, false, sizeof(visited));
                ans = 0;
                dfs(i-1, j-1);
                printf("%d\n", ans);
            } else {
                sscanf(str, "%s", a[n]);
                n++;
            }
        }
        if(t)
            puts("");
    }
    return 0;
}

Leave a Reply