ACW 4223. 点火游戏 day5

 数据范围太小了 问就是暴力 O(T*n^6)

最开始没有清空g啊 非常痛苦!

#include <bits/stdc++.h>
using namespace std;
const int N = 12;
const int mod =11451419;
#define endl '\n'
#define Endl '\n'
#define inf 1e18
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int n,m,dist[N][N],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char g[N][N];
int bfs(int x,int y,int x1,int y1){
    memset(dist,-1,sizeof dist);
    dist[x][y]=0;
    dist[x1][y1]=0;
    queue<pair<int,int>>q;
    q.push({x,y});
    q.push({x1,y1});
    while(!q.empty()){
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            if(dist[t.first+dx[i]][t.second+dy[i]]==-1&&g[t.first+dx[i]][t.second+dy[i]]=='#'){
                dist[t.first+dx[i]][t.second+dy[i]]=dist[t.first][t.second]+1;
                q.push({t.first+dx[i],t.second+dy[i]});
            }
        }
    }
    int res=-2e9;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(dist[i][j]==-1&&g[i][j]=='#')return 2e9;
            if(g[i][j]=='#')res=max(res,dist[i][j]);
        }
    }
    return res;
}
signed main(){
    fast
    int t;cin>>t;
    int cnt=0;
    while(t--){
        cnt++;
        cin>>n>>m;
        memset(g,0,sizeof g);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>g[i][j];
            }
        }
        int ans=2e9;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                for(int k=i;k<n;k++){
                    int q;
                    if(k==i)q=j;
                    else q=0;
                    for(;q<m;q++){
                        if(g[i][j]!='#')goto out;
                        else if(g[k][q]=='#'){
                            ans=min(ans,bfs(i,j,k,q));
                        }
                    }
                }
                out:1;
            }
        }
        if(ans!=2e9)cout<<"Case "<<cnt<<": "<<ans<<endl;
        else cout<<"Case "<<cnt<<": "<<-1<<endl;
    }
    return 0^0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值