费解的开关

暴力枚举所有状态

先将第一行都变成1

再将中间三行编程1

判断最后一行是不是全是1

记录答案存最小步数

 

#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
int dx[]={1,-1,0,0,0};
int dy[]={0,0,-1,1,0};
char g[6][6],u[6][6];
void turn(int a,int b){
    for(int i=0;i<5;i++){
        int x=a+dx[i],y=b+dy[i];
        if(x<0||y<0||x>=5||y>=5)continue;
        g[x][y]^=1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    while(cin>>t){
        while(t--)
        {
            for(int i=0;i<5;i++) cin>>g[i];
            int res,ans=0x7fffffff;
            for(int op=0;op<32;op++)
            {
                res=0;
                memcpy(u,g,sizeof u);
                for(int i=0;i<5;i++)
                {
                    if(op>>i&1)
                    {
                        turn(0,i);
                        res++;
                    }
                }
                for(int i=0;i<4;i++)
                    for(int j=0;j<5;j++)
                    {
                        if(g[i][j]=='0')
                        {
                            res++;
                            turn(i+1,j);
                        }
                    }
                bool check=true;
                for(int i=0;i<5;i++)
                    if(g[4][i]=='0')check=false;
                if(check)ans=min(res,ans);
                memcpy(g,u,sizeof g);
            }
            cout<<(ans<=6?ans:-1)<<"\n";
        }
    }
}
View Code

 

posted @ 2021-06-06 21:00  Acception  阅读(36)  评论(0)    收藏  举报