训练记录 day2

ACW 95. 费解的开关

递推 1.先枚举第一行的所有可能情况 2.从第二层开始递推 3.check最后一层是否符合条件 时间复杂度 第一层情况数T十字形变换递推次数*

```

#include <bits/stdc++.h>
using namespace std;
const int N =7;
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);
char g[N][N],bg[N][N];
int dx[5]={-1,0,1,0,0},dy[5]={0,1,0,-1,0};
void turn(int x,int y){
    for(int i=0;i<5;i++){
        int a=x+dx[i],b=y+dy[i];
        if(a>=0&&a<=4&&b>=0&&b<=4)g[a][b]^=1;
    }
}
signed main(){
    int t;cin>>t;
    while(t--){
        for(int i=0;i<5;i++){
            cin>>bg[i];
        }
        int ans=10;
        for(int i=0;i<32;i++){
            int cnt=0;
            memcpy(g,bg,sizeof g);
            for(int j=0;j<5;j++){
                if(i>>j&1){
                    turn(0,j);
                    cnt++;
                }
            }
            for(int j=0;j<4;j++){
                for(int k=0;k<5;k++){
                    if(g[j][k]=='0'){
                        turn(j+1,k);
                        cnt++;
                    }
                }
            }
            bool flag=true;
            for(int j=0;j<5;j++){
                if(g[4][j]=='0')flag=false;
            }
            if(flag&&ans>cnt)ans=cnt;
        }
        if(ans>6)ans=-1;
        cout<<ans<<Endl;
    }
    return 0^0;
}

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值