回复 楼主 lzy19870721 的帖子
经典纳什博弈论有意讨论电脑知识及 算法和 语言类网络类 知识的人 可以入群 75126876
#include <stdio.h> #include <stdlib.h> #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) typedef struct node { int *lhs, *rhs; int sa, sb; } game_node; int min_max(game_node *n, int player, int level) { int lhv, rhv; game_node orig_n = *n; if (n->lhs > n->rhs) { if (player) return n->sa > n->sb; else return n->sb > n->sa; } *(player ? &n->sa : &n->sb) += *n->lhs++; lhv = min_max(n, !player, level + 1); if ((player && lhv) || (!player && !lhv)) return lhv; *n = orig_n; *(player ? &n->sa : &n->sb) += *n->rhs--; rhv = min_max(n, !player, level + 1); return rhv; } int main(void) { int a[] = {3, 5, 3, 2}; game_node inital = {a, a + 3, 20, 20}; printf("%s player win!\n", min_max(&inital, 1, 0)?"first":"second"); printf("score: %d - %d\n", inital.sa, inital.sb); return 0; }