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

智能推荐

Maven编译打包项目 mvn clean install报错ERROR_mvn clean install有errors-程序员宅基地

文章浏览阅读1.1k次。在项目的target文件夹下把之前"mvn clean package"生成的压缩包(我的是jar包)删掉重新执行"mvn clean package"再执行"mvn clean install"即可_mvn clean install有errors

navacate连接不上mysql_navicat连接mysql失败怎么办-程序员宅基地

文章浏览阅读974次。Navicat连接mysql数据库时,不断报1405错误,下面是针对这个的解决办法:MySQL服务器正在运行,停止它。如果是作为Windows服务运行的服务器,进入计算机管理--->服务和应用程序------>服务。如果服务器不是作为服务而运行的,可能需要使用任务管理器来强制停止它。创建1个文本文件(此处命名为mysql-init.txt),并将下述命令置于单一行中:SET PASSW..._nvarchar链接不上数据库

Python的requests参数及方法_python requests 参数-程序员宅基地

文章浏览阅读2.2k次。Python的requests模块是一个常用的HTTP库,用于发送HTTP请求和处理响应。_python requests 参数

近5年典型的的APT攻击事件_2010谷歌网络被极光黑客攻击-程序员宅基地

文章浏览阅读2.7w次,点赞7次,收藏50次。APT攻击APT攻击是近几年来出现的一种高级攻击,具有难检测、持续时间长和攻击目标明确等特征。本文中,整理了近年来比较典型的几个APT攻击,并其攻击过程做了分析(为了加深自己对APT攻击的理解和学习)Google极光攻击2010年的Google Aurora(极光)攻击是一个十分著名的APT攻击。Google的一名雇员点击即时消息中的一条恶意链接,引发了一系列事件导致这个搜_2010谷歌网络被极光黑客攻击

Android 开发的现状及发展前景_android现状-程序员宅基地

文章浏览阅读8.8k次,点赞3次,收藏31次。在几年前的时候,曾听过很多人说 Android 学习很简单,做个App就上手了,工作机会多,毕业后也比较容易找工作。这种观点可能是很多Android开发者最开始入行的原因之一。在工作初期,工作主要是按照业务需求实现App页面的功能,按照设计师的设计稿实现页面的效果。在实现的过程中,总是会被提如下的需求:这个字能不能大点或者醒目点儿?感觉颜色和设计稿有差别,能不能再调调?怎么老是崩溃啊,行不行啊?…所以,工作过一、两年后你会发现,自己每天重复工作内容就是将找各种各样的组件、框架,拖拖拽拽,改_android现状

php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法_php 判断是否月最后一天取上月月份-程序员宅基地

文章浏览阅读274次。在做查询过程中,例如要实现查上个月从第一天到最后一天的佣金(提成),那我们在程序实现过程中就要让程序在上个月的范围内查询,第一天是比较好办,但最后一天就不定,要去写段函数进行月份及年份判断来得出上个月共有多少天.那就比麻烦,还有获取当前月份,当前年份等常规日期获取函数,以下代码都是经过本公司工程师测试后的正确代码,可以放心使用. 1.获取上个月第一天及最后一天. echo date('_php 判断是否月最后一天取上月月份

随便推点

微信小程序api视频课程-定时器-setTimeout的使用_微信小程序 settimeout 向上层传值-程序员宅基地

文章浏览阅读1.1k次。JS代码 /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { setTimeout( function(){ wx.showToast({ title: '黄菊华老师', }) },2000 ) },说明该代码只执行一次..._微信小程序 settimeout 向上层传值

uploadify2.1.4如何能使按钮显示中文-程序员宅基地

文章浏览阅读48次。uploadify2.1.4如何能使按钮显示中文博客分类:uploadify网上关于这段话的搜索恐怕是太多了。方法多也试过了不知怎么,反正不行。最终自己想办法给解决了。当然首先还是要有fla源码。直接去管网就可以下载。[url]http://www.uploadify.com/wp-content/uploads/uploadify-v2.1.4...

戴尔服务器安装VMware ESXI6.7.0教程(U盘安装)_vmware-vcsa-all-6.7.0-8169922.iso-程序员宅基地

文章浏览阅读9.6k次,点赞5次,收藏36次。戴尔服务器安装VMware ESXI6.7.0教程(U盘安装)一、前期准备1、下载镜像下载esxi6.7镜像:VMware-VMvisor-Installer-6.7.0-8169922.x86_64.iso这里推荐到戴尔官网下载,Baidu搜索“戴尔驱动下载”,选择进入官网,根据提示输入服务器型号搜索适用于该型号服务器的所有驱动下一步选择具体类型的驱动选择一项下载即可待下载完成后打开软碟通(UItraISO),在“文件”选项中打开刚才下载好的镜像文件然后选择启动_vmware-vcsa-all-6.7.0-8169922.iso

百度语音技术永久免费的语音自动转字幕介绍 -程序员宅基地

文章浏览阅读2k次。百度语音技术永久免费的语音自动转字幕介绍基于百度语音技术,识别率97%无时长限制,无文件大小限制永久免费,简单,易用,速度快支持中文,英文,粤语永久免费的语音转字幕网站: http://thinktothings.com视频介绍 https://www.bilibili.com/video/av42750807 ...

Dyninst学习笔记-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏9次。Instrumentation是一种直接修改程序二进制文件的方法。其可以用于程序的调试,优化,安全等等。对这个词一般的翻译是“插桩”,但这更多使用于软件测试领域。【找一些相关的例子】Dyninst可以动态或静态的修改程序的二进制代码。动态修改是在目标进程运行时插入代码(dynamic binary instrumentation)。静态修改则是直接向二进制文件插入代码(static b_dyninst

在服务器上部署asp网站,部署asp网站到云服务器-程序员宅基地

文章浏览阅读2.9k次。部署asp网站到云服务器 内容精选换一换通常情况下,需要结合客户的实际业务环境和具体需求进行业务改造评估,建议您进行服务咨询。这里仅描述一些通用的策略供您参考,主要分如下几方面进行考虑:业务迁移不管您的业务是否已经上线华为云,业务迁移的策略是一致的。建议您将时延敏感型,有快速批量就近部署需求的业务迁移至IEC;保留数据量大,且需要长期稳定运行的业务在中心云上。迁移方法请参见如何计算隔离独享计算资源..._nas asp网站