FZU

来源:互联网 发布:ubuntu双显卡驱动安装 编辑:程序博客网 时间:2024/06/06 06:47

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150点击打开链接

Problem 2150 Fire Game

Accept: 2540    Submit: 8781
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

 Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

 Output

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

 Sample Input

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

 Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2

很不错的题目 居然是fzu的。。 没什么不好 就是服务器emmm。。

一开始拿到这道题觉得很麻烦 需要分类讨论

确定草的连通度 可能是1   2  >2 

   如果为1 就两个起点进行暴力广搜搜索 找最小的步数(两张图记录两个火源的步数)

   如果为2 将两个点分开分别进行搜索 此时找最快蔓延全部的方法(一开始以为只需要判断xy方向上的最大位置最小位置 后面发现错了 因为蔓延方法路径可能只有一条) 也是    广搜 每个点枚举

   如果为>2   -1

然后打了一下午。。代码长的恐怖

然后搜了题解 直接将12合并 暴力搜两个点就行

当然一开始这种想法很锻炼对搜索的能力 在经历了warchess和这道题之后 有很明显的提高 重点还考验意志啊。。

好了不吹牛了。。

先付12000b代码

缩进很难看 估计也没人看吧

#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits.h>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;struct xjy{    int x,y;};xjy b,e;int n,m,k;queue <xjy > q;int dir[4][2]={1,0,-1,0,0,1,0,-1};char mmap1[20][20];char mmap2[20][20];void bfsgrass(){    while(!q.empty())        q.pop();    q.push(b);    while(!q.empty())    {        xjy mid=q.front();            q.pop();        mmap1[mid.x][mid.y]='.';        for(int i=0;i<4;i++)        {            xjy midmid;            midmid.x=mid.x+dir[i][0];            midmid.y=mid.y+dir[i][1];            if( mmap1[midmid.x][midmid.y]=='#')            {                q.push(midmid);            }        }    }}int bfsgrasstwo(){    int ans=0;    int endans=INT_MAX;    int endans1=INT_MAX;    int endans2=INT_MAX;    int minx=12,miny=12,maxx=0,maxy=0;    while(!q.empty())        q.pop();    q.push(b);    while(!q.empty())    {        xjy mid=q.front();            q.pop();        mmap2[mid.x][mid.y]='*';        minx=min(minx,mid.x);        maxx=max(maxx,mid.x);        miny=min(miny,mid.y);        maxy=max(maxy,mid.y);        for(int i=0;i<4;i++)        {            xjy midmid;            midmid.x=mid.x+dir[i][0];            midmid.y=mid.y+dir[i][1];            if( mmap2[midmid.x][midmid.y]=='#')            {                q.push(midmid);            }        }    }    for(int ii=minx;ii<=maxx;ii++)        for(int jj=miny;jj<=maxy;jj++)            if(mmap2[ii][jj]=='*')            {                xjy mid1;                xjy mid2;                mid1.x=ii;                mid1.y=jj;                int book[20][20];                for(int iii=0;iii<20;iii++)                {                    for(int jjj=0;jjj<20;jjj++)                        {                            book[iii][jjj]=INT_MAX;                        }                }                    //bfs                while(!q.empty())                    q.pop();                book[mid1.x][mid1.y]=0;                q.push(mid1);                while(!q.empty())                {                    xjy mid=q.front();                    q.pop();                    xjy midmid;                    for(int i1=0;i1<4;i1++)                    {                    midmid.x=mid.x+dir[i1][0];                    midmid.y=mid.y+dir[i1][1];                    if(mmap2[midmid.x][midmid.y]=='*'&&book[midmid.x][midmid.y]>(book[mid.x][mid.y]+1) )                        {                            q.push(midmid);                            book[midmid.x][midmid.y]=book[mid.x][mid.y]+1;                        }                    }                }                int ans=0;                for(int iiii=1;iiii<=m;iiii++)                {                    for(int jjjj=1;jjjj<=k;jjjj++)                    {                        if(book[iiii][jjjj]!=INT_MAX)                        {                              ans=max(ans,book[iiii][jjjj]);                        }                    }                }                endans1=min(endans1,ans);            }    for(int i=1;i<=m;i++)        {            for(int j=1;j<=k;j++)            {                    if(mmap2[i][j]=='#')                       {                            b.x=i;                            b.y=j;                            ans=0;                            minx=12,miny=12,maxx=0,maxy=0;                            while(!q.empty())                                q.pop();                            q.push(b);                            while(!q.empty())                            {                                xjy mid=q.front();                                q.pop();                                mmap2[mid.x][mid.y]='!';                                minx=min(minx,mid.x);                                maxx=max(maxx,mid.x);                                miny=min(miny,mid.y);                                maxy=max(maxy,mid.y);                                for(int i=0;i<4;i++)                                {                                    xjy midmid;                                    midmid.x=mid.x+dir[i][0];                                    midmid.y=mid.y+dir[i][1];                                    if( mmap2[midmid.x][midmid.y]=='#')                                    {                                        q.push(midmid);                                    }                                }                            }                            for(int ii=minx;ii<=maxx;ii++)                                for(int jj=miny;jj<=maxy;jj++)                                    if(mmap2[ii][jj]=='!')                                    {                                        xjy mid1;                                        xjy mid2;                                        mid1.x=ii;                                        mid1.y=jj;                                        int book[20][20];                                        for(int iii=0;iii<20;iii++)                                        {                                            for(int jjj=0;jjj<20;jjj++)                                                {                                                    book[iii][jjj]=INT_MAX;                                                }                                        }                    //bfs                                        while(!q.empty())                                            q.pop();                                        book[mid1.x][mid1.y]=0;                                        q.push(mid1);                                        while(!q.empty())                                        {                                            xjy mid=q.front();                                            q.pop();                                            xjy midmid;                                            for(int i1=0;i1<4;i1++)                                            {                                            midmid.x=mid.x+dir[i1][0];                                            midmid.y=mid.y+dir[i1][1];                                            if(mmap2[midmid.x][midmid.y]=='!'&&book[midmid.x][midmid.y]>(book[mid.x][mid.y]+1) )                                                {                                                    q.push(midmid);                                                    book[midmid.x][midmid.y]=book[mid.x][mid.y]+1;                                                }                                            }                                        }                                        int ans=0;                                        for(int iiii=1;iiii<=m;iiii++)                                        {                                            for(int jjjj=1;jjjj<=k;jjjj++)                                            {                                            if(book[iiii][jjjj]!=INT_MAX)                                                {                                                    ans=max(ans,book[iiii][jjjj]);                                                }                                            }                                        }                                        endans2=min(endans2,ans);                                    }                                    endans=max(endans1,endans2);                                    return endans;                        }            }        }    return endans1;}int bfsgrassone(){    xjy mid1;    xjy mid2;    int endans=INT_MAX;    for(int i=1;i<=m;i++)        {            for(int j=1;j<=k;j++)            {                    if(mmap2[i][j]=='#')                        {                            mid1.x=i,mid1.y=j;                            for(int ii=1;ii<=m;ii++)                                {                                    for(int jj=1;jj<=k;jj++)                                    {                                            if(mmap2[ii][jj]=='#')                                                {                                                    mid2.x=ii,mid2.y=jj;                                                        int book[20][20];                                                        for(int iii=0;iii<20;iii++)                                                            {                                                            for(int jjj=0;jjj<20;jjj++)                                                                {                                                                    book[iii][jjj]=INT_MAX;                                                                }                                                            }                                                            //bfs                                                            while(!q.empty())                                                                q.pop();                                                            book[mid1.x][mid1.y]=0;                                                            book[mid2.x][mid2.y]=0;                                                            q.push(mid1);                                                            q.push(mid2);                                                            while(!q.empty())                                                            {                                                                xjy mid=q.front();                                                                q.pop();                                                                xjy midmid;                                                                for(int i1=0;i1<4;i1++)                                                                {                                                                    midmid.x=mid.x+dir[i1][0];                                                                    midmid.y=mid.y+dir[i1][1];                                                                    if(mmap2[midmid.x][midmid.y]=='#'&&book[midmid.x][midmid.y]>(book[mid.x][mid.y]+1) )                                                                    {                                                                        q.push(midmid);                                                                        book[midmid.x][midmid.y]=book[mid.x][mid.y]+1;                                                                    }                                                                }                                                            }                                                            int ans=0;                                                            for(int iiii=1;iiii<=m;iiii++)                                                            {                                                                for(int jjjj=1;jjjj<=k;jjjj++)                                                                {                                                                    if(book[iiii][jjjj]!=INT_MAX)                                                                        {                                                                            ans=max(ans,book[iiii][jjjj]);                                                                        }                                                                }                                                            }                                                            endans=min(endans,ans);                                                }                                    }                                }                        }            }        }        return endans;}int main(){    scanf("%d",&n);    for(int step=1;step<=n;step++)    {        int minx,miny,maxx,maxy;        int cnt=0;        memset(mmap1,'.',sizeof(mmap1));        memset(mmap2,'.',sizeof(mmap2));        while(!q.empty())            q.pop();        scanf("%d%d",&m,&k);        getchar();        for(int i=1;i<=m;i++)        {            for(int j=1;j<=k;j++)            {                    scanf("%c",&mmap1[i][j]);                    mmap2[i][j]=mmap1[i][j];            }getchar();        }        for(int i=1;i<=m;i++)        {            for(int j=1;j<=k;j++)            {                    if(mmap1[i][j]=='#')                       {                            b.x=i;                            b.y=j;                            bfsgrass();                            cnt++;                       }            }        }        cout << "Case " << step << ": ";        if(cnt>=3)            cout << "-1" << endl;        else if(cnt==2)        {           cout << bfsgrasstwo() << endl;        }        else if(cnt==1)        {            cout << bfsgrassone() << endl;        }    }}

然后这个是爆搜的

   

#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits.h>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;char mmap[20][20];int m,k;int ans=0;struct xjy{    int x;    int y;};int dir[4][2]={0,1,0,-1,1,0,-1,0};int bfs(xjy mid1,xjy mid2){    queue<xjy> q;    int aans=0;    int book[20][20];    for(int i=0;i<=11;i++)        for(int j=0;j<=11;j++)        {            book[i][j]=INT_MAX;        }    book[mid1.x][mid1.y]=0;    book[mid2.x][mid2.y]=0;    while(!q.empty())        q.pop();    q.push(mid1);    q.push(mid2);    while(!q.empty())    {        xjy mid;        mid=q.front();        q.pop();        xjy midmid;        for(int i=0;i<4;i++)        {            midmid.x=mid.x+dir[i][0];            midmid.y=mid.y+dir[i][1];            if(book[midmid.x][midmid.y]>(book[mid.x][mid.y]+1)&&mmap[midmid.x][midmid.y]=='#')            {                book[midmid.x][midmid.y]=book[mid.x][mid.y]+1;                q.push(midmid);            }        }    }    for(int i=1;i<=m;i++)        for(int j=1;j<=k;j++)    {        if(mmap[i][j]=='#')            aans=max(aans,book[i][j]);    }    return aans;}int main(){    int n=0;    scanf("%d",&n);    for(int step=1;step<=n;step++)    {        ans=INT_MAX;        memset(mmap,'.',sizeof(mmap));        scanf("%d%d",&m,&k);        for(int i=1;i<=m;i++)                for(int j=1;j<=k;j++)                    {                        scanf(" %c",&mmap[i][j]);                    }            for(int i1=1;i1<=m;i1++)                for(int j1=1;j1<=k;j1++)                    if(mmap[i1][j1]=='#')                    {                        for(int i2=1;i2<=m;i2++)                            for(int j2=1;j2<=k;j2++)                                if(mmap[i2][j2]=='#')                                {                                    xjy mid1;                                    xjy mid2;                                    mid1.x=i1;                                    mid2.x=i2;                                    mid1.y=j1;                                    mid2.y=j2;                                    ans=min(ans,bfs(mid1,mid2));                                }                    }            cout  << "Case " << step << ": " ;            if(ans!=INT_MAX)                cout  << ans <<endl;            else                cout << "-1" << endl;    }}