FZU 2150 Fire Game(BFS)

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

题意:两个小孩分别在两个地方放火,问是否能烧光草地,若能求最少时间。

由于n,m<=10,考虑暴力枚举搜索(BFS)求最少时间。。附上(10000MS的代码)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
int dr[][2]={{0,1},{0,-1},{-1,0},{1,0}};
//typedef pair<int,int>pill;
struct point {
    int x, y;
    point() {}
    point(int a, int b) { x = a; y = b; }
};
char mp[20][20];
int vis[20][20];
int d[20][20];
int n,m,t,ans;
int bfs(int x1,int y1,int x2,int y2)
{
    CLEAR(d,INF);
    queue<point>q;
    point st;
    d[x1][y1]=d[x2][y2]=0;
    q.push(point(x1,y1));
    q.push(point(x2,y2));
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        REP(i,4)
        {
            int xx=st.x+dr[i][0];
            int yy=st.y+dr[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m)   continue;
            if(mp[xx][yy]!='#')  continue;
            if(d[xx][yy]>d[st.x][st.y]+1)
            {
                d[xx][yy]=d[st.x][st.y]+1;
                q.push(point(xx,yy));
            }
        }
    }
    int sum=0;
    REP(i,n)
    {
        REP(j,m)
       {
           if(mp[i][j]=='#')
              sum=max(sum,d[i][j]);
       }
    }
    return sum;
}
void solve()
{
    ans=INF;
    REP(i,n)
    {
        REP(j,m)
        {
            if(mp[i][j]=='#')
            {
                REP(k,n)
                {
                    REP(l,m)
                    {
                        if(k==i&&l<=j)  continue;//改了就超时
                        if(mp[k][l]=='#')
                            ans=min(ans,bfs(i,j,k,l));
                    }
                }
            }
        }
    }
//    cout<<"2333  "<<ans<<endl;
    printf("%d\n",ans==INF?-1:ans);
}
int main()
{
//     std::ios::sync_with_stdio(false);
     scanf("%d",&t);
     int cas=1;
     while(t--)
     {
         scanf("%d%d",&n,&m);
         getchar();
         int num=0;
         REP(i,n)
         {
             gets(mp[i]);
             REP(j,m)
             {
                 if(mp[i][j]=='#')
                    num++;
             }
         }
         printf("Case %d: ",cas++);
         if(num<=2)
         {
              cout<<0<<endl;
              continue;
         }
         solve();
     }
     return 0;
}

附上我的超时的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
int dr[][2]={{-1,0},{1,0},{0,-1},{0,1}};
typedef pair<int,int>pill;
char mp[15][15];
int vis[15][15];
int d[15][15];
int n,m,t,ans;
int bfs(int x1,int y1,int x2,int y2)
{
    CLEAR(d,INF);
    queue<pill>q;
    pill st;
    d[x1][y1]=d[x2][y2]=0;
    q.push(make_pair(x1,y1));
    q.push(make_pair(x2,y2));
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        REP(i,4)
        {
            int xx=st.first+dr[i][0];
            int yy=st.second+dr[i][1];
            if(xx<0&&xx>=n&&yy<0&&yy>=m)   continue;
            if(mp[xx][yy]!='#')  continue;
            if(d[xx][yy]>d[st.first][st.second]+1)
            {
                d[xx][yy]=d[st.first][st.second]+1;
                q.push(make_pair(xx,yy));
            }
        }
    }
    int sum=0;
    REP(i,n)
    {
        REP(j,m)
       {
           if(mp[i][j]=='#')
              sum=max(sum,d[i][j]);
       }
    }
    return sum;
}
void solve()
{
    ans=INF;
    REP(i,n)
    {
        REP(j,m)
        {
            if(mp[i][j]=='#')
            {
                REP(k,n)
                {
                    REP(l,m)
                    {
                        if(k<=i&&l<=j)  continue;
                        if(mp[k][l]=='#')
                            ans=min(ans,bfs(i,j,k,l));
                    }
                }
            }
        }
    }
//    cout<<"2333  "<<ans<<endl;
    printf("%d\n",ans==INF?-1:ans);
}
int main()
{
     std::ios::sync_with_stdio(false);
     cin>>t;
     int cas=1;
     while(t--)
     {
         cin>>n>>m;
         int num=0;
         REP(i,n)
         {
             REP(j,m)
             {
                 cin>>mp[i][j];
                 if(mp[i][j]=='#')
                    num++;
             }
         }
         printf("Case %d: ",cas++);
         if(num<=2)
         {
              cout<<0<<endl;
              continue;
         }
         solve();
     }
     return 0;
}

时间: 2024-08-08 05:38:38

FZU 2150 Fire Game(BFS)的相关文章

FZU 2150 Fire Game(BFS)

Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 cons

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 2150 Fire Game (暴力BFS)

[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间可以烧掉,如果烧不掉就输出-1 [解题思路]: 数据比较弱的情况下直接暴力枚举每块草坪上可以放的位置,比较高端的写法目前没有想到,以后想到了文章更新下~~ ps:由于一个细节没注意,导致WA了几乎一页,还以为FZU 判题出错了,后来突然发现每次从队列里拿出队首的元素,才是

FZU 2150 Fire Game(DFS+BFS)

题意  在n*m个格子组成的草地上   你可以选择两个是草('#')的格子点燃  每个点燃的格子在下一秒其四个相邻的是草的格子也会被点燃   问点燃所有的草至少需要多少秒 DFS和BFS的综合  如果'#'连通块的数量大于2个是肯定不能点燃所有的  先dfs判断连通块个数  再bfs找出选哪两个格子可以最快把草烧完 #include <map> #include <cstdio> #include <cstring> using namespace std; const

FZU - 2150 Fire Game(两点bfs)

题目大意: 给你一个n*m的图,里面有草也有空地(#代表草).现在有两个人各在一块草地点火要烧掉这些草,并且燃烧的草可以向上下左右四个方向蔓延,问最少多长时间可以将所有的草都烧完,不能全部烧完输出-1. 两个起点的BFS,感觉和求最短路差不多,依次枚举两个起点,找到步数最多的那个草地,再从每次枚举的结果中找最小的. #include<iostream> #include<cstdio> #include<cmath> #include<algorithm>

FZU 2150 Fire Game (高姿势bfs--两个起点)

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 c

FZU 2150 Fire Game --两点同步搜索

枚举两点,然后同步BFS,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #define Mod 1000000007 using namespace std; struct Po

FZU Problem 2150 Fire Game (双起点BFS啊 )

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 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 a

(FZU 2150) Fire Game (bfs)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 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 a