POJ2425(树形,无向无环图博弈) SG函数

本文介绍了一个基于图的拓扑结构的两人棋类游戏编程问题,玩家轮流移动棋子直到一方无法移动为止。文章通过实现Sprague-Grundy(SG)函数来解决这类博弈问题,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description
Let’s design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed edges connecting some positions, and no cycle exists. Two players you and I move chesses alternately. In each turn the player should move only one chess from the current position to one of its out-positions along an edge. The game does not end, until one of the players cannot move chess any more. If you cannot move any chess in your turn, you lose. Otherwise, if the misfortune falls on me… I will disturb the chesses and play it again.

Do you want to challenge me? Just write your program to show your qualification!
Input
Input contains multiple test cases. Each test case starts with a number N (1 <= N <= 1000) in one line. Then the following N lines describe the out-positions of each position. Each line starts with an integer Xi that is the number of out-positions for the position i. Then Xi integers following specify the out-positions. Positions are indexed from 0 to N-1. Then multiple queries follow. Each query occupies only one line. The line starts with a number M (1 <= M <= 10), and then come M integers, which are the initial positions of chesses. A line with number 0 ends the test case.
Output
There is one line for each query, which contains a string “WIN” or “LOSE”. “WIN” means that the player taking the first turn can win the game according to a clever strategy; otherwise “LOSE” should be printed.
Sample Input
4
2 1 2
0
1 3
0
1 0
2 0 2
0

4
1 1
1 2
0
0
2 0 1
2 1 1
3 0 1 3
0
Sample Output
WIN
WIN
WIN
LOSE
WIN

题意:两人挪棋子,棋子放在棋盘上,一个人挪动的时候可以移动到链接到的下一个点。当一个人不能挪动棋子的时候就输了。问先手是赢还是输?
解法:如果光看一个石子的话,这就是sg函数的定义。然后对整个图处理一下每个点的sg值。对这M个点怎么处理,这么想吧。假设其中一个点为i,其sg值为sg[i] = a (a != 0)
那可以发现,i可以走向[0, a-1]的任意sg值,这相当与什么?相当与这里有一堆石子,共a个。M个点就是M堆石子,每堆石子的个数就是这个点的sg值。所以这就是NIM博弈的模型了。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
const int maxn = 1010;
using namespace std;
vector <int> G[maxn];
int n, m, SG[maxn];

int dfs(int u)
{
    int i;
    if(SG[u] != -1) return SG[u];
    bool vis[maxn];
    memset(vis, 0, sizeof(vis));
    for(i = 0; i < G[u].size(); i++){
        vis[dfs(G[u][i])] = 1;
    }
    i = 0;
    while(vis[i]) i++;
    return SG[u] = i;
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 0; i < maxn; i++) G[i].clear();
        memset(SG, -1, sizeof(SG));
        for(int i = 0; i < n; i++){
            int k;
            scanf("%d", &k);
            if(k == 0) SG[i] = 0;
            for(int j = 0; j < k; j++){
                int x;
                scanf("%d", &x);
                G[i].push_back(x);
            }
        }
        while(scanf("%d", &m) != EOF)
        {
            if(m == 0) break;
            int XOR = 0;
            for(int i = 0; i < m; i++){
                int x;
                scanf("%d", &x);
                XOR ^= dfs(x);
            }
            if(XOR == 0) printf("LOSE\n");
            else printf("WIN\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值