首页 > 代码库 > hdu 5077 NAND(暴力打表)

hdu 5077 NAND(暴力打表)

题目链接:hdu 5077 NAND

题目大意:Xiaoqiang要写一个编码程序,然后根据x1,x2,x3的值构造出8个字符,现在给定要求生成的8个字符,问

说Xiaoqiang最少要写多少行代码。代码内容只能为NAND操作和return操作,操作的变量可以是常数。

解题思路:输入总共就256中情况,所以暴力剪枝打表,打表的代码手贱给删了。。。所以就将一下思路,开一个s数组

表示变量,然后对应每一层每次两个变量进行NAND操作。

大致三个剪枝,dfs时候,变量出现相同就跳过;8个字符可以直接根据数的位运算计算;单前层出现相同的跳过。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int ans[300] = {1, 5, 6, 3, 6, 3, 7, 4, 7, 8, 4, 5, 4, 5, 4, 1, 6, 3, 7, 4, 7, 4, 9, 7, 8, 8, 7, 5, 7, 5, 7, 4, 7, 8, 4, 5, 8, 8, 7, 5, 8, 9, 5, 6, 8, 8, 5, 5, 4, 5, 4, 1, 7, 5, 7, 4, 8, 8, 5, 5, 5, 7, 6, 4, 7, 8, 8, 8, 4, 5, 7, 5, 8, 9, 8, 8, 5, 6, 5, 5, 4, 5, 7, 5, 4, 1, 7, 4, 8, 8, 5, 7, 5, 5, 6, 4, 8, 9, 8, 8, 8, 8, 5, 7, 11, 9, 8, 9, 8, 9, 8, 8, 5, 6, 5, 5, 5, 5, 6, 4, 8, 9, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9, 9, 9, 10, 9, 5, 7, 6, 6, 6, 6, 7, 6, 9, 9, 10, 9, 10, 9, 10, 10, 7, 6, 7, 7, 7, 7, 9, 7, 5, 7, 6, 6, 7, 6, 7, 7, 5, 6, 2, 3, 6, 6, 4, 3, 6, 6, 7, 6, 7, 7, 9, 7, 6, 6, 4, 3, 7, 7, 7, 6, 5, 7, 7, 6, 6, 6, 7, 7, 5, 6, 6, 6, 2, 3, 4, 3, 6, 6, 7, 7, 7, 6, 9, 7, 6, 6, 7, 7, 4, 3, 7, 6, 5, 6, 6, 6, 6, 6, 7, 7, 8, 9, 5, 6, 5, 6, 2, 5, 2, 3, 4, 3, 4, 3, 7, 6, 5, 6, 2, 5, 2, 5, 4, 1};

int main () {
    int cas;
    char p[10];
    scanf("%d", &cas);
    while (cas--) {
        scanf("%s", p);
        int ret = 0;
        for (int i = 0; i < 8; i++)
            ret = ret * 2 + p[i] - ‘0‘;
        printf("%d\n", ans[ret]);
    }

    return 0;
}

hdu 5077 NAND(暴力打表)