FZU-2150 广搜_两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向-程序员宅基地

技术标签: 搜索  

Problem 2150 Fire Game
Accept: 3701 Submit: 12637
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
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题意:
两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。如不能烧完输出-1。
*题意引用自luciozhang的博文。

没有人能一开始就想到正确的思路,就像第一次看到深搜和广搜的一些细节,书上都处理的很好,当时不理解为什么要这样写,其实没有什么不理解的,自己写一遍,再自己做几道题,就知道这些地方为什么这样写了,肯定是为了方便嘛!
下面这个代码有很多问题,也懒得改了,放在上面留个纪念,决定看下题解,自己再写一遍。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
char a[11][11];
int book[11][11];
struct node{
    int x;
    int y;
    int time;//记录自己是第几次被引燃的
};
queue<node> q;
int mintime;//同一次测试最小的时间
int time;//同一次测试每种情形的时间
int d[4][2]={
   {
   0,1},{
   1,0},{-1,0},{
   0,-1}};

void bfs(node n,int bx,int by){
    if(time<n.time)//记录最大时间,也就是实际需要的时间
        time=n.time;
    if(a[n.x+d[0][0]][n.y+d[0][1]]=='#' && n.y<by && book[n.x+d[0][0]][n.y+d[0][1]]){
        node tmp;
        tmp.x=n.x;
        tmp.y=n.y+1;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);//不采用把烧过的草变为空地,那会破坏原图,所以用集合来标记。
    }
    if(a[n.x+d[1][0]][n.y+d[1][1]]=='#' && n.x<bx && book[n.x+d[1][0]][n.y+d[1][1]]){
        node tmp;
        tmp.x=n.x+1;
        tmp.y=n.y;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
    if(a[n.x+d[2][0]][n.y+d[2][1]]=='#' && n.x>0 && book[n.x+d[2][0]][n.y+d[2][1]]){
        node tmp;
        tmp.x=n.x-1;
        tmp.y=n.y;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
    if(a[n.x+d[3][0]][n.y+d[3][1]]=='#' && n.y>0  && book[n.x+d[3][0]][n.y+d[3][1]]){
        node tmp;
        tmp.x=n.x;
        tmp.y=n.y-1;
        tmp.time=n.time+1;
        q.push(tmp);
        book.insert(100*tmp.x+tmp.y);
    }
}
int main(){
    int t;
    int flag=0;//标记是否成功
    cin >> t;//接受测试次数
    int m,n;
    int cnt;//记录草的数目
    while(t--){
        cnt=0;
        mintime=999;
        cin >> m >> n;// m rows n colums
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++){
    
                cin >> a[i][j];
                if(a[i][j]=='#')
                    cnt++;
            }
        for(int i=0;i<m;i++)//找第一个点
            for(int j=0;j<n;j++){
    
                if(a[i][j]=='#'){
                    node tmp;
                    tmp.x=i;
                    tmp.y=j-1;
                    tmp.time=0;
                    node mothertmp=tmp;
                    int mother=100*tmp.x+tmp.y;
                    for(int k=0;k<m;k++)//找第二个点
                        for(int l=0;l<n;l++){
    
                            if(a[k][l]=='#' && (k!=i||l!=j)){
                                book.clear();
                                time=0;
                                node tmp;
                                tmp.x=k;
                                tmp.y=l-1;
                                tmp.time=0;
                                q.push(mothertmp);
                                book.insert(100*tmp.x+tmp.y);
                                book.insert(mother);
                                while(!q.empty()){
                                    //模拟火的蔓延
                                    node tmp = q.front();
                                    bfs(tmp,m,n);
                                    q.pop();
                                }
                                if(mintime>time)
                                    mintime = time;
                                if(book.size()==cnt && flag==0)
                                    flag=1;
                            }
                        }
                }
            }
            while(!q.empty())
                q.pop();
            if(flag==1)
                cout << mintime << endl;
            else
                cout << -1 << endl;
        }
    system("pause");
    return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39557517/article/details/78311797

智能推荐

全志A33开发板Linux内核定时器编程-程序员宅基地

文章浏览阅读160次。开发平台* 芯灵思SinlinxA33开发板淘宝店铺: https://sinlinx.taobao.com/嵌入式linux 开发板交流 641395230Linux 内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 和 kernel/timer.c 文件中。内核定时器的数据结构s..._opengl 全志a33

Rasa官方教程翻译6 模型评估_rasa 评估-程序员宅基地

文章浏览阅读675次。评估NLU模型比较NLU管道意图分类响应选择实体提取实体评分评估Core模型比较Core配置端对端评估注意如果你想优化NLU模型的超参数,参阅教程tutorial.评估NLU模型机器学习的一个标准技术是把一些数据分离出来作为测试集。你可以使用下面命令把NLU训练数据集拆分成训练数据集和测试数据集:rasa data split nl..._rasa 评估

TLS抓包分析-程序员宅基地

文章浏览阅读6.8k次,点赞6次,收藏36次。TLS(Transport Layer Security, 传输层安全):用于保证Web通信以及其他流行协议的安全。TLS的前身是安全套接字层(SSL)。TLSv1.2版本运行在面向流的协议(如TCP)之上。记录协议提供分片、压缩、完整性保护以及对客户端与服务器之间所交换数据的加密服务。信息交换协议负责建立身份,进行认证,提示警报,以及为用于每一条连接的记录协议提供唯一的密钥材料。TL..._tls抓包

android studio 代码提示消失_android studio flutter没有代码提示-程序员宅基地

文章浏览阅读8.8k次,点赞3次,收藏3次。android studio编辑过程中,按下选择提示方法自动消失windowalt+shift+insert或者在空白区域点击鼠标右键将这个关闭即可mac双击shift 搜索 column select 选择最下面的把这个状态设置成关闭状态补充:发现这样有时还是不行,比如当稍微等一会后,方法介绍弹出后还是不行,所以需要关闭自动方法介绍自动提示..._android studio flutter没有代码提示

C++ 查看dll导出函数_c++dll 查看源码-程序员宅基地

文章浏览阅读5.3k次,点赞3次,收藏2次。输入如下命令,查看dll导出函数:dumpbin -exports D:\xxx.dll 回车_c++dll 查看源码

PyQt5 颜色、字体、打开文件对话框_pyqt5 textclip color有几种?-程序员宅基地

文章浏览阅读3.5k次。有点简单,运行一下看结果即可。大栗子from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QColorDialog, QFontDialog, QTextEdit, QFileDialogimport sysclass Example(QWidget): def __init__(self): ..._pyqt5 textclip color有几种?

随便推点

win虚拟机 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务_虚拟机 tns监听程序-程序员宅基地

文章浏览阅读3.2w次,点赞2次,收藏13次。转载请注明作者(独孤尚良dugushangliang)出处:https://blog.csdn.net/dugushangliang/article/details/81835747附注:如此文未能解决问题,请移步另一篇:https://blog.csdn.net/dugushangliang/article/details/89513494下为原答案:本人及其w..._虚拟机 tns监听程序

解决 el-table 多选框,前端分页,选中失效问题;多选改为单选_el-table的reserve-selection和rowkey都有,但是没效果-程序员宅基地

文章浏览阅读1.6k次,点赞3次,收藏3次。【代码】解决 el-table 多选框,前端分页,选中失效问题。_el-table的reserve-selection和rowkey都有,但是没效果

WIFI模块ESP8266使用总结和示例_esp8266输出5v-程序员宅基地

文章浏览阅读7.5w次,点赞24次,收藏197次。ESP8266这个模块真的很便宜,但比之前用过的各种wifi模块都难折腾。主要是很多细节说明书都没怎么提及,或者是我没看仔细。总之,本篇就根据我的使用经历来教大家如何折腾这东东。引脚连接:GND:接地GPIO16:其实是RST,低电平复位,所以为了正常工作,直接连接VCC即可VCC:接3.3V,看过其他教程说不能接5V,不过小编有试过直接用5V来把玩,玩了一段时间都没啥问题_esp8266输出5v

opengl | openmesh 读取显示3d模型文件_基于opengl的三维模型读取-程序员宅基地

文章浏览阅读2.4w次,点赞10次,收藏73次。操作鼠标控制物体旋转移动,滚轮缩放F1,F2,F3 可以更换显示文件 (file1:cow.obj file2:cactus.ply file3 : Armadillo.off)F4 更换显示模式 (wire,flat,flatlines)截图 使用命令行显示当前状态准备openmesh的下载配置下载最新的安装包安装openmesh配置vs工具-》选项-》项目和解决方案-》VC++目录_基于opengl的三维模型读取

sed & awk 概述-程序员宅基地

文章浏览阅读71次。概述一般情况下,从grep到sed和awk的学习过程是很自然的。sed和awk是一般用户、程序员和系统管理员们处理文本文件的有力工具。sed的名字来源于其功能,它是个字符流编辑器(stream editor),可以很好地完成对多个文件的一系列编辑工作。awk的名字来源于它的开发人Aho、Weinberger和Kernighan,它是一种程序设计语言,非常适合结构化数据的处理和格式化报表...

Flutter小知识:RichText(富文本标签)_flutter richtext-程序员宅基地

文章浏览阅读1w次,点赞2次,收藏9次。Flutter小知识:RichText富文本标签苹果风格弹框RichText富文本标签仁义道德,也是一种奢侈。——疾风剑豪先来看看今天的效果:什么是富文本:富文本格式(Rich Text Format)即RTF格式,又称多文本格式,是由微软公司开发的跨平台文档格式。大多数的文字处理软件都能读取和保存RTF文档。富文本格式 (RTF) 是一种方便于不同的设备、系统查看的文本和图形文档格式。来自百度百科在Flutter中富文本指的就是多种文本格式苹果风格弹框 @override _flutter richtext

推荐文章

热门文章

相关标签