HDU 5077 NAND (2014鞍山现场赛H题,搜索打表)

胡搞,搜索+打表。

NAND

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

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

2014 Asia AnShan Regional Contest

可以直接进行dfs. 把大部分答案打出来。   搜索策略就是纯粹的爆搜, 我是把深度现在在10以下,这样可以把答案 <= 9 的数据都打出来了。   打表程序:  

/* ***
Author :kuangbin
Created Time :2014/11/20 22:57:22
File Name :E:\2014ACM\2014现场赛\鞍山\H.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;
int dp[1<<8];
bool used[1<<8];
unsigned char a[1<<8];
int cnt;
void dfs(int dep){
if(dep >= 10)return;
for(int i = 0;i < cnt;i++)
for(int j = i;j < cnt;j++){
unsigned char tmp = ~(a[i]&a[j]);
if(used[tmp])continue;
used[tmp] = true;
a[cnt++] = tmp;
if(dp[tmp] == -1 || dp[tmp] > dep)dp[tmp] = dep;
dfs(dep+1);
used[tmp] = false;
cnt–;
}
}

int main()
{
//freopen(“in.txt”,”r”,stdin);
freopen(“out.txt”,”w”,stdout);
memset(dp,-1,sizeof(dp));
cnt = 0;
dp[0] = 1;
dp[255] = 1;
unsigned char x1 = 0x0f;
unsigned char x2 = 0x33;
unsigned char x3 = 0x55;
dp[x1] = 1;
dp[x2] = 1;
dp[x3] = 1;
memset(used,false,sizeof(used));
used[0] = true;
used[255] = true;
used[x1] = true;
used[x2] = true;
used[x3] = true;
a[cnt++] = 0;
a[cnt++] = 255;
a[cnt++] = x1;
a[cnt++] = x2;
a[cnt++] = x3;
dfs(2);
for(int i = 0;i < 256;i++)
printf(“%d,\n”,dp[i]);
return 0;
}

这个程序大概几分钟就跑出来了。   然后有几个是-1,说明没有打出来,他们的答案肯定是 >=10的。 然后其余几个随机吧!   随机一发在HDU竟然一直都是AC的~~~~  

/* ***
Author :kuangbin
Created Time :2014/11/20 23:29:04
File Name :E:\2014ACM\2014现场赛\鞍山\H2.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;
int dp[] = {
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,
-1,
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,
-1,
9,
5,
7,
6,
6,
6,
6,
7,
6,
9,
9,
-1,
9,
-1,
9,
-1,
-1,
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()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int T;
char str[20];
scanf(“%d”,&T);
srand(time(NULL));
while(T–){
scanf(“%s”,str);
int n = 0;
for(int i = 0;i < 8;i++){
n *= 2;
n += str[i]-‘0’;
}
if(dp[n] != -1)printf(“%d\n”,dp[n]);
else printf(“%d\n”,10+rand()%3);
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 509.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%