传送门:点我

题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1

思路:暴力BFS,看到n和m都不大,直接把每个“#”都存下来,每次加入2个点进广搜搜能否烧完,然后更新ans即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
void closeio(){
cin.tie();
ios::sync_with_stdio();
}
struct note{
int x,y,step;
}p,pos;
char ma[][];
int vis[][];
int n,m;
int go[][] = {{,},{-,},{,-},{,}};//四个方向
void init(){
memset(vis,,sizeof(vis));
}
bool check(int x,int y){
if(x < || x >= n || y < || y >= m || vis[x][y] == ){
return false;
}
return true;
}//判断是否越界和已经访问过
bool judge(){
for(int i = ; i < n ; i ++){
for(int j = ; j < m ; j ++){
if(ma[i][j] == '#' && !vis[i][j]){
return false;
}
}
}
return true;
}//判断是否满足全都着火了
int bfs(int x,int y,int a,int b){
int sum = ;
queue<note>q;
q.push((note){x,y,});
vis[x][y] = ;
q.push((note){a,b,});
vis[a][b] = ;
//把两个点都加入队列
while(!q.empty()){
pos = q.front();
sum = pos.step;//队列中最后一个点的step就是需要的步数
q.pop();
for(int i = ; i < ;i ++){
int dx = pos.x + go[i][];
int dy = pos.y + go[i][];
if(check(dx,dy) && ma[dx][dy] == '#'){
p.x = dx;
p.y = dy;
p.step = pos.step + ;
q.push(p);
vis[dx][dy] = ;
}
}
}
return sum;
}
int main()
{
int t,cas = ;
for(scanf("%d",&t);t--;){
vector<note>v;
scanf("%d %d",&n,&m);
for(int i = ; i < n ; i++){
scanf("%s",ma[i]);
for(int j = ; j < m ; j ++){
if(ma[i][j] == '#'){
p.x = i;p.y = j;p.step = ;
v.push_back(p);
}
}
}
int ans = ;
for(int i = ; i < v.size() ; i++){
for(int j = i ; j < v.size() ; j ++){
init();
int a = bfs(v[i].x,v[i].y,v[j].x,v[j].y);
if(judge()){
ans = min(a,ans);
}//如果都着火了,那么ans值取ans和这次广搜中小的那个
}
}
printf("Case %d: %d\n",cas++,ans == ?-:ans);
}
return ;
}

FZU2150 :Fire Game (双起点BFS)的更多相关文章

  1. FZU 2150 Fire Game(双起点)【BFS】

    <题目链接> 题目大意: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一 ...

  2. HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

  4. FZU2150 Fire Game

    题目: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一.求烧完所有的草需要的最少时间 ...

  5. FZU - 2150 Fire Game bfs+双起点枚举

    题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...

  6. FZU2150 Fire Game —— BFS

    题目链接:https://vjudge.net/problem/FZU-2150 Problem 2150 Fire Game Accept: 2702    Submit: 9240 Time Li ...

  7. FZU2150 Fire Game BFS搜索

    题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的 ...

  8. Fire! (双bfs+预处理)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

随机推荐

  1. ARP 欺骗攻击与防御

    <一> ARP攻防 理论 和 工具 工具: 01: Wireshark ( 抓包软件 )   02: P2P终结者 ( ARP欺骗工具 )   03: cain & abel  ( ...

  2. leetcode1014

    这道题暴力算法,会超时: class Solution(object): def maxScoreSightseeingPair(self, A: 'List[int]') -> int: n ...

  3. leetcode1011

    class Solution: def shipWithinDays(self, weights: 'List[int]', D: int) -> int: left = max(weights ...

  4. [转]USB之Part 4 - Protocol

    原地址http://www.usbmadesimple.co.uk/ums_4.htm Controlling a Device Before we go into detail, we need t ...

  5. django 使用mysql 数据库

    在 django 创建项目中 默认使用的是  splite3 数据库,不是mysql 数据库,要使用mysql ,要做一些配置: 在 settings.py 中修改如下: DATABASES = { ...

  6. Redis 配置主从

  7. CSS多行文字垂直居中的两种方法

    之前写过一篇关于:CSS左右居中对齐的文章,里面提到的两种方法其实也可以引申为垂直居中对齐.写这篇文章是因为要兼容IE6.IE7的问题,我们都知道一行文字时可以通过line-height来设置垂直居中 ...

  8. C# 方法参数传递方式 关键字(in、out、ref)

    in:  值传递,默认传递方式: ref:地址/引用传递,调用时该参数必需已经初始化: out:地址/引用传递,调用时该参数不需要先初始化(被调用方负责该参数的初始化). 注1: in  关键字用于向 ...

  9. Others-阿里专家强琦:流式计算的系统设计和实现

    阿里专家强琦:流式计算的系统设计和实现 更多深度文章,请关注云计算频道:https://yq.aliyun.com/cloud 阿里云数据事业部强琦为大家带来题为“流式计算的系统设计与实现”的演讲,本 ...

  10. Who am I?

    陈治宏. 一只想做软件开发,但还在machine learning领域挣扎的计算机汪.