题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103921#problem/I

bfs水题。好像还做过一次了。思路题意都见代码吧~

  1 /*
  2 大意是给一个n*m的图。#表示长草,.表示空着,开始可以同时点燃两个格子里面的草,一秒钟蔓延到上下左右相邻的格子。
  3 空格不会有火。不能隔着格子蔓延。问是否能够让草的区域全部着火。如果能输出用的最少的时间。
  4 不知道思路从哪里来的。暴力。先判断有几个连通区域。如果>2,不可能。=2 两个区域分别找最短时间相加。=1.两重循环找最短时间、
  5 无脑。
  6 无脑卡壳了。不知道怎么实现。首先是计算有几个区域,广搜?可以。然后 == 2计算最短时间的时候,广搜一遍?然后=1 ,,依然是广搜.可行就是觉得麻烦。
  7 然后。小王sir果断告诉了我新的思路。两重循环任意两个草的位置为起点,搜索,从所有可能的结果中找最小值就可以了。我T_T。
  8 */
  9 
 10 #include <stdio.h>
 11 #include <string.h>
 12 #include <iostream>
 13 #include <queue>
 14 #define maxn 1000000
 15 using namespace std;
 16 
 17 struct Node {
 18     int x, y;
 19 }node[10000];
 20 
 21 queue<Node>que;
 22 int vis[20][20];
 23 int step[20][20];
 24 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
 25 int n, m;
 26 int cnt;
 27 bool viss[20][20];
 28 
 29 bool check(Node a) {
 30     int x = a.x, y = a.y;
 31     if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && viss[x][y]) {
 32         return true;
 33     }
 34     return false;
 35 }
 36 
 37 bool checkAll() {
 38     for (int i=0; i<cnt; ++i) {
 39         if (step[node[i].x][node[i].y] == maxn)
 40             return false;
 41     }
 42     return true;
 43 }
 44 
 45 
 46 int bfs(int ii, int jj) {
 47     while(!que.empty()) {
 48         que.pop();
 49     }
 50     memset(vis, 0, sizeof(vis));
 51 
 52     for (int i=0; i<n; ++i) {
 53         for (int j=0; j<m; ++j) {
 54             step[i][j] = maxn;
 55         }
 56     }
 57     que.push(node[ii]);
 58     que.push(node[jj]);
 59     vis[node[ii].x][node[ii].y] = 1;
 60     vis[node[jj].x][node[jj].y] = 1;
 61     step[node[ii].x][node[ii].y] = 0;
 62     step[node[jj].x][node[jj].y] = 0;
 63 
 64     while(!que.empty()) {
 65         Node now = que.front();
 66         que.pop();
 67         for (int i=0; i<4; ++i) {
 68             Node temp;
 69             temp.x = now.x + dir[i][0];
 70             temp.y = now.y + dir[i][1];
 71             if (check(temp)) {
 72                 que.push(temp);
 73                 vis[temp.x][temp.y] = 1;
 74                 step[temp.x][temp.y] = step[now.x][now.y] + 1;
 75             }
 76         }
 77         if (checkAll()) break;
 78     }
 79     if (!checkAll()) return maxn;
 80     int tempans = -1;
 81     for (int i=0; i<cnt; ++i) {
 82         tempans = max(step[node[i].x][node[i].y], tempans);
 83     }
 84     return tempans;
 85 }
 86 
 87 int main() {
 88     int t;
 89     char temp;
 90     cin >> t;
 91     int num = 0;
 92     while(t--) {
 93         Node now;
 94         cnt = 0;
 95         memset(viss, 0, sizeof(viss));
 96         cin >> n >> m;
 97         for (int i=0; i<n; ++i) {
 98             for (int j=0; j<m; ++j) {
 99                 cin >> temp;
100                 if (temp == '#') {
101                     now.x = i;
102                     now.y = j;
103                     viss[now.x][now.y] = 1;
104                     node[cnt++] = now;
105                 }
106             }
107         }
108 
109         int ans = maxn;
110         for (int i=0; i<cnt; ++i) {
111             for (int j=0; j<cnt; ++j) {
112                 ans = min(bfs(i, j), ans);
113             }
114         }
115 
116         cout << "Case " << ++num << ": ";
117         if (ans == maxn) {
118             cout << -1 << endl;
119         }
120         else {
121             cout << ans << endl;
122         }
123     }
124     return 0;
125 }
View Code

 

posted on 2016-01-25 22:50  小小八  阅读(217)  评论(0编辑  收藏  举报