NEFU 1356 帽儿山奇怪的棋盘(暴搜)

帽儿山奇怪的棋盘
帽儿山奇怪的棋盘
Problem:1356

Time Limit:1000ms

Memory Limit:65535K

Description
军哥来到了帽儿山,发现有两位神人在顶上对弈。棋盘长成下图的模样:

这里写图片描述
每个点都有一个编号:由上到下,由左到右,依次编号为 1、2……12。两位神人轮流博
弈,每一轮操作的一方可以取走一个棋子,或者取走相邻的两个棋子(即在同一直线上相邻
的棋子)。取走最后一颗棋子的人输。
给定初始状态,如果两个人都采取最优决策,问谁能赢。
Input
第一行一个数 n 表示数据组数,接下来 n 行每行一个长度为 12 的 01 串,1 表示该位置
上有棋子,0 表示没有。(n<=100)
Output
输出一行,一个长度为 n 的 01 串,0 表示先手赢,1 表示后手赢。
Sample Input
3
110000000000
100000000000
110000000011
Sample Output
010

思路

分析一下我们其实可以有24种操作,取单石子有12种,连着取两个也有12种,我们再利用博弈的一个性质,在下一个局势下若我是必败的那这个局势我就是必胜的了,那我们规定一下当没有石子的时候是胜态,那么可以用dfs暴搜状态了

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<set>
using namespace std;
int dx[12]={1<<0,1<<1,1<<2,1<<3,1<<3,1<<4,1<<4,1<<6,1<<7,1<<8,1<<7,1<<8};
int dy[12]={1<<3,1<<4,1<<3,1<<4,1<<7,1<<5,1<<8,1<<7,1<<8,1<<9,1<<10,1<<11};
int dfs(int x)
{
    if(x==0) return 0;
    else if(x==1) return 1;
    for(int i=0;i<12;i++)
        if((x&(1<<i))&&dfs(x^(1<<i))) return 0;
    for(int i=0;i<12;i++)
        if((x&dx[i])&&(x&dy[i])&&dfs(x^dx[i]^dy[i])) return 0;
    return 1;
}
int main()
{
    int n;
    scanf("%d",&n);
        char a[n][13];
        for(int i=0;i<n;i++)
            scanf("%s",a[i]);
        for(int i=0;i<n;i++)
        {
            int x=0;
            for(int j=0;j<12;j++)
                if(a[i][j]=='1')
                x+=1<<j;
            printf("%d",dfs(x));
        }
        printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ftx456789/article/details/79854232