Friday, June 10, 2011

782 - Contour Painting


#include<iostream>
#include<cstdio>
#include<cstring>
#define M 35
#define N 100
using namespace std;

char graph[M][N],bound,mark,inp[10];
int pocket,hv,m,n,Max;
int next[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
bool Visited[M][N];
void DFS_Visit(int,int);

int main()
{
    int t,i=0,j,n;
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    gets(inp);
    sscanf(inp,"%d",&t);
    m = -1;

    while(t--)
    {
       
        while(gets(graph[++m]))
            if(graph[m][0]=='_')
                break;

        bound = '*';Max=0;
        for(i=0;i<m;i++)
        {
            n = strlen(graph[i]);
            if(Max<n)Max=n;
            for(j=0;j<n;j++)
                if(graph[i][j]!=' ' && graph[i][j]!='*')
                    bound = graph[i][j];
        }
        Max=Max+5;

        for(i=0;i<=m;i++)
        {
            n = strlen(graph[i]);
            for(j=n;j<=Max;j++)
                graph[i][j] = ' ';
            graph[i][j] = '\0';
        }

        memset(Visited,false,sizeof(Visited));

        for(i=0;i<m;i++)
        {
            n = strlen(graph[i]);
            for(j=0;j<n;j++)
                if(graph[i][j]=='*')
                {
                    graph[i][j] = ' ';
                    DFS_Visit(i,j);
                }
        }
        for(i=0;i<=m;i++)
        {
            n = strlen(graph[i]);
            for(j=n-1;graph[i][j]==' ';j--);
            graph[i][j+1] = '\0';
        }

        for(i=0;i<=m;i++)
            printf("%s\n",graph[i]);
        m = -1;
    }
    return 0;
}

void DFS_Visit(int x,int y)
{
    int i,tx,ty;
    Visited[x][y]=true;
   
    for(i=0;i<4;i++)
    {
        tx=x+next[i][0],ty=y+next[i][1];
        if(tx>=0&&tx<m&&ty>=0&&ty<Max)
        {
            if(graph[tx][ty]==bound)
                graph[x][y] = '#';
            if(!Visited[tx][ty]&&graph[tx][ty]==' ')
                DFS_Visit(tx,ty);
        }
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.