AcWing 95. 费解的开关(递推)

题目链接

活动 - AcWing 本活动组织刷《算法竞赛进阶指南》,系统学习各种编程算法。主要面向有一定编程基础的同学。https://www.acwing.com/problem/content/97/

题解

只要第一行开关的状态确定,则所有开关的状态都可以被推出来。第一行开关总共有种操作方法,可以先二进制枚举出第一行的状态,其它行的状态就可以从上一行推出来。上一行为0,下一行必须得变;上一行为1,下一行必须不变。最终,如果最后一行全为1且步数小于等于6,则可以使所有的灯全变亮,否则不能。

代码

cpp 复制代码
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 6;

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)  // 按一下第x行第y列的开关
{
    for (int i = 0; i < 5; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= 5 || b < 0 || b >= 5) continue;
        g[a][b] ^= 1;
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        for (int i = 0; i < 5; i++) scanf("%s", bg[i]);
        
        int res = 10;
        for (int op = 0; op < 32; op++)
        {
            int cnt = 0;
            memcpy(g, bg, sizeof g);
            // 操作第一行的开关
            for (int i = 0; i < 5; i++)
                if (op >> i & 1)
                {
                    turn(0, i);
                    cnt++;
                }
                
            // 递推出第1~4行开关的状态
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 5; j++)
                    if (g[i][j] == '0')
                    {
                        turn(i + 1, j);
                        cnt++;
                    }
            
            // 检查最后一行灯是否全亮
            bool success = true;
            for (int i = 0; i < 5; i++)
                if (g[4][i] == '0')
                    success = false;
            if (success && res > cnt) res = cnt;
        }
        
        if (res > 6) res = -1;
        printf("%d\n", res);
    }
    
    return 0;
}
相关推荐
民乐团扒谱机8 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
饮水桶_T8 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
wy_hhxx9 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者10 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK11 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜14 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z14 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
h_a_o777oah14 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
草履虫AI课15 小时前
强化学习从零到一 07|重要性采样:借别人的数据,算自己的期望
算法·强化学习