fzu 2150(bfs)

 Problem 2150 Fire Game

Accept: 693    Submit: 2657 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

4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#

 Sample Output

题目描述:

给定一张图。图中"#"代表草,‘.‘代表地,可能有几个草地,给你两把火,问烧完整个图中的草的最短时间。

由于n<=10,所以设立一个结构体,里面的属性有位置,和被烧的时间,然后枚举两把火的位置,开始烧,

用cnt计数烧过的草,然后将枚举的两个位置放进队列,如果两个位置重合的话。cnt=1,否则cnt=2;

其实我们可以认为i这两把火是有一把火烧起来的,那么之后就跟裸的宽搜是一样的,(其实跟锁屏样式的两种判断转换成一种差不多)time,每层+1.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;

char MAP[20][20];
int visit[20][20];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
int n,m,sum,cnt;
struct node
{
  int x, y,Time;
};

int main()
{
   int t;
   scanf("%d",&t);
   int _case=0;
   while(t--)
   {
       queue< node > Q;
       sum=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        getchar();
        for(int j=0;j<m;j++)
        {

            scanf("%c",&MAP[i][j]);
            if(MAP[i][j]==‘#‘)
            {
                ++sum;
            }
        }
    }
     int answer=inf;
    int cntll=0;
     for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
     {
         if(MAP[i][j] == ‘#‘)
         {
            for(int xx=i;xx<n;xx++)
            for(int yy=0;yy<m;yy++)
         {
             if(xx==i && yy<j)
                continue;
             memset(visit,0,sizeof(visit));
            if(MAP[xx][yy]==‘#‘)
            {
                cnt=2;
                if(i==xx && j==yy)
                {
                    cnt=1;
                }
              visit[i][j]=1; visit[xx][yy]=1;
              node NODE1,NODE2,NODE;
              NODE1.x=i; NODE1.y=j;NODE1.Time=0;
              NODE2.x=xx; NODE2.y=yy;NODE2.Time=0;
               Q.push(NODE1);
               Q.push(NODE2);
             while(!Q.empty())
            {

                NODE=Q.front();
                Q.pop();
                node point;
                for(int k=0;k<4;k++)
                {
                    point.x=NODE.x+dir[k][0];
                    point.y=NODE.y+dir[k][1];
                    if(point.x>=0 && point.x<n && point.y>=0 && point.y<m)
                    if(visit[point.x][point.y]==0 && MAP[point.x][point.y]==‘#‘)
                     {
                         visit[point.x][point.y]=1;
                          point.Time=NODE.Time+1;
                         Q.push(point);
                          cnt=cnt+1;
                     }
                }
            }

             if(cnt==sum)
             {
                 answer=min(answer,NODE.Time);
             }
           }
          }
         }
        }
         if(answer!=inf)
         printf("Case %d: %d\n",++_case,answer);
         else
             printf("Case %d: -1\n",++_case);

     }

    return 0;
}
时间: 2024-08-11 01:35:35

fzu 2150(bfs)的相关文章

ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

FZU 2150 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 boar

FZU 2150 Fire Game (暴力BFS)

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

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,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #define Mod 1000000007 using namespace std; struct Po

(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

fzu 2150 Fire Game 【技巧BFS】

题目:fzu2150 Fire Game 题意:给出一个m*n的图,'#'表示草坪,' . '表示空地,然后可以选择在任意的两个草坪格子点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小? 分析:这个题目如果考虑技巧的话有点难度,但是鉴于数据范围比较小,我们可以暴力枚举任意的草坪所在的点,然后两个点压进队列里面BFS,去一个满足条件的最小值即可. 顺便说一下 fzu 2141 Sub-Bipartite Graph 的思路,比赛的时候没有做出来. 这个题目想的复

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(DFS+BFS)

Problem 2150 Fire Game Accept: 1357    Submit: 4807 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 gri

Fire Game FZU - 2150 (bfs)

Problem 2150 Fire Game Accept: 3772    Submit: 12868Time 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 gri