Borya and Hanabi - CF#253 (Div. 2)C (442A) 状压暴力枚举

C. Borya and Hanabi
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.

Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).

The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.

A color hint goes like that: a player names some color and points at all the cards of this color.

Similarly goes the value hint. A player names some value and points at all the cards that contain the value.

Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.

Output

Print a single integer — the minimum number of hints that the other players should make.

Sample test(s)
input
2
G3 G3
output
0
input
4
G4 R4 R3 B3
output
2
input
5
B1 Y1 W1 G1 R1
output
4
Note

In the first sample Borya already knows for each card that it is a green three.

In the second sample we can show all fours and all red cards.

In the third sample you need to make hints about any four colors.


题意:一个人知道他手上有哪些类型的排,但是他不知道顺序,然后每次提醒可以告诉他哪些牌是哪一种颜色的,或哪些牌的序号是多少,问你至少需要多少次提醒才能完全知道手上的排。

思路:状压的暴力枚举,用状压的方式枚举出提醒的所有方式,然后先找到知道的牌,然后通过已知的牌了解到未知的牌有两种情况,一种是已知颜色的牌,数字未知的只剩一个,另一种是已知数字牌,颜色未知的只剩一个。再有就是最后未知的牌只剩1个也是可以的。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
int num1[6][6],num2[6][6],S1,S2,kc[6],kn[6],ans=10;
char str[10];
int findc(int n)
{ int i,j,k=0,p=0;
  for(i=1;i<=5;i++)
   if(num1[n][i]==1 && num2[n][i]==0)
   { p++;
     k=i;
   }
  if(p==1)
  { num2[n][k]=1;
    return 1;
  }
  return 0;
}
int findn(int n)
{ int i,j,k=0,p=0;
  for(i=1;i<=5;i++)
   if(num1[i][n]==1 && num2[i][n]==0)
   { p++;
     k=i;
   }
  if(p==1)
  { num2[k][n]=1;
    return 1;
  }
  return 0;
}
int main()
{ int n,i,j,k;
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  { scanf("%s",str);
    if(str[0]=='R')
     num1[1][str[1]-'0']=1;
    else if(str[0]=='G')
     num1[2][str[1]-'0']=1;
    else if(str[0]=='B')
     num1[3][str[1]-'0']=1;
    else if(str[0]=='Y')
     num1[4][str[1]-'0']=1;
    else if(str[0]=='W')
     num1[5][str[1]-'0']=1;
  }
  for(S1=0;S1<=31;S1++)
  { memset(kc,0,sizeof(kc));
    for(i=0;i<=4;i++)
     if((S1>>i)&1)
     { kc[0]++;
       kc[i+1]=1;
     }
    for(S2=0;S2<=31;S2++)
    { memset(kn,0,sizeof(kn));
      for(i=0;i<=4;i++)
       if((S2>>i)&1)
       { kn[0]++;
         kn[i+1]=1;
       }
      if(kc[0]+kn[0]>=ans)
        continue;
      memset(num2,0,sizeof(num2));
      for(i=1;i<=5;i++)
       for(j=1;j<=5;j++)
        if(num1[i][j]==1 && kc[i]==1 && kn[j]==1)
         num2[i][j]=1;
      k=0;
      while(true)
      {
      for(i=1;i<=5;i++)
       if(kc[i]==1)
        k+=findc(i);
      for(i=1;i<=5;i++)
       if(kn[i]==1)
        k+=findn(i);
       if(k>0)
        k=0;
       else
        break;
      }
      k=0;
      for(i=1;i<=5;i++)
       for(j=1;j<=5;j++)
        if(num1[i][j]==1 && num2[i][j]==0)
         k++;
      if(k<=1)
      { ans=kc[0]+kn[0];
      }
    }
  }
  printf("%d\n",ans);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 下载mysql压缩包 从mysql官网下载相应的mysql压缩包(tar.gz格式),如mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz。 2. 解压mysql压缩包 在终端进入下载目录,执行以下命令解压mysql压缩包: tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 解压后会得到一个mysql-5.7.28-linux-glibc2.12-x86_64的文件夹。 3. 创建mysql用户和组 执行以下命令创建mysql用户和组: groupadd mysql useradd -r -g mysql mysql 4. 移动mysql文件夹 将解压后的mysql文件夹移动到/usr/local目录下: sudo mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql 5. 修改mysql文件夹权限 执行以下命令修改mysql文件夹的权限: sudo chown -R mysql:mysql /usr/local/mysql 6. 初始化mysql 执行以下命令初始化mysql: cd /usr/local/mysql sudo ./bin/mysqld --initialize --user=mysql 执行完上述命令后,会生成一个随机密码,需要记录下来。密码会保存在日志文件中,可以在终端中执行以下命令查看: sudo cat /var/log/mysqld.log | grep 'temporary password' 7. 启动mysql 执行以下命令启动mysql: sudo systemctl start mysql 8. 修改mysql密码 执行以下命令修改mysql密码: sudo ./bin/mysql_secure_installation 按照提示进行操作,修改mysql密码和其他配置。 9. 验证mysql安装是否成功 执行以下命令验证mysql安装是否成功: sudo ./bin/mysql -u root -p 输入修改后的mysql密码,如果成功登录mysql,则说明mysql安装成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值