麻将的胡牌算法

来源:互联网 发布:免费制作名片软件 编辑:程序博客网 时间:2024/05/17 01:31

正常的麻将胡牌方式为满足N * ABC + M *DDD +EE 的形式,及存在一个对子(EE),剩余牌均能组成顺子(ABC)或者刻子(DDD)。

很容易发现必须满足size%3 == 2的形式才可以去计算胡牌。

数据结构的选取:

麻将有万、饼、条各九种,另外还有东西南北中,春夏秋冬。
种类不是很多,一个字节表示就可以了,前四位代表类型,后四位代表值,东西南北中,春夏秋冬可以集中到一种类型中去。

普通麻将的计算方式:

  • 1.首先找出所有包含一对的情形,移除对子(注意去重),记下剩余牌的所有集合为Tn;
  • 2.针对每个Tn中的数组尝试移除一个顺子,成功转到2,失败到3。
  • 3.针对每个Tn中的数组尝试移除一个刻子(DDD),成功转到2。
  • 4.若当前的数组的数量变为0,则表示,当前的方案可以胡牌。

2,3,4可以作为一个check_3n(检测是否满足N * ABC + M *DDD)的函数,递归调用即可。

针对有癞子的麻将(百搭):

最简单的办法是尝试将癞子牌变为所有派来进行尝试,不过如果手中有多张癞子牌的话计算量就相当大了,比如3张,则需要计算牌的种类的3次方次,虽然中途可以通过剪枝减少部分计算量,但还是太慢了。
针对这种情况我们可以在计算出癞子的数量,如果出现找出顺子或刻子失败,我们则可以用癞子去补,如果失败了,那么当前的方案就不通过。

  • 1.同样找出所有包含一对的情形,移除对子,移除的时候需要注意更新癞子的数量这里需要注意的是对子是怎么产生的:
    • 原有的对子
    • 一个癞子和一普通的组成的对子
    • 一对癞子
  • 2.针对每个数组尝试移除一个顺子,成功转到2,如果失败尝试用癞子去补,癞子也不够,转到3。
  • 3.针对每个数组尝试移除一个刻子(DDD),成功转到2,如果失败尝试用癞子去补,癞子也不够,当前的方案就不通过。
  • 4.若当前的数组的数量变为0,则表示,当前的方案可以胡牌。

有些人好像还不很了解,补充一个lua版无癞子的算法吧(很多复制都可以优化掉):

function table_copy_table(ori_tab)    if (type(ori_tab) ~= "table") then        return nil    end    local new_tab = {}    for i,v in pairs(ori_tab) do        local vtyp = type(v)        if (vtyp == "table") then            new_tab[i] = table_copy_table(v)        elseif (vtyp == "thread") then            new_tab[i] = v        elseif (vtyp == "userdata") then            new_tab[i] = v        else            new_tab[i] = v        end    end    return new_tabendfunction sort_card(t)    table.sort(t, function(a,b)        local sa,ra = a&0xf,a&0xf0        local sb,rb = b&0xf,b&0xf0        if ra == rb then            return sa < sb        else            return ra < rb        end    end)endfunction remove_three_same(t)    assert(#t%3 == 0 and #t > 0)    local found = false    local begin    for i=1,#t - 2 do        if t[i] == t[i + 1] and t[i] == t[i + 2] then            found = true            begin = i            break        end    end    if found then        local ret = {}        for k=1,3 do            table.insert(ret,table.remove(t,begin))        end        return true,ret    endendfunction remove_straight(t)    assert(#t%3 == 0 and #t > 0)    local ret = {}    for i=1,#t - 2 do        local found1        local found2        local position = {}        table.insert(position,i)        for k = i,#t do            if not found1 and t[i] + 1 == t[k] then                found1  = true                 table.insert(position,k)            end             if not found2 and t[i] + 2 == t[k] then                found2  = true                 table.insert(position,k)            end             if found2 and found1 then                break            end        end        if found2 and found1 then            for i = #position,1,-1 do                table.insert(ret,table.remove(t,position[i]))            end            return true,ret        end    endendfunction check_3n(set)    assert(#set%3 == 0)    sort_card(set)    local t1 = table_copy_table(set)    local t2 = table_copy_table(set)    if remove_three_same(t1) then        if #t1 == 0 or check_3n(t1)then            return true        end    end    if remove_straight(t2) then        if #t2 == 0 or check_3n(t2) then            return true        end    end    return falseendfunction check_hu(set)    assert((#set)%3 == 2)    sort_card(set)    for i=1,#set - 1 do        if set[i] == set[i + 1] then            local check = {}            for k = 1,#set do                if k ~= i and k ~= i + 1 then                    table.insert(check,set[k])                end            end            if #check == 0 or check_3n(check) then                return true            end        end    endendlocal sets = {    {0x11,0x11,0x11,0x11,0x12,0x12,0x12,0x12,0x13,0x13,0x13,0x13,0x14,0x14,0x14,0x14,0x15},     {0x11,0x11},    {0x12,0x13,0x14,0x23,0x23,0x24,0x25,0x26}}for k,v in pairs(sets) do    print(check_hu(v))end

再补个c版的吧:

#include "stdio.h"#include "stdbool.h"#include <stdlib.h>#include <malloc.h>bool check_hu(unsigned char *A, unsigned int sz);bool check_3n(unsigned char *set, unsigned int sz);bool remove_straight(unsigned char *set, unsigned int sz);bool remove_three_same(unsigned char *set, unsigned int sz);void sort_card(unsigned char *A, unsigned int sz);bool big_than(unsigned char a, unsigned char b);void dump(unsigned char *set, unsigned int sz);bool check_hu(unsigned char *A, unsigned int sz) {    unsigned char *set = (unsigned char *)malloc(sizeof(char) * sz);    sort_card(A, sz);    unsigned int i = 0;    for (i = 0; i < sz; i++) {        if (A[i] == A[i + 1]) {            int k = 0;            for (size_t j = 0; j < sz; j++) {                if (j != i && j != i + 1) {                    set[k] = A[j];                    k = k + 1;                }            }            if (check_3n(set, sz - 2) || sz - 2 == 0) {                return true;            }        }    }    free(set);    return false;}bool check_3n(unsigned char *set, unsigned int sz) {    if (sz == 0) {        return true;    }    int set_t[20] = { 0 };    for (size_t i = 0; i < sz; i++)    {        set_t[i] = set[i];    }    if (remove_straight(set, sz)) {        if (check_3n(set, sz - 3))        {            return true;        }    }    for (size_t i = 0; i < sz; i++)    {        set[i] = set_t[i];    }    if (remove_three_same(set, sz)) {        if (check_3n(set, sz - 3))        {            return true;        }    }    return false;}bool remove_straight(unsigned char *set, unsigned int sz) {    int second = 0, third = 0;    for (size_t i = 0; i < sz; i++)    {        if (set[i] == set[0] + 1)        {            second = i;        }        if (set[i] == set[0] + 2)        {            third = i;        }        if (set[i] > set[0] + 2)        {            break;        }        if (second != 0 && third != 0)        {            break;        }    }    if (second != 0 && third != 0)    {        int j = 0;        for (size_t i = 1; i < sz; i++)        {            if (i != second && i != third)            {                set[j] = set[i];                j++;            }        }        return true;    }    return false;}bool remove_three_same(unsigned char *set, unsigned int sz) {    if (set[0] == set[2] && set[1] == set[0]) {        unsigned int i = 0;        for (i = 0; i < sz - 3; i++) {            set[i] = set[i + 3];        }        return true;    }    return false;}//----------------------------------------------------------------------------void sort_card(unsigned char *A, unsigned int sz) {    int i = 0, j = 0;    int len = sz;    for (i = len - 1; i >= 0; i--) {        unsigned char max = A[i];        int postion = i;        for (j = 0; j <= i; j++) {            if (big_than(A[j], max)) {                max = A[j];                postion = j;            }        }        unsigned char temp = A[i];        A[i] = max;        A[postion] = temp;    }}bool big_than(unsigned char a, unsigned char b) {    if ((a & (unsigned int)0xf0) > (b & (unsigned int)0xf0)) {        return true;    }    else if ((a & (unsigned int)0xf0) == (b & (unsigned int)0xf0)) {        if ((a & (unsigned int)0xf) > (b & (unsigned int)0xf)) {            return true;        }    }    return false;}void dump(unsigned char *set, unsigned int sz) {    for (size_t i = 0; i < sz; i++)    {        printf("%02x ", *(set + i));    }    printf("\n");}int main() {    unsigned char A[17] = {0x11,0x11,0x11,0x11,0x12,0x12,0x12,0x12,0x13,0x13,0x13,0x13,0x14,0x14,0x14,0x14,0x15};    bool hu1 = check_hu(A,17);    bool hu2 = check_hu(A,2);    printf("%d  %d\n",hu1,hu2);}
7 1