比赛 20241024 评测结果 AAAAAAAAAA
题目名称 费解的开关 最终得分 100
用户昵称 darkMoon 运行时间 0.115 s
代码语言 C++ 内存使用 3.58 MiB
提交时间 2024-10-24 08:49:59
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
auto IN = freopen("switch.in", "r", stdin);
auto OUT = freopen("switch.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
int t = mread(), xx[5] = {0, 0, 0, 1, -1}, yy[5] = {0, 1, -1, 0, 0};
signed main(){
    while(t --){
        char s[10][10];
        char t[10][10];
        int n = 5;
        for(int i = 1; i <= n; i ++){
            scanf("%s", s[i] + 1);
        }
        int m = 1 << n, ans = LONG_LONG_MAX;
        auto make = [&](int x, int y){
            for(int i = 0; i < 5; i ++){
                int vx = x + xx[i], vy = y + yy[i];
                if(t[vx][vy] == '1'){
                    t[vx][vy] = '0';
                }
                else{
                    t[vx][vy] = '1';
                }
            }
        };
        auto solve = [&](int now){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    t[i][j] = s[i][j];
                }
            }
            int sum = 0;
            for(int i = 1; i <= n; i ++){
                if((now >> i - 1) & 1){
                    make(1, i);
                    sum ++;
                }
            }
            for(int i = 2; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    if(t[i - 1][j] == '0'){
                        make(i, j);
                        sum ++;
                    }
                }
            }
            for(int i = 1; i <= n; i ++){
                if(t[n][i] == '0'){
                    return 10ll;
                }
            }
            return sum;
        };
        for(int now = 0; now < m; now ++){
            ans = min(ans, solve(now));
        }
        if(ans >= 7){
            printf("-1\n");
        }
        else{
            printf("%lld\n", ans);
        }
    }
    return 0;
}