博客
关于我
AcWing 95. 费解的开关 二进制全排列+思维
阅读量:578 次
发布时间:2019-03-09

本文共 2209 字,大约阅读时间需要 7 分钟。

为了解决这个问题,我们需要判断在6步以内是否可以使5x5的灯阵中的所有灯都亮起。每个灯的状态可以通过翻动其开关来改变,并且翻动一个灯会导致其相邻的灯也跟着改变状态。

方法思路

我们可以通过枚举所有可能的初始状态来解决这个问题。具体来说,我们将第一行的灯状态固定下来,共有32种可能的组合。对于每一种组合,我们会模拟翻动灯的过程,计算所需的步骤数,并检查是否在6步以内完成任务。

具体步骤如下:

  • 读取输入,包括n个测试用例。
  • 对于每个测试用例,枚举所有可能的第一行状态(32种)。
  • 对于每种状态,复制初始状态到当前状态数组。
  • 模拟翻动灯的过程,计算所需的步骤数。
  • 检查是否在6步以内完成任务,并记录最小的步骤数。
  • 如果所有情况都遍历过了,找到最小的步骤数,否则返回-1。
  • 解决代码

    import sysdef main():    input = sys.stdin.read().split()    idx = 0    n = int(input[idx])    idx += 1    for _ in range(n):        rows = []        while len(rows) < 5:            rows.append(input[idx])            idx += 1        grid = [list(row) for row in rows]        found = -1        for mask in range(32):            current = [list(row) for row in grid]            cnt = 0            for j in range(5):                if (mask >> j) & 1:                    x, y = 0, j                    for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]:                        if 0 <= x+dx <5 and 0 <= y+dy <5:                            current[x+dx][y+dy] = '1' if current[x+dx][y+dy] == '0' else '0'                    cnt += 1            for i in range(4):                for j in range(5):                    if current[i][j] == '0':                        x, y = i+1, j                        for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]:                            if 0 <= x+dx <5 and 0 <= y+dy <5:                                current[x+dx][y+dy] = '1' if current[x+dx][y+dy] == '0' else '0'                        cnt += 1                        if cnt >= 6:                            break                if cnt >=6:                    break            all_on = True            for j in range(5):                if current[4][j] != '1':                    all_on = False                    break            if all_on and cnt <=6:                if found == -1 or cnt < found:                    found = cnt            if found <=6:                break        print(found if found != -1 else -1)if __name__ == '__main__':    main()

    代码解释

  • 读取输入:从标准输入读取数据,包括n个测试用例。
  • 枚举第一行状态:对于每个测试用例,枚举所有可能的第一行状态(0到31)。
  • 模拟翻动过程:对于每种第一行状态,复制初始状态到当前状态数组,然后模拟翻动灯的过程,计算所需的步骤数。
  • 检查是否完成任务:检查当前状态是否所有灯都亮起,并且步骤数是否在6步以内。如果是,记录最小的步骤数。
  • 输出结果:对于每个测试用例,输出最少需要的步骤数,或者-1表示无法在6步内完成任务。
  • 通过这种方法,我们可以高效地解决问题,并找到最优解。

    转载地址:http://phkpz.baihongyu.com/

    你可能感兴趣的文章
    php 7.2 安装 mcrypt 扩展: mcrypt 扩展从 php 7.1.0 开始废弃;自 php 7.2.0 起,会移到 pecl...
    查看>>
    php aes sha1解密,PHP AES加密/解密
    查看>>
    php array 分片,PHP常用数组函数小结
    查看>>
    php CI框架单个file表单多文件上传例子
    查看>>
    php composer
    查看>>
    reflow和repaint引发的性能问题
    查看>>
    Reflection反射机制原理、使用场景 及 缺陷
    查看>>
    php csv 导出
    查看>>
    php curl 实例+详解
    查看>>
    php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
    查看>>
    php curl_multi批量发送http请求
    查看>>
    php curl请求微信发红包接口出现错误:Peer's Certificate issuer is not recognized.
    查看>>
    PHP curl请求错误汇总和解决方案
    查看>>
    php declare(ticks=1)
    查看>>
    UVA 10474
    查看>>
    php echo 输出 锘?... 乱码问题
    查看>>
    PHP empty、isset、isnull的区别
    查看>>
    ReferenceQueue的使用
    查看>>
    PHP FastCGI进程管理器PHP-FPM的架构
    查看>>
    referenceQueue用法
    查看>>