题目链接:https://vjudge.net/problem/FZU-2150

题意:’ . '代表火无法烧着的地方,‘ # ’表示草,火可以烧着。选择任意两个‘ # ’(可以两个都选同一个 ‘ # ’),火会蔓延,每过1个时间消耗,向四周蔓延。问:能不能把草全部烧完,可以的话得出最短时间,否则输出 -1。

思路:bfs,枚举所有点火情况就OK了,直接看代码吧。


  1. #include <iostream>
  2. #include <cstring>
  3. #include<vector>
  4. #include<string>
  5. #include <cmath>
  6. #include <map>
  7. #include <queue>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. #define inf (1LL << 31) - 1
  12. #define rep(i,j,k) for(int i = (j); i <= (k); i++)
  13. #define rep__(i,j,k) for(int i = (j); i < (k); i++)
  14. #define per(i,j,k) for(int i = (j); i >= (k); i--)
  15. #define per__(i,j,k) for(int i = (j); i > (k); i--)
  16.  
  17. const int N = ;
  18. int mv_x[] = { , , , - };
  19. int mv_y[] = { , -, , };
  20. int x[]; //草的x
  21. int y[]; //草的y
  22. int l; //几颗草
  23. char mp[N][N]; //原始地图
  24. char cp_mp[N][N]; //原始地图副本,用于bfs
  25. bool vis[N][N];
  26. int ans; //答案
  27. int n, m;
  28.  
  29. struct node{
  30. int x, y, c;
  31. node(){}
  32. node(int o, int oo, int ooo){
  33. x = o;
  34. y = oo;
  35. c = ooo;
  36. }
  37. };
  38.  
  39. inline void input(){
  40.  
  41. l = ;
  42. rep(i, , n) rep(j, , m){
  43. cin >> mp[i][j];
  44.  
  45. //记录下所有草
  46. if (mp[i][j] == '#'){
  47. x[l] = i;
  48. y[l++] = j;
  49. }
  50. }
  51. }
  52.  
  53. //每种情况前,初始化函数
  54. inline void init_vis_And_cp_mp(){
  55. memset(vis, , sizeof(vis));
  56. memcpy(cp_mp, mp, sizeof(mp));
  57. }
  58.  
  59. //边界
  60. inline bool check(int x, int y){
  61. return x >= && x <= n && y >= && y <= m;
  62. }
  63.  
  64. //检查草是否都烧完
  65. bool all_Fired(){
  66. rep(i, , n) rep(j, , m){
  67. if (cp_mp[i][j] == '#' && !vis[i][j]) return false;
  68. }
  69.  
  70. return true;
  71. }
  72.  
  73. void bfs(int x1, int y1, int x2, int y2){
  74.  
  75. queue<node> que;
  76.  
  77. //标记两个点火源
  78. vis[x1][y1] = true;
  79. vis[x2][y2] = true;
  80.  
  81. //放入队列
  82. node a1(x1, y1, );
  83. node a2(x2, y2, );
  84. que.push(a1);
  85. que.push(a2);
  86.  
  87. int tmp_ans = ;
  88. while (!que.empty()){
  89.  
  90. node tmp = que.front();
  91. que.pop();
  92.  
  93. tmp_ans = max(tmp_ans, tmp.c); //这里是得出能烧到的草的最后时间
  94.  
  95. rep__(p, , ){
  96.  
  97. int dx = tmp.x + mv_x[p];
  98. int dy = tmp.y + mv_y[p];
  99.  
  100. //没越界,没被访问过,是草
  101. if (check(dx, dy) && !vis[dx][dy] && cp_mp[dx][dy] == '#'){
  102. vis[dx][dy] = true;
  103. node k(dx, dy, tmp.c + );
  104. que.push(k);
  105. }
  106. }
  107. }
  108.  
  109. //全部烧完才能算一个答案
  110. if (all_Fired()) ans = min(ans, tmp_ans);
  111. }
  112.  
  113. int main(){
  114.  
  115. ios::sync_with_stdio(false);
  116. cin.tie();
  117.  
  118. int T;
  119. cin >> T;
  120.  
  121. rep(o, , T){
  122.  
  123. cin >> n >> m;
  124. input();
  125.  
  126. ans = inf; //
  127.  
  128. //枚举点火情况
  129. rep__(i, , l ) rep__(j, i, l){
  130. init_vis_And_cp_mp(); //每种情况前,初始化函数
  131. bfs(x[i], y[i], x[j], y[j]); //两个点火源
  132. }
  133. // cout << "Case 1: 1" << endl;
  134. cout << "Case " << o << ": " << (ans == inf ? - : ans) << endl;
  135. }
  136.  
  137. return ;
  138. }

kuangbin专题 专题一 简单搜索 Fire Game FZU - 2150的更多相关文章

  1. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  2. kuangbin专题 专题一 简单搜索 Fire! UVA - 11624

    题目链接:https://vjudge.net/problem/UVA-11624 题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫 ...

  3. kuangbin专题总结一 简单搜索

    A - 棋盘问题:在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有 ...

  4. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  5. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  6. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  7. 简单搜索 kuangbin C D

    C - Catch That Cow POJ - 3278 我心态崩了,现在来回顾很早之前写的简单搜索,好难啊,我怎么写不出来. 我开始把这个写成了dfs,还写搓了... 慢慢来吧. 这个题目很明显是 ...

  8. ElasticSearch 5学习(4)——简单搜索笔记

    空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...

  9. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

随机推荐

  1. MVC 用基架创建Controller,通过数据库初始化器生成并播种数据库

    1 创建MVC应用程序 2 在Model里面创建实体类 using System; using System.Collections.Generic; using System.Linq; using ...

  2. AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享

    原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net       杨洋    wpfui.com      ...

  3. 在WPF中减少逻辑与UI元素的耦合

    原文:在WPF中减少逻辑与UI元素的耦合             在WPF中减少逻辑与UI元素的耦合 周银辉 1,    避免在逻辑中引用界面元素,别把后台数据强加给UI  一个糟糕的案例 比如说主界 ...

  4. delphi中使用词霸2005的动态库XdictGrb.dll实现屏幕取词

    近日来,在网上发现关于屏幕取词技术的捷径,搜索很长时间,发现实现方式以VB出现的居多,但是通过Delphi来实现的却好象没有看到,自己参考着VB的相关代码琢磨了一下通过delphi来实现的方式. 其实 ...

  5. Win8Metro(C#)数字图像处理--2.14Prewitt 边缘检测

    原文:Win8Metro(C#)数字图像处理--2.14Prewitt 边缘检测  [函数名称] 图像Prewitt边缘检测函数PrewittEdgeProcess(WriteableBitmap ...

  6. NET C#创建WINDOWS系统用户

    原文:NET C#创建WINDOWS系统用户   /前提是当前用户有相应的权限 /WinNT用户管理 using System; using System.DirectoryServices;  na ...

  7. DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS & PUBLISHING THEM TO THE MICROSOFT STORE

    原文: DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS & PUBLISHING THEM ...

  8. ML:吴恩达 机器学习 课程笔记(Week9~10)

    Anomaly Detection Recommender Systems Large Scale Machine Learning

  9. Tensorflow-常见报错解决方案

    1. AttributeError: 'module' object has no attribute 'SummaryWriter' tf.train.SummaryWriter 改为:tf.sum ...

  10. 如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

    <span style="font-size:14px;">一直想找如何解析JSON数据的说,今天终于找到有人发帖子了.之前有人说用superobject,Tlkjso ...