Monday, August 18, 2014

UVa 469 - Wetlands of Florida

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<cctype>
#include<cstdlib>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int test,no=1;
    scanf("%d\n\n",&test);
    char a[1000];
    while(test--)
    {
        if(no>1)printf("\n");
        char name[200][1000];
        int n=1;
        while(gets(a))
        {
            if(a[0]=='L'||a[0]=='W')
            {
                strcpy(name[n++],a);
            }
            else break;
        }
        char *p;
        p=strtok(a," \n");
        int row=atoi(p);
        p=strtok(NULL," \n");
        int col=atoi(p);
        int number[n][strlen(name[1])+1];
            for(int i=1;i<n;i++)
            {
                for(int j=0;j<strlen(name[1])+1;j++)
                {
                    if(name[i][j]=='L')number[i][j+1]=0;
                    else number[i][j+1]=1;
                }
            }
            queue<int>r,c;
            number[row][col]=0;
            r.push(row);
            c.push(col);
            row=n-1;
            col=strlen(name[1]);
            int count=0;
            while(!r.empty())
            {
                int x=r.front(),y=c.front();
                //printf("%d %d\n",x,y);
                r.pop();c.pop();
                count++;
                if(x-1>0&&number[x-1][y]==1)
                        {
                            r.push(x-1);c.push(y);number[x-1][y]=0;
                        }
                        if(y-1>0&&number[x][y-1]==1)
                        {
                            r.push(x);c.push(y-1);number[x][y-1]=0;
                        }
                        if(x+1<=row&&number[x+1][y]==1)
                        {
                            r.push(x+1);c.push(y);number[x+1][y]=0;
                        }
                        if(y+1<=col&&number[x][y+1]==1)
                        {
                            r.push(x);c.push(y+1);number[x][y+1]=0;
                        }
                        if(x-1>0&&y-1>0&&number[x-1][y-1]==1)
                        {
                            r.push(x-1);c.push(y-1);number[x-1][y-1]=0;
                        }
                        if(x-1>0&&y+1<=col&&number[x-1][y+1]==1)
                        {
                            r.push(x-1);c.push(y+1);number[x-1][y+1]=0;
                        }
                        if(x+1<=row&&y-1>0&&number[x+1][y-1]==1)
                        {
                            r.push(x+1);c.push(y-1);number[x+1][y-1]=0;
                        }
                        if(x+1<=row&&y+1<=col&&number[x+1][y+1]==1)
                        {
                            r.push(x+1);c.push(y+1);number[x+1][y+1]=0;
                        }
            }
            printf("%d\n",count);
        while(gets(a)!=NULL&&strlen(a)!=0)
        {
            if(strlen(a)==0)break;
            p=strtok(a," \n");
            row=atoi(p);
            p=strtok(NULL," \n");
            col=atoi(p);
            int number[n][strlen(name[1])+1];
            for(int i=1;i<n;i++)
            {
                for(int j=0;j<strlen(name[1])+1;j++)
                {
                    if(name[i][j]=='L')number[i][j+1]=0;
                    else number[i][j+1]=1;
                }
            }
            queue<int>r,c;
            number[row][col]=0;
            r.push(row);
            c.push(col);
            row=n-1;
            col=strlen(name[1]);
            int count=0;
            while(!r.empty())
            {
                int x=r.front(),y=c.front();
                //printf("%d %d\n",x,y);
                r.pop();c.pop();
                count++;
                if(x-1>0&&number[x-1][y]==1)
                        {
                            r.push(x-1);c.push(y);number[x-1][y]=0;
                        }
                        if(y-1>0&&number[x][y-1]==1)
                        {
                            r.push(x);c.push(y-1);number[x][y-1]=0;
                        }
                        if(x+1<=row&&number[x+1][y]==1)
                        {
                            r.push(x+1);c.push(y);number[x+1][y]=0;
                        }
                        if(y+1<=col&&number[x][y+1]==1)
                        {
                            r.push(x);c.push(y+1);number[x][y+1]=0;
                        }
                        if(x-1>0&&y-1>0&&number[x-1][y-1]==1)
                        {
                            r.push(x-1);c.push(y-1);number[x-1][y-1]=0;
                        }
                        if(x-1>0&&y+1<=col&&number[x-1][y+1]==1)
                        {
                            r.push(x-1);c.push(y+1);number[x-1][y+1]=0;
                        }
                        if(x+1<=row&&y-1>0&&number[x+1][y-1]==1)
                        {
                            r.push(x+1);c.push(y-1);number[x+1][y-1]=0;
                        }
                        if(x+1<=row&&y+1<=col&&number[x+1][y+1]==1)
                        {
                            r.push(x+1);c.push(y+1);number[x+1][y+1]=0;
                        }
            }
            printf("%d\n",count);
        }
        no=2;
    }
    return 0;
}

No comments:

Post a Comment