关闭

FZU Fire Game(两点BFS)

标签: FZU2150Fire Game
245人阅读 评论(0) 收藏 举报
分类:
Fire Game

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
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

思路:
直接BFS暴力完所有情况就好了,就是一下进去两个点同时搜。
先上最终代码吧
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define min(a,b)  (a<b?a:b)
#define max(a,b)  (a>b?a:b)
const int INF=0x3f3f3f3f;
int n,m;
int vis[15][15];
char s[15][15];
int to[4][2]={0,1,0,-1,1,0,-1,0};
struct node
{
    int x,y,step;
}w[100];
int judge(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&s[x][y]=='#')
        return 1;
    return 0;
}
int bfs(node a,node b)//两点进去同时搜
{
    queue<node>q;
    q.push(a);
    q.push(b);
    vis[a.x][a.y]=1;
    vis[b.x][b.y]=1;
    int cnt=0;
    while(!q.empty())
    {
        a=q.front();
        cnt=max(cnt,a.step);
        for(int i=0;i<4;i++)
        {
            b.x=a.x+to[i][0],b.y=a.y+to[i][1],b.step=a.step+1;
            if(judge(b.x,b.y))
            {
                q.push(b);
                vis[b.x][b.y]=1;
            }
        }
        q.pop();
    }
    return cnt;
}
int main()
{
    int cont=0,t;
    scanf("%d",&t);
    while(++cont<=t)
    {
        int i,j,k,l,sum=0;
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='#')
                {
                    w[sum].x=i,w[sum].y=j,w[sum].step=0;//记录可搜点的坐标
                    sum++;
                }
            }
        }
        if(sum<=2)
            printf("Case %d: 0\n",cont);
        else
        {
            int ans=INF;
            for(i=0;i<sum;i++)
            {
                for(j=i;j<sum;j++)
                {
                    memset(vis,0,sizeof(vis));
                    int cnt=bfs(w[i],w[j]);//两点BFS
                    int flag=0;
                    for(k=0;k<n;k++)
                    {
                        for(l=0;l<m;l++)
                        {
                            if(s[k][l]=='#'&&!vis[k][l])//判断是否剩下没有点燃的草
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag)
                            break;
                    }
                    if(!flag)//全部烧完,则求最短时间
                        ans=min(ans,cnt);
                }
            }
            if(ans==INF)
                printf("Case %d: -1\n",cont);
            else
                printf("Case %d: %d\n",cont,ans);
        }
    }
    return 0;
}

记录一下我是怎么做的吧睡觉Orz
  第一次我是想把他们分成堆,如果超过两堆就直接输出-1,如果是两堆那么就BFS两次就好了,一堆的话就开始BFS全部的情况
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
int vis[15][15],vix[15][15];
char s[15][15];
int to[4][2]= {0,1,0,-1,1,0,-1,0};
int n,m,ans;
struct node
{
    int x,y,step;
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]=='#')
        return 1;
    return 0;
}
int l_bfs(int x,int y)
{
    queue<node>q;
    node now,next;
    now.x=x,now.y=y,now.step=0;
    q.push(now);
    vis[x][y]=1;
    vix[x][y]=1;
    int cnt=0;
    while(!q.empty())
    {
        now=q.front();
        if(now.step>cnt)
            cnt=now.step;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+to[i][0],yy=now.y+to[i][1];
            if(judge(xx,yy))
            {
                vis[xx][yy]=1;
                vix[xx][yy]=1;
                next.x=xx,next.y=yy,next.step=now.step+1;
                q.push(next);
            }
        }
    }
    return cnt;
}
int bfs(int x1,int y1,int x2,int y2)
{
    queue<node>q;
    node w1,w2,e1,e2,u;
    w1.x=x1,w1.y=y1,w1.step=0,e1.x=x2,e1.y=y2,e1.step=0;
    q.push(w1);
    q.push(e1);
    vis[x1][y1]=1;
    vis[x2][y2]=1;
    int cnt=0;
    while(!q.empty())
    {
        u=q.front();
        if(u.step>cnt)
            cnt=u.step;
        for(int i=0; i<4; i++)
        {
            int x=u.x+to[i][0],y=u.y+to[i][1];
            if(judge(x,y))
            {
                vis[x][y]=1;
                w2.x=x,w2.y=y,w2.step=u.step+1;
                q.push(w2);
            }
        }
        q.pop();
    }
    return cnt;
}
int main()
{
    int t,a=0;
    scanf("%d",&t);
    while(++a<=t)
    {
        memset(vix,0,sizeof(vix));
        memset(vis,0,sizeof(vis));
        int i,j,k,l,x=0;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
            scanf("%s",s[i]);
        ans=0;
        int sum=INF;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='#'&&!vix[i][j])
                {
                    x++;
                    l_bfs(i,j);
                    if(x==2)
                        ans=sum,sum=INF;
                }
                if(vix[i][j]&&s[i][j]=='#')
                {
                    memset(vis,0,sizeof(vis));
                    int cnt=l_bfs(i,j);
                    if(cnt<sum)
                        sum=cnt;
                }
            }
        if(sum>ans&&x==2)
            ans=sum;
        if(x>2)
            printf("Case %d: -1\n",a);
        else if(x==2)
        {
            printf("Case %d: %d\n",a,ans);
        }
        else if(x==1)
        {
            ans=INF;
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    if(s[i][j]=='#')
                    {
                        for(k=i; k<n; k++)
                            for(l=0; l<m; l++)
                            {
                                if(s[k][l]=='#')
                                {
                                    memset(vis,0,sizeof(vis));
                                    int cnt=bfs(i,j,k,l);
                                    if(cnt<ans)
                                        ans=cnt;
                                }
                            }

                    }
            printf("Case %d: %d\n",a,ans);
        }
        else if(x==0)
            printf("Case %d: 0\n",a);
    }
    return 0;
}
不过这里不好控制的是两堆的情况,而且写了两个BFS,所以bug多多敲打(但是怎样就找不到0.0)

好吧,看了一下别人的,原来其实并不用分堆,直接判断烧没烧完就好了,于是改了一下
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int vis[15][15],vix[15][15];
char s[15][15];
int to[4][2]= {0,1,0,-1,1,0,-1,0};
int n,m,ans;
struct node
{
    int x,y,step;
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]=='#')
        return 1;
    return 0;
}
int l_bfs(int x,int y)
{
    queue<node>q;
    node now,next;
    now.x=x,now.y=y,now.step=0;
    q.push(now);
    vis[x][y]=1;
    vix[x][y]=1;
    int cnt=0;
    while(!q.empty())
    {
        now=q.front();
        if(now.step>cnt)
            cnt=now.step;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+to[i][0],yy=now.y+to[i][1];
            if(judge(xx,yy))
            {
                vis[xx][yy]=1;
                vix[xx][yy]=1;
                next.x=xx,next.y=yy,next.step=now.step+1;
                q.push(next);
            }
        }
    }
    return cnt;
}
int bfs(int x1,int y1,int x2,int y2)
{
    queue<node>q;
    node w1,w2,e1,e2,u;
    w1.x=x1,w1.y=y1,w1.step=0,e1.x=x2,e1.y=y2,e1.step=0;
    q.push(w1);
    q.push(e1);
    vis[x1][y1]=1;
    vis[x2][y2]=1;
    int cnt=0;
    while(!q.empty())
    {
        u=q.front();
        if(u.step>cnt)
            cnt=u.step;
        for(int i=0; i<4; i++)
        {
            int x=u.x+to[i][0],y=u.y+to[i][1];
            if(judge(x,y))
            {
                vis[x][y]=1;
                w2.x=x,w2.y=y,w2.step=u.step+1;
                q.push(w2);
            }
        }
        q.pop();
    }
    return cnt;
}
int main()
{
    int t,a=0;
    scanf("%d",&t);
    while(++a<=t)
    {
        memset(vix,0,sizeof(vix));
        memset(vis,0,sizeof(vis));
        int i,j,k,l,x=0;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
            scanf("%s",s[i]);
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='#'&&!vix[i][j])
                {
                    x++;
                    l_bfs(i,j);
                }
            }
        if(x>2)
            printf("Case %d: -1\n",a);
        else
        {
            ans=INF;
            for(i=0; i<n; i++)
                for(j=0; j<m; j++)
                    if(s[i][j]=='#')
                    {
                        for(k=i; k<n; k++)
                            for(l=0; l<m; l++)
                            {
                                if(s[k][l]=='#')
                                {
                                    memset(vis,0,sizeof(vis));
                                    int cnt=bfs(i,j,k,l);
                                    int flag=0;
                                    for(int si=0; si<n; si++)
                                    {
                                        for(int sj=0;sj<m; sj++)
                                        {
                                            if(s[si][sj]=='#'&&!vis[si][sj])
                                            {
                                                flag=1;
                                                break;
                                            }
                                        }
                                        if(flag)
                                            break;
                                    }
                                    if(!flag)
                                        ans=min(ans,cnt);
                                }
                            }
                    }
            if(ans==INF)
                printf("Case %d: -1\n",a);
            else
                printf("Case %d: %d\n",a,ans);
        }
    }
    return 0;
}
把小于3堆的都合并在了一起
A了,不过时间有点慢,并且比较乱,,,大神们都是把可以搜的点先存起来,然后再开始搜,也不用分堆了,于是又改改改,
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define min(a,b)  (a<b?a:b)
#define max(a,b)  (a>b?a:b)
const int INF=0x3f3f3f3f;
int n,m;
int vis[15][15];
char s[15][15];
int to[4][2]= {0,1,0,-1,1,0,-1,0};
struct node
{
    int x,y,step;
} w[100];
int judge(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&s[x][y]=='#')
        return 1;
    return 0;
}
int l_bfs(int x,int y)
{
    queue<node>q;
    node now,next;
    now.x=x,now.y=y,now.step=0;
    q.push(now);
    vis[x][y]=1;
    int cnt=0;
    while(!q.empty())
    {
        now=q.front();
        if(now.step>cnt)
            cnt=now.step;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+to[i][0],yy=now.y+to[i][1];
            if(judge(xx,yy))
            {
                vis[xx][yy]=1;
                next.x=xx,next.y=yy,next.step=now.step+1;
                q.push(next);
            }
        }
    }
    return cnt;
}
int bfs(node a,node b)//两点进去同时搜
{
    queue<node>q;
    q.push(a);
    q.push(b);
    vis[a.x][a.y]=1;
    vis[b.x][b.y]=1;
    int cnt=0;
    while(!q.empty())
    {
        a=q.front();
        cnt=max(cnt,a.step);
        for(int i=0; i<4; i++)
        {
            b.x=a.x+to[i][0],b.y=a.y+to[i][1],b.step=a.step+1;
            if(judge(b.x,b.y))
            {
                q.push(b);
                vis[b.x][b.y]=1;
            }
        }
        q.pop();
    }
    return cnt;
}
int main()
{
    int cont=0,t;
    scanf("%d",&t);
    while(++cont<=t)
    {
        memset(vis,0,sizeof(vis));
        int i,j,k,l,sum=0,x=0;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
        {
            scanf("%s",s[i]);
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='#')
                {
                    w[sum].x=i,w[sum].y=j,w[sum].step=0;//记录可搜点的坐标
                    sum++;
                }
            }
        }
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(s[i][j]=='#'&&!vis[i][j])
                {
                    x++;
                    l_bfs(i,j);
                }
            }
        if(x>2)
            printf("Case %d: -1\n",cont);
        else if(sum<=2)
            printf("Case %d: 0\n",cont);
        else
        {
            int ans=INF;
            for(i=0; i<sum; i++)
            {
                for(j=i; j<sum; j++)
                {
                    memset(vis,0,sizeof(vis));
                    int cnt=bfs(w[i],w[j]);
                    int flag=0;
                    for(k=0; k<n; k++)
                    {
                        for(l=0; l<m; l++)
                        {
                            if(s[k][l]=='#'&&!vis[k][l])
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag)
                            break;
                    }
                    if(!flag)
                        ans=min(ans,cnt);
                }
            }
            if(ans==INF)
                printf("Case %d: -1\n",cont);
            else
                printf("Case %d: %d\n",cont,ans);
        }
    }
    return 0;
}
好吧,时间还是没快多少睡觉,而且代码太长了,所以精简一下,最终就改成最上面的那个了~Orz
ps:此次最大的收获
1.存点搜(减少复杂度)
2.怎样判断是否搜完(很明显我想的太麻烦且凌乱了)
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    阶段性抽风
    格物,致知
    个人资料
    • 访问:63263次
    • 积分:4578
    • 等级:
    • 排名:第6426名
    • 原创:368篇
    • 转载:34篇
    • 译文:0篇
    • 评论:18条
    博客专栏
    ACM

    文章:332篇

    阅读:51460
    最新评论