程序设计与算法(二)001 特殊密码锁

郭祎老师的慕课地址https://www.icourse163.org/learn/PKU-1001894005?tid=1450413466#/learn/content

代码就直接按着网课的例子的代码做了一点修改

#include <set>
#include <iostream>
#include<cstdlib>
#include<map>
#include<cstring>
#include<memory>
using namespace std;
bool orilight[30];
bool optlight[30];
bool lights[30];
bool result[30];

bool Getbit(bool c)//为什么用char,char这里只提供空间,并没有用到asc2映射成字符
{
    return c;//右移i位,再跟1做与
}
void Setbit(bool &c, bool v)
{
    c = v;
}
void Flipbit(bool &c)
{
    c ^= 1;
}
void OutputResult(bool result[])
{
    for(int i = 0;i<30;i++){
            cout<<result[i];
    }
    cout<<endl;
}
/*
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
*/
int main()
{

    string s;
    bool a;
    int flag = 0;//监听(假设没有方法可以实现转变)
    int mx = -1;

    //录入
    cin>>s;
    int n = s.size();
    for(int i = 0;i<n;i++){
        a = s[i]-'0';
        Setbit(orilight[i],a);
    }
    cin>>s;
    for(int i = 0;i<n;i++){
        a = s[i]-'0';
        Setbit(optlight[i],a);
    }

    //枚举(共有64种)
    for(int k =0;k<2;++k){//循环两次
        int switches = k;
        int sum = 0;
        memcpy(lights,orilight,sizeof(orilight));//把值赋给一个用于枚举时实时更新的矩阵。
        for(int i = 0;i<n;++i){
            //记录更改操作
            result[i] = switches;
            if(switches)sum++;
            //更改
            if(switches){
                Flipbit(lights[i]);
                if(i < n-1) Flipbit(lights[i+1]);
                if(i > 0) Flipbit(lights[i-1]);
            }
            //根据实时矩阵和目标矩阵的i,i+1行综合得出第i+1行修改方案
            if(i<n-1){
                if(lights[i]==optlight[i])switches = 0;
                else switches = 1;
            }
        }
        if(lights[n-1] == optlight[n-1]){
            //for(int i = 0;i<n;i++)cout<<result[i];
            mx = max(mx,sum);
            flag = 1;
        }
    }
    if(flag)cout<<mx;
    else cout<<"impossible";
    return 0;
/*
011
000

001
100

*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值