首页 > ACM题库 > HDU-杭电 > HDU 1524 A Chess Game-博弈论-[解题报告] C++
2013
12-12

HDU 1524 A Chess Game-博弈论-[解题报告] C++

A Chess Game

问题描述 :

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

输出:

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.

样例输入:

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

样例输出:

WIN
WIN
WIN
LOSE
WIN

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents          
by—cxlove

这是一个针对有向无环图的博弈。

题目:有N个位置,其中存在拓扑关系,移动时必须遵守。最后移动者胜,问是否有必胜策略

http://acm.hdu.edu.cn/showproblem.php?pid=1524

拓扑关系,说明是一个有向无环图。那么对于某个点的SG函数,便是他的后继结点中没有出现的最小的。(MEX操作),完全就是名字悬乎一点

和求普通的SG函数一样。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define N 10005
#define LL long long
#define inf 1<<29
#define eps 1e-7
using namespace std;
vector<int>v[1005];
int n,sg[1005];
int get_sg(int n){
    if(sg[n]!=-1)
       return sg[n];
    if(v[n].size()==0)
       return 0;
    int vis[1005];
    memset(vis,0,sizeof(vis));
    for(int i=0;i<v[n].size();i++){
        sg[v[n][i]]=get_sg(v[n][i]);
        vis[sg[v[n][i]]]=1;
    }
    for(int i=0;;i++)
        if(vis[i]==0)
            return i;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        memset(sg,-1,sizeof(sg));
        for(int i=0;i<n;i++){
            v[i].clear();
            int k,u;
            scanf("%d",&k);
            while(k--){
                scanf("%d",&u);
                v[i].push_back(u);
            }
        }
        int q,k,u;
        while(scanf("%d",&k)&&k){
            int ret=0;
            while(k--){
                scanf("%d",&u);
                if(sg[u]==-1)
                    sg[u]=get_sg(u);
                ret^=sg[u];
            }
            if(ret==0)
                puts("LOSE");
            else
                puts("WIN");
        }
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/acm_cxlove/article/details/7842242


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  3. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮