题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150

题目大意:小明小红要在一块网格状土地上玩火。土地上有的地方是草块,有的地方是空地。草地会被相邻格子的火引燃,土地不会。小明小红想要让所有有草的格子都燃烧,并且它俩各自点着了一块草地(可能一样)。问是否能够达到他们的目的?如果能,最少需要多长时间才能烧完全部的草地。

解题思路:显然有三个以上互相不连通的草地块那么就无法达到目的了,如果有两个,那必须一人点一个草地块,如果有一个就任选两个格子点燃,即:

DFS求连通块个数,若两个-对于每个块,枚举每个点,BFS求最少时间然后求两个最小时间的最大值;若一个-枚举这个快内任意两个点(可以一样),然后两个点同时为起点bfs即可,最后求最小值即可。

代码:

  1 const int inf = 0x3f3f3f3f;
  2 const int iadd[] = {0, 1, 0, -1}, jadd[] = {1, 0, -1, 0};
  3 const int maxn = 15;
  4 char maze[maxn][maxn];
  5 int n, m;
  6 int bt[maxn][maxn], cnt = 0;
  7 int anss[3];
  8 short vis[maxn][maxn];
  9
 10 void dfs(int x, int y){
 11     vis[x][y] = 1; bt[x][y] = cnt + 1;
 12     for(int i = 0; i < 4; i++){
 13         int vi = x, vj = y;
 14         vi += iadd[i]; vj += jadd[i];
 15         if(vi < 0 || vj < 0 || vi >= n || vj >= m) continue;
 16         if(maze[vi][vj] == '.' || vis[vi][vj]) continue;
 17         dfs(vi, vj);
 18     }
 19 }
 20
 21 struct node{
 22     int i, j, t;
 23 };
 24 int bfs(int i, int j){
 25     queue<node> q;
 26     memset(vis, 0, sizeof(vis));
 27     vis[i][j] = 1;
 28     node u; u.i = i; u.j = j; u.t = 0;
 29     q.push(u);
 30     int ans = 0;
 31     while(!q.empty()){
 32         u = q.front(); q.pop();
 33         ans = u.t; u.t++;
 34         for(int i = 0; i < 4; i++){
 35             node v = u;
 36             v.i += iadd[i]; v.j += jadd[i];
 37             if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;
 38             if(maze[v.i][v.j] == '.' || vis[v.i][v.j]) continue;
 39             vis[v.i][v.j] = 1;
 40             q.push(v);
 41         }
 42     }
 43     anss[bt[i][j]] = min(anss[bt[i][j]], ans);
 44 }
 45
 46 int bfs(int i1, int j1, int i2, int j2){
 47     memset(vis, 0, sizeof(vis));
 48     queue<node> q;
 49     vis[i1][j1] = vis[i2][j2] = 1;
 50     node u1; u1.i = i1; u1.j = j1; u1.t = 0;
 51     node u2; u2.i = i2; u2.j = j2; u2.t = 0;
 52     q.push(u1); q.push(u2);
 53     int ans = 0;
 54     while(!q.empty()){
 55         node u = q.front(); q.pop();
 56         ans = max(ans, u.t);
 57         u.t++;
 58         for(int i = 0; i < 4; i++){
 59             node v = u;
 60             v.i += iadd[i]; v.j += jadd[i];
 61             if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;
 62             if(maze[v.i][v.j] == '.' || vis[v.i][v.j]) continue;
 63             q.push(v);
 64             vis[v.i][v.j] = 1;
 65         }
 66     }
 67     return ans;
 68 }
 69
 70 int main(){
 71     int T;
 72     scanf("%d", &T);
 73     for(int t = 1; t <= T; t++){
 74         memset(bt, 0, sizeof(bt));
 75         cnt = 0;
 76         scanf("%d %d", &n, &m);
 77         for(int i = 0; i < n; i++){
 78             scanf("%s", maze[i]);
 79         }
 80         printf("Case %d: ", t);
 81         memset(vis, 0, sizeof(vis));
 82         for(int i = 0; i < n; i++){
 83             for(int j = 0; j < m; j++){
 84                 if(maze[i][j] == '#' && !vis[i][j]){
 85                     dfs(i, j);
 86                     cnt++;
 87                 }
 88             }
 89         }
 90         if(cnt == 0) {
 91             puts("0");
 92             continue;
 93         }
 94         else if(cnt >= 3){
 95             puts("-1");
 96             continue;
 97         }
 98         else if(cnt == 1){
 99             vector<PII> va;
100             for(int i = 0; i < n; i++)
101                 for(int j = 0; j < m; j++)
102                     if(maze[i][j] == '#')
103                         va.push_back(PII(i, j));
104             int ans = inf;
105             for(int i = 0; i < va.size(); i++){
106                 for(int j = i; j < va.size(); j++){
107                     int tmp = bfs(va[i].first, va[i].second, va[j].first, va[j].second);
108                     ans = min(tmp, ans);
109                 }
110             }
111             printf("%d\n", ans);
112             continue;
113         }
114         memset(vis, 0, sizeof(vis));
115         memset(anss, 0x3f, sizeof(anss));
116         for(int i = 0; i < n; i++){
117             for(int j = 0; j < m; j++){
118                 if(maze[i][j] == '#') bfs(i, j);
119             }
120         }
121         if(anss[1] >= inf) anss[1] = -1;
122         if(anss[2] >= inf) anss[2] = -1;
123         printf("%d\n", max(anss[2], anss[1]));
124     }
125 }

题目:

Problem 2150 Fire Game

Accept: 2635    Submit: 9094
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

FZUOJ 2150 Fire Game相关推荐

  1. fzu 2150 Fire Game 【身手BFS】

    称号:fzu 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧全部 ...

  2. FZU 2150 Fire Game

    点击打开链接 Problem 2150 Fire Game Accept: 237    Submit: 808 Time Limit: 1000 mSec    Memory Limit : 327 ...

  3. FZU 2150 Fire Game bfs

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103921#problem/I bfs水题.好像还做过一次了.思路题意都见代码吧 ...

  4. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  5. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  6. [kuangbin带你飞]专题1

    专题一 简单搜索 POJ 1321 棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...

  7. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  8. [kuangbin带你飞]专题一 做题顺序与题解 【简单搜索】

    随便说点: 博主正在刷kuangbin专题的题目,初学者,没接触过什么算法,刷题的初衷是备战蓝桥杯,后来发现了算法资料大多是针对acm的,挑选kuangbin专题入门也是如此,毕竟这样分类看起来比较有 ...

  9. 老鱼的-kuangbin专题题解

    kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...

最新文章

  1. golang日志服务器_日志系统 | log/syslog (log) – Go 中文开发手册 - Break易站
  2. JavaScript 同源策略
  3. 如何复制一个含有随机指针节点的链表
  4. 学生时代,我做过最有价值的事是什么?
  5. svm 图片加标签_基于半监督的SVM多标签图数据分类算法研究
  6. 如何建立一个利于SEO的网站
  7. js高级编号笔记[新]-访问文档对象
  8. C#中this关键字-调用本类成员
  9. java calendar获取年_Java Calendar获取年、月、日、时间,设置年、月、日
  10. 拓端tecdat|R语言ARIMA、GARCH 和 VAR模型估计、预测ts 和 xts格式时间序列
  11. windows取证之镜像取证仿真步骤
  12. mysql数据库编程第六章试题_2016年计算机二级MySQL数据库试题及答案
  13. 《免费:商业的未来》“免费经济学”读书笔记----字节跳动案例分析
  14. 一维 cnn matlab,1D-CNN 一维信号的深度学习算法和例子包括CNN - 下载 - 搜珍网
  15. PR2021安装包下载
  16. python课程计算bmi_Python第十一课 计算体脂率1.0
  17. ubuntu20 卸载显卡驱动失败_Ubuntu20.04显卡驱动安装
  18. 如何破解终端算力困局?PRCV这篇论文让机器人“算有余力”
  19. 前端请求报错Provisional headers are shown接口请求失败
  20. 性格类型之ISFP艺术家型——有爱心的艺术工作者

热门文章

  1. c语言编写一个字母金字塔,【强迫症满足向】字母金字塔: C语言实现
  2. 黑马程序员 static的类可以用类名直接调用,无需创建对象
  3. Android POI 百度地图——周边检索
  4. 点击网页分享按钮,触发微信分享功能
  5. rosbag中--clock的使用
  6. MTK WIFImac地址
  7. Python快速刷题网站——牛客网 数据分析篇(十一)
  8. etl oracle 培训,ETL基础及常用技术培训备课讲稿.ppt
  9. Jfinal 框架搭建
  10. 争对让望对思野葛对山栀注解_笠翁对韵带拼音上卷·四支原文解释翻译