HDU 5077 NAND(真值表状压)

28 篇文章 0 订阅
20 篇文章 0 订阅

NAND

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 457    Accepted Submission(s): 162


Problem Description
Xiaoqiang entered the “shortest code” challenge organized by some self-claimed astrologists. He was given a boolean function taking n inputs (in C++):

bool f(bool x1, bool x2, bool x3){
//your code goes here
//return something
}


All possible inputs and expected outputs of this function have been revealed:



Xiaoqiang’s code must be like:

bool a = NAND(b, c);

where “a” is a newly defined variable,“b” and “c” can be a constant (0/1) or a function parameter (x1/x2/x3) or a previously defined variable. NAND is the “not-and” function:

NAND(b, c)=!(b&&c)

Because NAND is universal, Xiaoqiang knew that he could implement any boolean function he liked. Also, at the end of the code there should be a return statement:

return y;

where y can be a constant or a function parameter or a previously defined variable. After staring at the function for a while, Xiaoqiang came up with the answer:

bool a = NAND(x1, x2);
bool b = NAND(x2, x3);
bool y = NAND(a, b); return y;


Xiaoqiang wants to make sure that his solution is the shortest possible. Can you help him?
 

Input
The first line contains an integer T (T ≤ 20) denoting the number of the test cases.

For each test case, there is one line containing 8 characters encoding the truth table of the function.
 

Output
For each test case, output a single line containing the minimum number of lines Xiaoqiang has to write.
 

Sample Input
  
  
1 00010011
 

Sample Output
  
  
4
Hint
Due to the small input domain, you can solve all the cases on your computer and submit a program with a table of all the answers.
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5906  5905  5904  5903  5902 
 
题目大意:
   输入真值表的最后一列,求最少用多少补NAND(非与)可以得到。

解题思路:
    关键就是对真值表进行状态压缩,把一列压缩成一个数,然后bfs打表。但是这题的打表也有很多要求,需要加各种各样的剪枝,否则速度奇慢。而且,我就算加上能想到的所有剪枝,除了最后一个数都可以在10min打完,但是最后一个特别特别慢。不过我们可以由最后这个数的速度可以猜测出它的值一定比倒数第二个数大,倒数第二个数是9,所以最后一个数是10。我们手动的把这个数不上就行了。

AC代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))

struct Node
{
    int step;
    vector<int> save;
};

int ans[300];
map<vector<int>,bool> ma;//对能得到对数的列表去重
const int res[300] = {0,4,5,2,5,2,6,3,6,7,3,4,3,4,3,0,5,2,6,3,6,3,8,6,7,7,6,4,6,4,6,3,6,7,3,4,7,7,6,4,7,8,4,5,7,7,4,4,3,4,3,0,6,4,6,3,7,7,4,4,4,6,5,3,6,7,7,7,3,4,6,4,7,8,7,7,4,5,4,4,3,4,6,4,3,0,6,3,7,7,4,6,4,4,5,3,7,8,7,7,7,7,4,6,10,8,7,8,7,8,7,7,4,5,4,4,4,4,5,3,7,8,7,7,7,7,7,6,7,8,8,8,8,8,9,8,4,6,5,5,5,5,6,5,8,8,9,8,9,8,9,9,6,5,6,6,6,6,8,6,4,6,5,5,6,5,6,6,4,5,1,2,5,5,3,2,5,5,6,5,6,6,8,6,5,5,3,2,6,6,6,5,4,6,6,5,5,5,6,6,4,5,5,5,1,2,3,2,5,5,6,6,6,5,8,6,5,5,6,6,3,2,6,5,4,5,5,5,5,5,6,6,7,8,4,5,4,5,1,4,1,2,3,2,3,2,6,5,4,5,1,4,1,4,3,0};

void bfs()
{
    ma.clear();
    Node n0;
    n0.save.push_back(0);//结果全为0
    n0.save.push_back(15);//结果同a直接得到
    n0.save.push_back(51);//结果同b直接得到
    n0.save.push_back(85);//结果同c直接得到
    n0.save.push_back(255);//结果全为1
    n0.step=0;
    ma[n0.save]=true;
    queue<Node> que;
    que.push(n0);
    int num=5;
    while(!que.empty())
    {
        Node now=que.front(); que.pop();
        for(int i=0;i<now.save.size();++i)
            for(int j=0;j<now.save.size();++j)
            {
                int next=255^(now.save[i]&now.save[j]);
                if(ans[next]==-1)
                {
                    ans[next]=now.step+1;
                    ++num;
                    cout<<"num: "<<num<<" step+1: "<<now.step+1<<endl;//显示打表进度
                    if(num>=255)
                        return;
                }
                if(binary_search(now.save.begin(),now.save.end(),next))
                    continue;
                Node tmp=now;
                tmp.save.push_back(next);
                sort(tmp.save.begin(),tmp.save.end());//使能得到的数有序,便于用map去重
                ++tmp.step;
                if(ma[tmp.save]==1)
                    continue;
                ma[tmp.save]=true;
                que.push(tmp);
            }
    }
}

void make_table()
{
    mem(ans,-1);
    ans[15]=0;//00001111
    ans[51]=0;//00110011
    ans[85]=0;//01010101
    ans[0]=0;//00000000
    ans[255]=0;//11111111
    bfs();
    for(int i=0;i<256;++i)
        printf("%d,",ans[i]);
}

int main()
{
//    make_table();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        char s[10];
        scanf("%s",s);
        int tmp=0;
        for(int i=0;i<8;++i)
        {
            tmp<<=1;
            tmp+=s[i]-'0';
        }
        printf("%d\n",res[tmp]+1);//最后再加上一条return语句
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值