4223. 点火游戏

给定一个 N行 M 列的方格矩阵。

其中一部分方格是草地,其余部分是空地。

草地能够被燃烧,空地不会。

当某个草地在 t 时刻被点燃时,其上下左右四个方向的相邻方格中的草地方格也会在 t+1 时刻被点燃。

注意,空地方格无论如何都不可能被点燃。

现在,你可以选择最多两个草地,将它们点燃。

请你计算,使得所有草地都被点燃所需花费的最少时间。

输入格式

第一行包含整数 T,表示共有 T 组测试数据。

每组数据第一行包含两个整数 N,M。

接下来 N 行,包含一个 N×M 的字符矩阵,# 表示草地,. 表示空地。

输出格式

每组数据输出一个结果,每个结果占一行。

结果表示为 Case x: y,其中 x 为组别编号(从 1 开始),y 点燃所有草地需要花费的最短时间。如果无法点燃所有草地或者所有方格都是空地则输出 −1。

数据范围

1≤T≤100,
1≤N,M≤10。

输入样例:

5
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
3 3
...
...
...

输出样例:

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
Case 5: -1

 

简单的bfs,就是细节比较多 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace std;
//#define int long long
typedef pair<int,int>PII;
constexpr int N=10000;
int n,m;
vector<PII>p;
char s[12][12];
int st[12][12];
int res[12][12];
int g[12][12];
int ans=-1;
int dx[4]={1,0,0,-1},dy[4]={0,1,-1,0};
void bfs(int t1,int t2,int t3,int t4){
    ans=0;
    queue<PII>q;
    q.push({t1,t2});
    q.push({t3,t4});
    res[t1][t2]=0,res[t3][t4]=0;
    while(q.size()){
        auto t=q.front();
        //cout<<t.first<<" "<<t.second<<endl;
        q.pop();
        for(int i=0;i<4;i++){
            int x=t.first+dx[i],y=t.second+dy[i];
            if(x>=1&&x<=n&&y>=1&&y<=m&&!st[x][y]&&s[x][y]=='#'){
                q.push({x,y});
                st[x][y]=1;
                res[x][y]=res[t.first][t.second]+1;
                ans= max(ans,res[x][y]);
            }
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin>>t;
    int k=0;
    while(t--) {
        p.clear();
        memset(g,0, sizeof g);
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> s[i][j];
                if (s[i][j] == '#') {
                    p.push_back({i, j});
                    g[i][j] = 1;
                }
            }
        }
        if (p.empty()) cout <<"Case "<< ++k << ": " << -1 << endl;
        else if(p.size()==1) cout <<"Case "<< ++k << ": " << 0 << endl;
        else {
            int ww=1e7,tp=0;
            for (int i = 0; i < p.size(); i++) {
                for (int j = i + 1; j < p.size(); j++) {
                    tp=0;
                    memset(st,0, sizeof st);
                    memset(res,0, sizeof res);
                    st[p[i].first][p[i].second] = 1;
                    st[p[j].first][p[j].second] = 1;
                    //st[1][3] = 1;
                    //st[3][1] = 1;
                    //cout<<"........"<<endl;
                    bfs(p[i].first, p[i].second, p[j].first, p[j].second);
                    //bfs(1, 3, 3, 1);
                    for(int q1=1;q1<=n;q1++){
                        for(int q2=1;q2<=m;q2++){
                            if(st[q1][q2]!=g[q1][q2]) tp=1;
                        }
                    }
                    if(!tp) ww= min(ww,ans);
                    //cout<<tp<<endl;
                    //cout<<ans<<endl;
                    //cout<<"........"<<endl;
                }
            }
            if(ww!=1e7)cout <<"Case "<< ++k << ": " <<ww<< endl;
            else cout <<"Case "<< ++k << ": " <<-1<< endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值