FZU2150 :Fire Game (双起点BFS)
传送门:点我
题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-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)的更多相关文章
- FZU 2150 Fire Game(双起点)【BFS】
<题目链接> 题目大意: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一 ...
- HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- FZU2150 Fire Game
题目: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一.求烧完所有的草需要的最少时间 ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- FZU2150 Fire Game —— BFS
题目链接:https://vjudge.net/problem/FZU-2150 Problem 2150 Fire Game Accept: 2702 Submit: 9240 Time Li ...
- FZU2150 Fire Game BFS搜索
题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的 ...
- Fire! (双bfs+预处理)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
随机推荐
- C# 类库调试 启动外部程序无法调试
无法调试进程 test.exe [17936] 中的某些代码.请参阅下面的状态信息. IntelliTrace 代码失败(0x80131534). Managed (v4.6.v4.5.v4.0 ...
- How to start a VirtualBox VM headless in Windows 10
If you wanted to start a VirtualBox VM headless (no UI) in the past, you needed additional tools. I ...
- upupw
https://sourceforge.net/projects/upupw/files/ANK/
- LeetCode OJ 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 如何创建Servlet
//Servlet的生命周期:从Servlet被创建到Servlet被销毁的过程 //一次创建,到处服务 //一个Servlet只会有一个对象,服务所有的请求 /* * 1.实例化(使用构造方法创建对 ...
- Python 类的魔术方法
Python中类的魔术方法 在Python中以两个下划线开头的方法,__init__.__str__.__doc__.__new__等,被称为"魔术方法"(Magic method ...
- 新建gradle文件
按照新建自动步骤,建好文件后,在build-gradle 里面 写上: allprojects { group 'aaaa' version '1.0-SNAPSHOT' apply plugin: ...
- python字典dict的增、删、改、查操作
## python字典dict的增.删.改.查操作dict = {'age': 18, 'name': 'jin', 'sex': 'male', }#增# dict['heigh'] = 185 # ...
- gdufe1538-是男人就上100层-(三维bfs)
Problem Description: 桐老爷和UGO终于来到了名为“是男人就上一百层的塔”的下面,听说大祭司在第100层沉睡.为了题目需要,我把这个塔的层数随机打乱,层数的话大家就忘了前面的100 ...
- Winform 对话框
ColorDialog:显示可用颜色,以及用户可以自定义颜色的控件,以调色板对话框形式出现,可选择更改字体颜色 FolderBrowserDialog:显示一个对话框,提示用户选择文件夹 FontDi ...