(种子填充法) zoj 3304

Prison Break

Time Limit: 1 Second      Memory Limit: 32768 KB

In a prison, there are R*C rooms, and there are N prisoners in it, each in a room.

	For example: (R=5, C=6, N=7, 'P' means prisoner, '.' means empty)

			......
			..P...
			.PPP..
			..P..P
			.....P

On the first day, all the prisoners do not know each other, so a prisoner can only enter the rooms connected to his own which are empty. Two rooms are connected only when they share a wall.

		R\C	123456
		1	......
		2	..P...
		3	.PPP..
		4	..P..P
		5	.....P

	in this case, the (R3,C3) one can't go anywhere, but the others can go to any place marked with '.', they can even get out of the prison.

On the second day, all the prisoners can't find a way to get out of the prison (out of the R*C area) are so disappointed so they kill themselves and turn to be ghosts.

	('G' means ghost)

			......
			..P...
			.PGP..
			..P..P
			.....P

On the third day, all the prisoners alive know about the story about ghosts and are so scared that they decide to escape from the prison, so they make their rooms not only connected to the rooms sharing a wall with theirs, but also to the rooms diagonal adjacent. Of course, all the prisoners won't go into a room with a ghost.

On the Fourth day, the officer in the prison feels something uncommon, so he puts a guard in each empty room. Now, the prisoners can only enter the rooms adjacent to theirs which with a prisoner in it.

	('X' means a guard)

			XXXXXX
			XXPXXX
			XPGPXX
			XXPXXP
			XXXXXP

On the Fifth day, all the prisoners make a group with all the prisoners whose rooms they can arrive and get ready for the escape.

	('1' means group 1, '2' means group 2)

			XXXXXX
			XX1XXX
			X1G1XX
			XX1XX2
			XXXXX2

On the Sixth day, the guards are so tired and they go home to sleep. Each group of prisoners get out of the prison finally.

			......
			......
			......
			......
			......

Above is the story of prison break, however, of all the groups, what is the greatest number of prisoners in a group?

Input

The input contains multiple test cases(no more than 20 test cases). Each test case contains three parts:

1 Two integers R(1<= R <= 1000), C(1<= C <=1000) as mentioned above.

R rows each with C characters, the description of the prison on the first day. '.' means empty, 'P' means prisoner.

3 an empty line.

Process to the end-of-file.

Output

For each test case print a single line that contains answer.

Sample Input

5 6
......
..P...
.PPP..
..P..P
.....P

5 6
PPPP.P
....P.
......
......
PPPP.P

Sample Output

4
6

 

 

 

从边界开始搜就好了。。。水啊。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
int r,c,ans,cnt;
char s[1010][1010];
int dx1[4]={0,0,1,-1};
int dy1[4]={1,-1,0,0};
int dx2[8]={0,0,1,-1,1,1,-1,-1};
int dy2[8]={1,-1,0,0,1,-1,1,-1};
int vis[1010][1010];
void dfs1(int x,int y)
{
    if(s[x][y]=='P')
    {
        vis[x][y]=2;
        return ;
    }
    else
        vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int xx,yy;
        xx=x+dx1[i],yy=y+dy1[i];
        if(xx>=0&&yy>=0&&xx<r&&yy<c&&!vis[xx][yy])
               dfs1(xx,yy);
    }
}
void dfs2(int x,int y)
{
    cnt++;
    vis[x][y]=1;
    for(int i=0;i<8;i++)
    {
        int xx,yy;
        xx=x+dx2[i],yy=y+dy2[i];
        if(xx>=0&&yy>=0&&xx<r&&yy<c&&vis[xx][yy]==2&&s[xx][yy]=='P')
            dfs2(xx,yy);
    }
}
int main()
{
    while(scanf("%d%d",&r,&c)!=EOF)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<r;i++)
            scanf("%s",s[i]);
        for(int i=0;i<c;i++)
            if(!vis[0][i])
                dfs1(0,i);
        for(int i=1;i<r;i++)
            if(!vis[i][0])
                dfs1(i,0);
        for(int i=1;i<c;i++)
            if(!vis[r-1][i])
                dfs1(r-1,i);
        for(int i=1;i<r-1;i++)
            if(!vis[i][c-1])
                dfs1(i,c-1);
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(s[i][j]=='P'&&vis[i][j]==2)
                {
                    cnt=0;
                    dfs2(i,j);
                    ans=max(ans,cnt);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

posted @ 2015-05-26 09:11  waterfull  阅读(410)  评论(0编辑  收藏  举报