## 469 - Wetlands of Florida

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

mosharrafm
New poster
Posts: 2
Joined: Fri May 22, 2015 12:23 pm

### 469 - Wetlands of Florida

Getting Time Limit Please Help

Code: Select all

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

char str[120];
char A[120][120], B[120][120];
int N, count, M;
int rx[]={-1, -1, -1, 0, 0, 1, 1, 1};
int ry[]={-1, 0, 1, -1, 1, -1, 0, 1};

void traverse(int x, int y)
{
int dx, dy, i;
count++;
B[x][y]=1;

for( i = 0; i < 8; i++) {
dx = x + rx[i];
dy = y + ry[i];
if(dx>=0 && dx<N && dy>=0 && dy<M && A[dx][dy]=='W' && B[dx][dy]==0)

traverse(dx, dy);
}

}

void readCase()
{
int i = 0, flag = 0, j, k, p, l;
//memset(B, 0, sizeof(B));
memset(A, 0, sizeof(A));

while(1) {
gets(str);
l = strlen(str);
if(!l) {
break;
}
if(str[0]=='W' || str[0] == 'L')
{
strcpy(A[i], str);
//strcpy(B[i], A[i]);
M = l;
}
else {
sscanf(str, "%d %d", &j, &k);
if(flag==0) {
N= i;
flag = 1;
}

count=0;
if((j-1)>= 0 && (k-1) >=0 && A[j-1][k-1] == 'W') {
memset(B, 0, sizeof(B));
traverse(j-1,k-1);
}
printf("%d\n", count);

}

i++;
}

}

int main()
{
int T, Case;
scanf("%d", &T);
getchar();
getchar();
for(Case = 1; Case <= T; Case++) {
readCase();
}
return 0;
}``````

codegagu
New poster
Posts: 2
Joined: Sun Dec 28, 2014 6:07 pm

### Re: 469 - Wetlands of Florida

Hi I'm constantly getting Runtime error in this problem. I'm absolutely clueless about what is causing it. Please help me.
Here is my code.

http://ideone.com/r208lB

Thanks!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 469 - Wetlands of Florida

That link isn't working for me. Post your code here.
Check input and AC output for thousands of problems on uDebug!

codegagu
New poster
Posts: 2
Joined: Sun Dec 28, 2014 6:07 pm

### Re: 469 - Wetlands of Florida

@brianfry713

Sorry for the inconvenience, here is my code.

Code: Select all

``````

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define pip pair<int,pii>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007

typedef long long ll;
int dx[]={1,-1,0,0,-1,1,-1,1};
int dy[]={0,0,1,-1,-1,1,1,-1};

vector<string> grid;
vector<pii> points;
vector<pii> visited;
int area=0;

bool isValid(pii point)
{
int x=point.ff; int y=point.ss;
if(x<0 || x>=grid.size() || y<0 || y>=grid[0].size())
return false;
return true;
}

void dfs(pii start)
{
if(find(visited.begin(),visited.end(),start)!=visited.end())
return;

visited.pb(start);
area++;

int x=start.ff;
int y=start.ss;

for(int k=0;k<8;k++)
{
int nx=x+dx[k];
int ny=y+dy[k];
pii next=make_pair<int,int>(nx,ny);

if(find(visited.begin(),visited.end(),next)==visited.end() && grid[nx][ny]=='W' && isValid(next))
{
dfs(next);
}
}

}

int main()
{
int T;
scanf("%d",&T);
getchar();
getchar();

for(int t=0;t<T;t++)
{
char inp[100];
while(gets(inp) && strlen(inp)>0)
{
if(inp[0]=='L' || inp[0]=='W')
{
string p(inp);
grid.pb(p);
}
else
{
int r,c;
sscanf(inp,"%d %d",&r,&c);
r--;c--;
dfs(make_pair<int,int>(r,c));
cout<<area<<endl;
area=0;

}
visited.clear();
}

cout<<endl;

grid.clear();
points.clear();
}

return 0;
}

``````