zoj 3316 Game 一般图最大匹配+博弈 有N个棋子在棋盘上,2个人轮流拿走一个棋子,第一步可以拿任意一个,而之后每一步必须拿上一步拿走的棋子曼哈顿长度L以内的棋子,问,后手是否能赢

Fire and Lam are addicted to the game of Go recently. Go is one of the oldest board games. It is rich in strategy despite its simple rules. The game is played by two players who alternately place black and white stones on the vacant intersections of a grid of 19*19 lines. Once placed on the board, stones cannot be moved elsewhere, unless they are surrounded and captured by the opponent's stones. The object of the game is to control (surround) a larger portion of the board than the opponent.

Fire thinks it is too easy for him to beat Lam. So he thinks out a new game to play on the board. There are some stones on the board, and we don't need to care about the stones' color in this new game. Fire and Lam take turns to remove one of the stones still on the board. But the Manhattan distance between the removed stone and the opponent's last removed stone must not be greater than L. And the one who can't remove any stone loses the game.

The Manhattan distance between (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

To show the performance of grace, Fire lets Lam play first. In the beginning of the game, Lam can choose to remove any stone on the board.

Fire and Lam are clever, so they both use the best strategy to play this game. Now, Fire wants to know whether he can make sure to win the game.

Input

There are multiple cases (no more than 30).

In each case, the first line is a positive integer n (n <= 361) which indicates the number of stones left on the board. Following are n lines, each contains a pair of integers x and y (0 <= xy <= 18), which indicate a stone's location. All pairs are distinct. The last line is an integer L (1 <= L <= 36).

There is a blank line between cases.

Ouput

If Fire can win the game, output "YES"; otherwise, just output "NO".

Sample Input

2
0 2
2 0
2

2
0 2
2 0
4

Sample Output

NO
YES


//

分析:把每一个棋子与周围距离为L的棋子都连上边后,形成一些联通块。易知,一轮游戏只能在某一个联通块里开始直到结束。那么,如果有一个联通块不是完美匹配,先手就可以走那个没被匹配到的点,后手不论怎么走,都必然走到一个被匹配的点上,先手就可以顺着这个交错路走下去,最后一定是后手没有路可走,因为如果还有路可走,这一条交错路,就是一个增广路,必然有更大的匹配。

总结:判断是否是完美匹配。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=450;
int n;//点数(1->n)
int head;
int tail;
int Start;
int Finish;
int _link[N];     //表示哪个点匹配了哪个点
int Father[N];   //这个就是增广路的Father……但是用起来太精髓了
int Base[N];     //该点属于哪朵花
int Q[N];
bool mark[N];
bool mat[N][N];//邻接矩阵
bool InBlossom[N];
bool in_Queue[N];




void BlossomContract(int x,int y){
    memset(mark,0,sizeof(mark));
    memset(InBlossom,0,sizeof(InBlossom));
    #define pre Father[_link[i]]
    int lca,i;
    for (i=x;i;i=pre) {i=Base[i]; mark[i]=true; }
    for (i=y;i;i=pre) {i=Base[i]; if (mark[i]) {lca=i; break;} }  //寻找lca之旅……一定要注意i=Base[i]
    for (i=x;Base[i]!=lca;i=pre){
        if (Base[pre]!=lca) Father[pre]=_link[i]; //对于BFS树中的父边是匹配边的点,Father向后跳
        InBlossom[Base[i]]=true;
        InBlossom[Base[_link[i]]]=true;
    }
    for (i=y;Base[i]!=lca;i=pre){
        if (Base[pre]!=lca) Father[pre]=_link[i]; //同理
        InBlossom[Base[i]]=true;
        InBlossom[Base[_link[i]]]=true;
    }
    #undef pre
    if (Base[x]!=lca) Father[x]=y;     //注意不能从lca这个奇环的关键点跳回来
    if (Base[y]!=lca) Father[y]=x;
    for (i=1;i<=n;i++)
      if (InBlossom[Base[i]]){
          Base[i]=lca;
          if (!in_Queue[i]){
              Q[++tail]=i;
              in_Queue[i]=true;     //要注意如果本来连向BFS树中父结点的边是非匹配边的点,可能是没有入队的
          }
      }
}


void Change(){
    int x,y,z;
    z=Finish;
    while (z){
        y=Father[z];
        x=_link[y];
        _link[y]=z;
        _link[z]=y;
        z=x;
    }
}


void FindAugmentPath(){
    memset(Father,0,sizeof(Father));
    memset(in_Queue,0,sizeof(in_Queue));
    for (int i=1;i<=n;i++) Base[i]=i;
    head=0; tail=1;
    Q[1]=Start;
    in_Queue[Start]=1;
    while (head!=tail){
        int x=Q[++head];
        for (int y=1;y<=n;y++)
          if (mat[x][y] && Base[x]!=Base[y] && _link[x]!=y)   //无意义的边
            if ( Start==y || _link[y] && Father[_link[y]] )    //精髓地用Father表示该点是否
                BlossomContract(x,y);
            else if (!Father[y]){
                Father[y]=x;
                if (_link[y]){
                    Q[++tail]=_link[y];
                    in_Queue[_link[y]]=true;
                }
                else{
                    Finish=y;
                    Change();
                    return;
                }
            }
    }
}


void Edmonds(){
    memset(_link,0,sizeof(_link));
    for (Start=1;Start<=n;Start++)
      if (_link[Start]==0)
        FindAugmentPath();
}


void output(){
    memset(mark,0,sizeof(mark));
    int cnt=0;//一般图最大匹配  最大点数
    for (int i=1;i<=n;i++)
      if (_link[i]) cnt++;
    printf("%d\n",cnt);
    for (int i=1;i<=n;i++)
      if (!mark[i] && _link[i]){
          mark[i]=true;//i和link[i]匹配
          mark[_link[i]]=true;
          printf("%d %d\n",i,_link[i]);
      }
}
int _abs(int x){return x>0?x:-x;}
struct Node
{
    int x,y;
};
Node p[N];
int main(){
    int l;
    while(scanf("%d",&n)==1)
    {
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);scanf("%d",&l);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(_abs(p[i].x-p[j].x)+_abs(p[i].y-p[j].y)<=l)
                {
                    mat[i][j]=mat[j][i]=1;
                }
            }
        }
        Edmonds();
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(_link[i]) cnt++;
        }
        if(cnt==n) printf("YES\n");//所有块都是完美匹配,先手败
        else printf("NO\n");//先手选不是最大匹配中的点,后手只能选最大匹配中的点或不能选,此时先手选后手匹配的点,先手胜
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值