枚举之特殊密码锁

题目:有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态。然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转。当然,如果你按的是最左或者最右边的按钮,该按钮只会影响到跟它相邻的一个按钮。当前密码锁状态已知,需要解决的问题是,你至少需要按多少次按钮,才能将密码锁转变为所期望的目标状态。
输入:两行,给出两个由0、1组成的等长字符串,表示当前/目标密码锁状态,其中0代表凹,1代表凸。
输出:至少需要进行的按按钮操作次数,如果无法实现转变,则输出impossible。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int oriLock;// The range of int is between 2^31 and -2^31-1, so we can use int to indicate lock.
int lock;
int destLock;
inline void SetBit(int & n, int i, int v)//inline function 内联函数
{
    if (v)
        n |= (1 << i);
    else
        n &= ~(1 << i);
}
inline void FlipBit(int & n, int i)
{
    n ^= (1 << i);
}
inline int GetBit(int n, int i)
{
    return (n >> i) & 1;
}
int main()
{
    char line[40];
    destLock = lock = oriLock = 0;
    cin >> line;
    int N = strlen(line);
    for (int i = 0; i < N; ++i)
        SetBit(oriLock, i, line[i] - '0');//SetBit(int & n, int i, int v)
    cin >> line;
    for (int i = 0; line[i]; ++i)
        SetBit(destLock, i, line[i] - '0');
    int minTimes = 1 << 30;//each button has two states,total 2^30.
    for (int p = 0; p < 2; ++p) {//Press the 1th button or not. During the first loop , we do not press the
        lock = oriLock; // 1th button, but during the second loop,we press.
        int times = 0;
        int curButton = p;
        for (int i = 0; i < N; ++i) {
            if (curButton) {
                ++times;
                if (i > 0)
                    FlipBit(lock, i - 1);
                FlipBit(lock, i);
                if (i < N - 1)
                    FlipBit(lock, i + 1);
            }
            if (GetBit(lock, i) != GetBit(destLock, i))
                curButton = 1;
            else
                curButton = 0;
        }
        if (lock == destLock)
            minTimes = min(minTimes, times);
    }
    if (minTimes == 1 << 30)
        cout << "imposibble" << endl;
    else
    {
        cout << minTimes << endl;
        return 0;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个基于STM32的按键电子密码可以分为以下步骤: 1. 硬件设计:根据需求选择合适的STM32型号和按键、LED等外设,设计电路原理图和PCB布局。 2. 软件开发:使用Keil等集成开发环境编写STM32的C语言程序,包括按键扫描、密码输入、密码验证、开关控制等功能。 3. 调试测试:将程序烧录到STM32芯片中,通过串口调试工具等方式验证程序的正确性和稳定性。 下面是一个简单的示例程序,以实现4位数字密码为例: ```c #include "stm32f10x.h" // 按键GPIO端口和引脚定义 #define KEY_GPIO_PORT GPIOA #define KEY_GPIO_PIN GPIO_Pin_0 // LED GPIO端口和引脚定义 #define LED_GPIO_PORT GPIOB #define LED_GPIO_PIN GPIO_Pin_0 // 密码长度和正确密码定义 #define PASSWORD_LEN 4 const uint8_t PASSWORD[PASSWORD_LEN] = {1, 2, 3, 4}; // 按键状态枚定义 typedef enum { KEY_RELEASED = 0, KEY_PRESSED } KeyStateTypeDef; // 按键扫描函数 KeyStateTypeDef KeyScan(void) { static uint8_t keyStatus = KEY_RELEASED; if (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == Bit_RESET) { if (keyStatus == KEY_RELEASED) { keyStatus = KEY_PRESSED; return KEY_PRESSED; } } else { keyStatus = KEY_RELEASED; } return KEY_RELEASED; } // LED控制函数 void LedControl(uint8_t state) { if (state == Bit_SET) { GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN); } else { GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN); } } int main(void) { uint8_t passwordInput[PASSWORD_LEN] = {0}; uint8_t passwordIndex = 0; // 初始化GPIO端口 GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 按键输入,浮空输入 GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN; GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // LED输出,推挽输出 GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN; GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure); // 主循环 while (1) { // 按键扫描 if (KeyScan() == KEY_PRESSED) { // 密码输入 if (passwordIndex < PASSWORD_LEN) { passwordInput[passwordIndex++] = passwordIndex; LedControl(Bit_SET); } } // 密码验证 if (passwordIndex == PASSWORD_LEN) { uint8_t i; for (i = 0; i < PASSWORD_LEN; i++) { if (passwordInput[i] != PASSWORD[i]) { break; } } if (i == PASSWORD_LEN) { // 密码正确,打开 LedControl(Bit_RESET); } // 重置密码输入状态 passwordIndex = 0; passwordInput[0] = passwordInput[1] = passwordInput[2] = passwordInput[3] = 0; } } } ``` 以上示例程序仅实现了按键扫描和密码验证的功能,你可以根据实际需求添加更多功能,例如LCD显示、EEPROM存储、报警等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值