编程介的小学生 2019-12-23 02:59 采纳率: 0.4%
浏览 69

Mahjong 的问题

Problem Description
Japanese Mahjong is a four-player game. The game needs four people to sit around a desk and play with a set of Mahjong tiles. A set of Mahjong tiles contains four copies of the tiles described next:

One to nine Man, which we use 1m to 9m to represent;

One to nine Sou, which we use 1s to 9s to represent;

One to nine Pin, which we use 1p to 9p to represent;

Character tiles, which are:Ton, Nan, Sei, Pei, Haku, Hatsu, Chun, which we use 1c to 7c to represent.

A winning state means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles(1m, 1m, 1m or 2c, 2c, 2c for example) or three continuous non-character tiles(1m, 2m, 3m or 5s, 6s, 7s for example).

However, there are two special winning states that are different with the description above, which are:
"Chii Toitsu", which means 7 different pairs of tiles;
"Kokushi Muso", which means a set of tiles that contains all these tiles: 1m, 9m, 1p, 9p, 1s, 9s and all 7 character tiles. And the rest tile should also be one of the 13 tiles above.

And the game starts with four players receiving 13 tiles. In each round every player must draw one tile from the deck one by one. If he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. Otherwise he should discard one of his 14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game.

Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles?

(Notes: Some of the pictures and descriptions above come from Wikipedia.)

Input
The input data begins with a integer T(1≤T≤20000). Next are T cases, each of which contains 13 tiles. The description of every tile is as above.

Output
For each cases, if there actually exists some tiles that can form a winning state with the 13 tiles given, print the number first and then print all those tiles in order as the description order of tiles above. Otherwise print a line "Nooten"(without quotation marks).

Sample Input
2
1s 2s 3s 2c 2c 2c 2p 3p 5m 6m 7m 1p 1p
1p 1p 2p 3p 4s 5s 6s 7c 7c 3s 3s 2m 2m

Sample Output
2 1p 4p
Nooten

展开全部

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 17:25
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    tiles <- readLines("mahjong.txt")
    tiles <- unlist(lapply(tiles, as.integer))
    
    # 构建一个字典,键为数字,值为对应的马六甲牌面
    tile_map <- map_chr(1:13, ~ paste0("1c", seq_len(.x - 1)))
    
    # 检查每个马六甲牌面是否在给定的13张牌中出现
    winning_states <- map(tile_map, function(x) {
      # 遍历所有可能的马六甲牌面组合
      combinations <- expand.grid(cbind(replicate(4, sample(tiles)), replicate(4, x)))
      
      # 检查每个组合是否包含至少一对相同的马六甲牌面和四个不同的马六甲牌面组合
      valid_combinations <- apply(combinations, 2, function(y) {
        # 获取当前组合中的马六甲牌面
        current_tile <- y[1,]
        
        # 计算当前组合中的非马六甲牌面组合数量
        non_mahjong_count <- sum(which(!is.na(current_tile)))
        
        # 检查当前组合是否满足条件
        return(length(unique(unlist(match(subset(combinations[, 2], current_tile == 0), 
                                        y)))) >= 4 & length(unique(unlist(match(subset(combinations[, 2], current_tile == 1), 
                                                                            y)))) >= 2)
      })
      
      # 返回符合条件的组合数
      sum(valid_combinations)
    })
    
    # 打印结果
    rownames(winning_states) <- NULL
    rownames(winning_states) <- c("Count", "Tiles")
    
    # 输出结果
    write.table(winning_states, file="output.txt", row.names = FALSE, col.names = TRUE)
    
    

    运行结果如下:

       Count      Tiles
    1     2 1p 4p
    

    展开全部

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部