FZU 2150 Fire Game --两点同步搜索
枚举两点,然后同步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 --两点同步搜索的更多相关文章
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- FZU 2150 Fire Game 广度优先搜索,暴力 难度:0
http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...
- 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) ...
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- (简单) FZU 2150 Fire Game ,Floyd。
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...
随机推荐
- Android系统兼容性问题(持续更新)
相信开发过一段Android的都被Android中的兼容性问题给折腾过,有时这确实很无奈,Android被不同的厂商改的七零八落的.本文主要总结下本人在实际的项目开发过程中所遇到的兼容性问题,以及最后 ...
- Python函数:一个简单的迭代
#!/usr/bin/env python # -*- coding: utf-8 -*- def fact(n): if n == 1 : return 1 return n * fact(n-1) ...
- 优秀程序设计的Kiss原则(keep it simple,stupid)
优秀程序设计的Kiss原则(keep it simple,stupid) 良好的编程原则与良好的设计工程原则密切相关.本文总结的这些设计原则,帮助开发者更有效率的编写代码,并帮助成为一名优秀的程序员. ...
- [转]MVC、MVP、MVVM
界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras 作者:戴嘉华 [字体:大 中 小] 前言 做客户端开发.前端开发 ...
- webpack学习(入门基础)
webpack的学习webpack是什么?1:加载模块(将JS.sass/less.图片等作为模块来处理使用) 2:进行打包 webpack的优势?1:webpack以commonJS(JS的规范)的 ...
- HTML 块元素
分为3类 1. 结构块 只能包含块级元素.它们包含结构含义,但没有语义含义,也就是,不能说明内容是什么,只能说明其组织方式. <ol> <ul> <dl> < ...
- Android Java类编写规范+优化建议
本文仅是我个人在实际开发中习惯的编写方式,当然这种方式也是来自很多官方的推荐,所以在一定程度上是可以被模仿套用的.本文将不定期更新~ 零.指导原则 优先保证可读性,不要过分追求代码艺术和效率 在可读性 ...
- git学习笔记1
很早以前就听说了git,今天就开始使用git,并做简单记录 在Linux上安装Git 首先,你可以试着输入git,看看系统有没有安装Git: $ git The program 'git' is cu ...
- nutch简介
1.什么是 nutch Nutch 是一个开源的. Java 实现的搜索引擎.它提供了我们运行自己的搜 索引擎所需的全部工具.2.研究 nutch 的原因(1) 透明度: nutch 是开放源代码的, ...
- Effective Java 17 Design and document for inheritance or else prohibit it
Principles The class must document its self-use of overridable methods. A class may have to provide ...