枚举两点,然后同步BFS,看代码吧,很容易懂的。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define Mod 1000000007
using namespace std; struct Point
{
int x,y;
int step;
}S;
char mp[][];
int vis[][];
int n,m;
int lef;
int dx[] = {,,,-};
int dy[] = {,-,,};
queue<Point> que; bool OK(int nx,int ny)
{
if(nx < n && nx >= && ny < m && ny >= )
return true;
return false;
} int BFS()
{
int maxi = ;
while(!que.empty()) //同步搜索
{
int SIZE = que.size();
while(SIZE--)
{
Point tmp = que.front();
que.pop();
int nx = tmp.x;
int ny = tmp.y;
int step = tmp.step;
maxi = max(maxi,step);
Point now;
for(int k=;k<;k++)
{
int kx = nx + dx[k];
int ky = ny + dy[k];
if(!OK(kx,ky) || vis[kx][ky])
continue;
if(mp[kx][ky] == '#')
{
vis[kx][ky] = ;
now.x = kx;
now.y = ky;
now.step = step+;
que.push(now);
lef--;
}
}
}
}
if(lef == )
return maxi;
else
return -;
} int main()
{
int t,cs = ,i,j,k,h;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int cnt = ;
for(i=;i<n;i++)
{
scanf("%s",mp[i]);
for(j=;j<m;j++)
if(mp[i][j] == '#')
cnt++;
}
int flag = ;
int tag = Mod;
for(i=;i<n;i++) //第一个点
{
for(j=;j<m;j++)
{
if(mp[i][j] == '#')
{
for(k=i;k<n;k++) //第二个点
{
for(h=(k==i?j:);h<m;h++)
{
if(mp[k][h] == '#')
{
memset(vis,,sizeof(vis));
S.x = i,S.y = j,S.step = ;
que.push(S);
lef = cnt-;
vis[i][j] = ;
if(!(i==k&&j==h))
{
S.x = k,S.y = h,S.step = ;
que.push(S);
vis[k][h] = ;
lef--;
}
int res = BFS();
if(res != -)
{
flag = ;
tag = min(tag,res);
}
}
}
}
}
}
}
if(flag)
printf("Case %d: %d\n",cs++,tag);
else
printf("Case %d: -1\n",cs++);
}
return ;
}

FZU 2150 Fire Game --两点同步搜索的更多相关文章

  1. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  2. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  4. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  5. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  6. FZU 2150 Fire Game 广度优先搜索,暴力 难度:0

    http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...

  7. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  8. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  9. (简单) FZU 2150 Fire Game ,Floyd。

    Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...

随机推荐

  1. DShow实现一个avi视频的播放(含有个人解释和注释)

    此项目为win32下的控制台C++代码(别忘记配置DShow库) // movie_test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...

  2. Vue入门演示

    工作中用了很久vue,但是都是我们这边前端经理封装好的组件,想要看到底部的原理还要从层层代码里面剥离出来,逻辑太复杂,还不如自己一点点整理一下,一步一步走下去. github地址:https://gi ...

  3. SET UPDATE TASK LOCAL

    SET Effect Switches on the local update task. This means that when you specify CALL FUNCTION ... IN ...

  4. C# DevExpress 的gridControl或gridView数据导出失败解决方法

    来自:http://blog.csdn.net/lybwwp/article/details/8049464 谢谢 在使用DevExpress 的GridPanel控件的时候出现了一个莫名其妙的现象, ...

  5. iOS多线程邂逅

    .线程之间的通信 //有一个特别耗时的操作,比如说网络请求,开启子线程去请求网络,我们一般是要在主线程更新UI,如何从子线程跳转到主线程? #import "ViewController.h ...

  6. Swift的7大误区

    Swift正在完成一个惊人的壮举,它正在改变我们在苹果设备上编程的方式,引入了很多现代范例,例如:函数式编程和相比于OC这种纯面向对象语言更丰富的类型检查. Swift语言希望通过采用安全的编程模式去 ...

  7. [docker] 管理docker容器中的数据

    之前我们介绍了Docker的基本概念(前面的没翻译...),了解了如何使用Docker镜像进行工作,并且学习了网 络和容器之间的链接.这一节我们将讨论如何管理容器中及容器之间的数据. 我们将查看下面两 ...

  8. Oracle 数据库二 基本查询

    查询当前用户:show user 查看当前用户下的表:select *from tab; 设置行宽: show linesize;(查看行宽)     set linesize 120;(设置行宽) ...

  9. Servlet/JSP-01 Servlet及其生命周期

    一.起步 1.新建一个类继承Servlet接口 public class HelloServlet implements Servlet { @Override public void destroy ...

  10. MySQL单机多实例安装并配置主从复制

    单机多实例据说可以最大程度提高硬件使用,谁知道呢,但是以前的公司喜欢这样搞,我最近也在学习复制什么的,电脑搞不起两台虚拟机,刚好单机多实例可以解救我.下面就说说步骤. 承上文http://www.cn ...