I - Fire Game

题目大意:

火焰游戏

在一个 N*M的网格里面有一些用‘#’表示的干草,可以选择在两个地方放火,火焰可以向四周燃烧,求烧完这些干草最快需要多少时间

////////////////////////////////////////////////////////////////////////

可以很容易想到任意取两个有干草的地方,看看那个可以烧的最快,并且烧完,1 <= n <=10, 1 <= m <=10,地图也不是太大,可以尝试一下...........................

没有问题.............

#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;

#define maxn 15
const int oo = 0xfffffff;

struct node{int x, y, step;}a[105];//记录所有干草的位置
char G[maxn][maxn];
int v[maxn][maxn];//标记数组
int dir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
int M, N;

int OK()
{
    int i, j;

for(i=0; i<M; i++)
    for(j=0; j<N; j++)
    {
        if(G[i][j]==‘#‘ && v[i][j] == 0)
            return 0;
    }

return 1;
}
int BFS(node s, node q)//任意的选取两堆干草
{
    queue<node> Q;
    Q.push(s);Q.push(q);
    v[s.x][s.y] = v[q.x][q.y] = 1;

while(Q.size())
    {
        s = Q.front();Q.pop();

for(int i=0; i<4; i++)
        {
            q = s;
            q.x += dir[i][0];
            q.y += dir[i][1];

if(q.x>=0 && q.x< M && q.y >= 0 && q.y < N && G[q.x][q.y] == ‘#‘ && v[q.x][q.y] == 0)
            {
                v[q.x][q.y] = 1;
                q.step ++;

Q.push(q);
            }
        }
    }

return s.step;
}

int main()
{
    int T, t=1;

scanf("%d", &T);

while(T--)
    {
        int i, j, k=0, ans=oo;

scanf("%d%d", &M, &N);

for(i=0; i<M; i++)
        {
            scanf("%s", G[i]);
            for(j=0; j<N; j++)
            {
                if(G[i][j] == ‘#‘)
                {
                    a[k].step = 0;
                    a[k].x = i;
                    a[k++].y = j;
                }
            }
        }

for(i=0; i<k; i++)
        for(j=i; j<k; j++)
        {
            memset(v, 0, sizeof(v));
            int L = BFS(a[i], a[j]);

if(L < ans && OK())
                ans = L;
        }

if(ans == oo)
            printf("Case %d: -1\n", t++);
        else
            printf("Case %d: %d\n", t++, ans);
    }

return 0;
}

时间: 2024-10-13 11:23:46

I - Fire Game的相关文章

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 {

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

Fire! 又是图 bfs

Joe works in a maze.  Unfortunately, portions of the maze havecaught on  re, and the owner of the maze neglected to create a  reescape plan. Help Joe escape the maze.Given Joe's location in the maze and which squares of the mazeare on  re, you must d

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

Fire Maze(广度优先搜索)

Fire Maze Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 53(19 users) Total Accepted: 26(17 users) Rating: Special Judge: No Description After escaping from Figo's chase, Severus falls into a N * M maze designed by Figo. At first, Severus is

(简单) UVA 11624 Fire! ,BFS。

Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the maze are o

(简单) FZU 2150 Fire Game ,Floyd。

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. Firstl

uva11624 - Fire! 两次bfs

题目链接 Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe's location in the maze and which squares of the

ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】

题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题意:给出n个点,n-1条边的一棵树.然后要在两个点上建立两个消防站.让全部点的到消防站最大距离的点的这个距离最小. 分析:首先先求这个树的直径.然后在树的直径的中点处把树分成两棵树.然后在把两棵树分别取中点的最大值就是ans值. 这个题目数据有点水了感觉... AC代码: #include <cstdi