Fire Game FZU - 2150

原题链接

考察:bfs

思路:

        vector存储所有是草的点,一次bfs枚举两个点作为起点.求遍历所有草的最少时间.

注意:存在只有一株草的情况

菜到只会做水题...蓝桥杯没得了5555

 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 const int N = 15,INF = 0x3f3f3f3f;
 7 typedef pair<int,int> PII;
 8 vector<PII> v;
 9 int n,m,dist[N][N];
10 char mp[N][N];
11 int xx[4] = {-1,1,0,0},yy[4] = {0,0,-1,1};
12 int bfs()
13 {
14     queue<PII> q;
15     int res = INF;
16     for(int i=0;i<v.size();i++)
17       for(int j=i;j<v.size();j++)
18       {
19           q.push(v[i]); q.push(v[j]);
20           memset(dist,0x3f,sizeof dist);
21           dist[v[i].first][v[i].second] = 0;
22           dist[v[j].first][v[j].second] = 0;
23           while(q.size())
24           {
25               PII it = q.front();
26               q.pop();
27               int x = it.first,y = it.second;
28               for(int i=0;i<4;i++)
29               {
30                   int dx = x+xx[i],dy = y+yy[i];
31                 if(dx<=n&&dx>=1&&dy<=m&&dy>=1&&mp[dx][dy]=='#'&&dist[dx][dy]==INF)
32                 {
33                     dist[dx][dy] = dist[x][y]+1;
34                     PII it; it.first = dx,it.second = dy;
35                     q.push(it);
36                 } 
37             }
38         }
39         int ans = 0;
40         for(int k=0;k<v.size();k++)
41             ans = max(ans,dist[v[k].first][v[k].second]);
42         res = min(ans,res); 
43       }
44     if(res==INF) return -1;
45     return res;
46 }
47 int main()
48 {
49     int T,kcase = 0;
50     scanf("%d",&T);
51     while(T--)
52     {
53         scanf("%d%d",&n,&m);
54         for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
55         v.clear();
56         for(int i=1;i<=n;i++)
57           for(int j=1;j<=m;j++)
58             if(mp[i][j]=='#')
59             {
60                 PII it; it.first = i,it.second = j;
61                 v.push_back(it);
62             }
63         int res = bfs();
64         printf("Case %d: %d\n",++kcase,res);
65     }
66     return 0;
67 }

 

posted @ 2021-04-20 13:59  acmloser  阅读(33)  评论(0编辑  收藏  举报