练习赛补题----B - Marbles 博弈

博客围绕大理石游戏展开,玩家轮流移动大理石,先将其移至(0,0)者获胜。皇帝不想输,需判断能否赢。可将问题转化为Nim游戏模型,剔除x = 0、y = 0、y = x三条线计算SG函数,终结态设为(1, 2)和(2, 1),求棋子位置SG函数异或值判断胜负。

sing marbles as a currency didn’t go so well in Cubicônia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace.

Of course, the game uses marbles, since the Emperor needs to find some use for so many of them. NN marbles are scattered in a board whose lines are numbered from 00 through LL and the columns numbered from 00 through CC. Players alternate turns. In his turn, a player must choose one of the marbles and move it. The first player to move a marble to position (0,0)(0,0) is the winner. The movements are limited so the game could be more interesting; otherwise, the first player could just move a marble to position (0,0)(0,0) and win. A movement consists in choosing an integer uu greater than 00 and a ball, whose location is denoted by (l,c)(l,c), and move it to one of the following positions, as long as it is inside the board:

(l−u,c)(l−u,c) or;
(l,c−u)(l,c−u) or;
(l−u,c−u)(l−u,c−u).
Note that more than one marble can occupy the same position on the board.

As the Emperor doesn’t like to lose, you should help him determine which games he should attend. Also, as expected, the Emperor always take the first turn when playing. Assuming both players act optimally, you are given the initial distribution of the marbles, and should find if it is possible for the Emperor to win if he chooses to play.

Input
The first line contains an integer NN (1≤N≤10001≤N≤1000). Each of the following NN rows contains two integers lili and cici indicating on which row and column the ii-th marble is in (1≤li,ci≤1001≤li,ci≤100).

Output
Your program should print a single line containing the character Y if it is possible for the Emperor to win the game or N otherwise.

Examples
Input
2
1 3
2 3
Output
Y
Input
1
1 2
Output
N

考虑把这个问题简单地转化为Nim游戏的模型。首先如果初始棋子有在x = 0, y = 0, y = x这三条线上的,特判一下输出Y。然后发现在游戏的过程中,将棋子移动到这三条线上就相当于必败,所有棋子肯定不选择移动到这三条线上。于是我们在计算SG函数的时候剔除掉这三条线。同时我们可以找到(1, 2)和(2, 1)这两个点,一旦棋子移动到这两个点,就无法继续移动了,否则对方就会获得胜利。那么(1, 2)和(2, 1)这两个点就对应着Nim游戏里的某一堆石子被取完了。最后的结束条件是所有的棋子都在(1, 2)或者(2, 1)了。
最后的解法:将终结态设为(1, 2)和(2, 1)两个点,SG函数值设为0。DFS求所有点的SG函数,过程中跳过两个坐标轴以及y = x这条线上的点。将n个棋子的位置上的SG函数求异或,最后的结果是0就输出N,否则是Y

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int sg[105][105];
bool vis[205];

void SG()
{
    //里面去除了x轴,y轴,斜线的情况
    for(int i = 1;i <= 100;++i){
        for(int j = 1;j <= 100;++j){
            if(i == j) continue;
            memset(vis,0,sizeof(vis));
            int len = max(i,j);
            for(int k = 1;k <= len;++k){
                int dx = i - k;
                int dy = j - k;
                if(dx > 0 && dx != j){
                    //后继态为(dx,j)
                    vis[sg[dx][j]] = 1;
                }
                if(dy > 0 && dy != i){
                    vis[sg[i][dy]] = 1;
                }
                if(dx > 0 && dy > 0 && dx != dy){
                    vis[sg[dx][dy]] = 1;
                }
            }
            //计算当前状态的sg值
            int g = 0;
            while(vis[g]){
                g++;
            }
            sg[i][j] = g;
        }
    }
}

int main()
{
    SG();
    int n;
    scanf("%d",&n);
    bool flag = false;
    int ans = 0;
    for(int i = 0;i < n;++i){
        int a,b;
        scanf("%d %d",&a,&b);
        if(flag) continue;
        if(a == 0 || b == 0 || a == b){
            flag = true;
            continue;
        }
        ans ^= sg[a][b];
    }
    if(flag){
        printf("Y\n");
    }else{
        if(ans){
            printf("Y\n");
        }else{
            printf("N\n");
        }
    }
    return 0;
}

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值