Page 5 of 5

469 - Wetlands of Florida

Posted: Fri May 22, 2015 12:46 pm
by mosharrafm
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;
}

Re: 469 - Wetlands of Florida

Posted: Mon Oct 26, 2015 8:13 pm
by codegagu
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! :)

Re: 469 - Wetlands of Florida

Posted: Tue Oct 27, 2015 3:11 am
by brianfry713
That link isn't working for me. Post your code here.

Re: 469 - Wetlands of Florida

Posted: Tue Oct 27, 2015 12:29 pm
by codegagu
@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;
    }