POJ 2599 A funny game(组合博弈 P/N态+记忆化搜索)

Description

There are several airports in one country, and there are flights between some of them. One can fly from any airport to any other, probably with some changes. For any pair of airports there exists only one sequence of flights that connects them. 

Two terrorists play a game. They make moves in turn. Each move consists of the following operations. A player mines an airport, chooses a flight and flies away together with his colleague. After the take-off he actuates a radio-controlled fuse. As a result the airport that the terrorists have just left is destroyed, and all the flights to and from this airport are no longer possible. After the aircraft lands the other player makes his move, and so forth. One loses if one cannot make a move. 

Given an initial list of flights and the number of an airport where the terrorists are at the start of the game, write a program which would determine who wins if the terrorists play a perfect game (each chooses the best move).

Input

The first line of an input contains two integers: N and K separated with a space. Here N is the number of airports (N <= 1000) and K is the number of an airport, which is the starting point of the game (1 <= K<= N). The next n-1 lines of the file contain pairs of integers separated with a space. These integers are numbers of airports, connected with a flight (all the flights are symmetric and are mentioned only once). There are at most 20 flights to each airport. Input data are always correct.

Output

If the player who starts the game wins, the program should write: "First player wins flying to airport L" where L is the number of an airport to which the players should fly first. If there are more than one such airports, the program should find one of them that has the minimal number. Otherwise the program should write "First player loses".

Sample Input

4 3
3 2
3 1
1 4

Sample Output

First player wins flying to airport 2
题意:一个有向图中几个点相连接,然后从规定的地方走,不可以回头,2人轮流,假设双方足够聪明,判断谁可以让对方无路可走
思路:组合博弈中的P/N态+记忆化搜索,(如果从一个点可以到达P态的点,那么原先的那个点为N态;如果从一个点怎么走哪里任何一个位置都到达不了的P态,则原先点为P态)
注意:此题目中每次只对一个点判断,所以数据相对较小,直接判断即可,不需要打表记录
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 1005
int M[MAXN][MAXN],vis[MAXN],L,N,K;
bool win(int p)
{
    vis[p]=true;
    for(int i=1;i<=N;i++){
        if(M[p][i]&&!vis[i]){ //有这条路径且未被访问
            if(!win(i)){      //如果i为P态,则p点为N态
                L=i;          //记录下位置
                return true;  //返回
            }
        }
    }
    return false;    //没有在i——N中找到P态,则p点为P态
}

int main()
{
    while(scanf("%d%d",&N,&K)==2){
        memset(M,0,sizeof(M));
        memset(vis,0,sizeof(vis));
        int x,y;
        for(int i=1;i<N;i++){
          scanf("%d%d",&x,&y);
          M[x][y]=M[y][x]=1;
        }
        if(win(K))
            printf("First player wins flying to airport %d\n",L);
        else
            printf("First player loses\n");
    }
}


教练PPT的题目,一周之后才逐渐理解博弈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值