POJ-3051

来源:互联网 发布:安卓微信数据迁移 编辑:程序博客网 时间:2024/05/16 16:17

Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wishes to determine the largest 'contiguous' (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by traversing adjacent vertical or horizontal pixels that are part of the pasture. (It is easy to create pastures with very strange shapes, even circles that surround other circles.) 

Each photo has been digitally enhanced to show pasture area as an asterisk ('*') and non-pasture area as a period ('.'). Here is a 10 x 5 sample satellite photo: 

..*.....** 
.**..***** 
.*...*.... 
..****.*** 
..****.***
 

This photo shows three contiguous pastures of 4, 16, and 6 pixels. Help FJ find the largest contiguous pasture in each of his satellite photos.
Input
* Line 1: Two space-separated integers: W and H 

* Lines 2..H+1: Each line contains W "*" or "." characters representing one raster line of a satellite photograph.
Output
* Line 1: The size of the largest contiguous field in the satellite photo.
Sample Input
10 5..*.....**.**..*****.*...*......****.***..****.***
Sample Output
16

    这就是难了我一个多小时的题,类似于原来一个相邻细胞的题。开始广搜,一直超时超时,加上些剪枝,上了scanf(),gets()都不行,后来深搜,对了。搞不明白。

代码如下:

深搜AC代码:

#include<iostream>#include<cstring>#include<stdio.h>using namespace std;int n,m,cnt;char mp[1001][1001];int vis[1001][1001];int dxy[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int ok(int x,int y){    if(x>=0 && x<n && y>=0 && y<m)        return 1;    return 0;}void dfs(int x,int y){    cnt++;    vis[x][y]=1;    int xx,yy;    for(int k=0;k<4;k++)    {       xx=x+dxy[k][0];       yy=y+dxy[k][1];       if(ok(xx,yy))       {           if(vis[xx][yy]==0 && mp[xx][yy]=='*')            dfs(xx,yy);       }    }    return ;}int main(){    while(cin>>m>>n && (m+n))    {        int ans=0;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                cin>>mp[i][j];            }        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(mp[i][j]=='*' && vis[i][j]==0)                {                   cnt=0;                   dfs(i,j);                   if(ans<cnt)                   {                       ans=cnt;                   }                }            }        }        cout<<ans<<endl;        //printf("%d\n",ans);    }    return 0;}


广搜超时:

#include<iostream>#include<cstring>#include<stdio.h>using namespace std;int n,m;char mp[82][1010];int vis[82][1010];int dxy[4][2]={{0,1},{0,-1},{1,0},{-1,0}};struct Node{    int x,y;}q[800010];;int ok(int x,int y){    if(x>=0 && x<n && y>=0 && y<m)        return 1;    return 0;}int ioan(int x,int y){    int cnt=1;    memset(q,0,sizeof(q));    Node a,b;    int head=0,tail=1;    q[1].x=x;    q[1].y=y;    vis[x][y]=1;    do    {        head++;        a=q[head];        for(int k=0;k<4;k++)        {            b.x=a.x+dxy[k][0];            b.y=a.y+dxy[k][1];            if(ok(b.x,b.y) && vis[b.x][b.y]==0 && mp[b.x][b.y]=='*')            {                tail++;                cnt++;                q[tail]=b;                vis[b.x][b.y]=1;            }        }    }while(head<tail);    return cnt;}int main(){    char s[82];    while(~scanf("%d %d",&m,&n))    {        //gets(s);        int ans=0;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            scanf("%s",&mp[i]);            //cin>>mp[i];            //gets(mp[i]);        }        int mid=m*n/2;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(mp[i][j]=='*' && vis[i][j]==0)                {                    //cout<<"i  j"<<i<<"  "<<j<<endl;                    int xx=ioan(i,j);                    if(ans<xx)                        ans=xx;                    if(ans>mid)                        break;                }            }        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击