FZU - 2150-Fire Game BFS-枚举

Fire Game 

题意:

两个小朋友可以任选一块草地点火,草地可以不同,也可以相同,问最少的烧光草地的时间。

思路:

一开始看到这个以为是联通块计数,没想到这道题通过枚举两个起始点作为队列的初始点,每次跑一边bfs即可。

 

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const int mod = 1e9+7;

const double PI=acos(-1.0);

// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
            const int maxn = 20;
            int n,m;
            string  mp[maxn];
            int dp[maxn][maxn];
            
            int nt[4][4] = {
                {1,0},{0,1},{-1,0},{0,-1},
            };
            struct node
            {
                int x,y,step;                
            }q[maxn*maxn];
            int mx,tot;
            void bfs(node a,node b){
                mx = 0;
                for(int i=0; i<maxn; i++){
                    for(int j=0; j<maxn; j++)dp[i][j] = inf;
                }
                queue<node>que;
                que.push(a);
                if(a.x!=b.x||a.y!=b.y)que.push(b);
                dp[a.x][a.y] = 0;
                dp[b.x][b.y] = 0;
                while(!que.empty()){
                    int x = que.front().x,y = que.front().y,s = que.front().step;
                    que.pop();
                    for(int i=0 ; i<4; i++){
                        int nx = x + nt[i][0],ny = y + nt[i][1];
                        if(nx < 0||nx >=n || ny <0||ny>=m)continue;
                        if(mp[nx][ny]=='.')continue;
                        if(dp[nx][ny] > s + 1){
                                dp[nx][ny] = s + 1;
                                mx = max(mx,s+1);
                                node tmp={nx,ny,s+1};
                                que.push(tmp);

                        }
                    }
                }
            }
            void solve(){
                cin>>n>>m;
                
                for(int i=0; i<n; i++){
                    cin>>mp[i];
                }
                int cnt = 0,ans = inf;
                for(int i=0; i<n ; i++){
                    for(int j=0; j<m; j++){
                      if(mp[i][j]=='#'){
                            q[++cnt].x = i;
                            q[cnt].y = j;
                            q[cnt].step = 0;
                      }
                    }
                }
                
                for(int i=1; i<=cnt; i++)
                {

                    for(int j=i; j<=cnt; j++){
                        bfs(q[i],q[j]);
                        int flag = 1;
                        for(int x = 0; x < n; x++){
                            for(int y = 0; y<m; y++){
                                if(dp[x][y] >=inf && mp[x][y] == '#')flag= 0;
                            }
                        }
                        if(flag)ans = min(ans, mx);
                        
                    }
                }
                if(ans < inf)cout<<ans<<endl;
                else cout<<-1<<endl;
            }
int main(){ 
            int T;  cin>>T;
            for(int t=1;t<=T;t++){
                cout<<"Case "<<t<<": ";
                solve();
            }
            return 0;
}
FZU-2150

 

posted @ 2018-08-19 11:23  ckxkexing  阅读(190)  评论(0编辑  收藏  举报