zoukankan      html  css  js  c++  java
  • Codeforces 442A

    题目链接

    A. 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.

    Accepted Code:

     1 from collections import defaultdict
     2 
     3 def main():
     4     input()
     5     cards = list(set(raw_input().split()));
     6 
     7     if len(cards) == 1:
     8         print 0
     9         return
    10     answer = 9;
    11     chars = 'RGBYW12345'
    12     for mask in xrange(1, 1024):
    13           cover = ''
    14         for i in xrange(10):
    15             if (mask & (1<<i)):
    16                   cover += chars[i]
    17 
    18         d = defaultdict(int)
    19           
    20         for card in cards:
    21             if card[0] in cover and card[1] in cover:
    22                 continue;
    23             if card[0] in cover:
    24                 d[card[0]] += 1;
    25             elif card[1] in cover:
    26                 d[card[1]] += 1;
    27             else:
    28                 d['*'] += 1;
    29         
    30         if all(x <= 1 for x in d.values()):
    31               answer = min(answer, len(cover));
    32         
    33     print answer;
    34 
    35 if __name__ == '__main__':
    36     main()

    第一次使用collections.defaultdict,如果定义一个dict(字典),当访问一个不存在的键值的时候会发生异常,

    但是defaultdict这种情况下就会提供一个默认值,他使用一个函数作为初始化的参数,可以是内建的类型,如int, list等,

    还可以是用户自定义的不含参数的函数,以该函数的返回值作为其默认值。

    关于all这个函数,他以一个可迭代的类型的对象作为参数,当所有的值都为True则返回True

  • 相关阅读:
    git错误操作导致代码丢失找回
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
    WebMvcConfigurerAdapter在Spring boot2.x已废弃
    AJAX实现模拟注册跳转
    遍历字符串替换实例
    spring boot2.x依赖
    Thymeleaf页面添加th:value后报错原因
    thymeleaf中th:href字符拼接
    刁肥宅数据结构课设“布隆过滤器的实现和应用”源代码(v1.0,永不上交)
    数据结构实验1:C++实现静态顺序表类
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3800717.html
Copyright © 2011-2022 走看看