POJ 4049 Chess(博弈)

这题一开始在用极大极小搜索,一只WA,最后才发现女孩不是走的最优,而是乱走的,知道这一点之后的搜索就好理解了。

//
//  main.cpp
//  Richard
//
//  Created by 邵金杰 on 16/8/28.
//  Copyright © 2016年 邵金杰. All rights reserved.
//


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
vector<pair<int,int> > can;
char str[5][5];
int vis[5][5];
const int boy=0,girl=1;
char boychess,girlchess;
string result;
bool check(int ch)
{
    int tot;
    
    for(int i=0;i<4;i++){
        tot=0;
        for(int j=0;j<4;j++){
            if(str[i][j]==ch) tot++;
        }
        if(tot==4) return true;
    }
    
    for(int i=0;i<4;i++){
        tot=0;
        for(int j=0;j<4;j++){
            if(str[j][i]==ch) tot++;
        }
        if(tot==4) return true;
    }
    
    tot=0;
    for(int i=0;i<4;i++){
        if(str[i][i]==ch) tot++;
    }
    if(tot==4) return true;
    
    tot=0;
    for(int i=0;i<4;i++){
        if(str[i][3-i]==ch) tot++;
    }
    if(tot==4) return true;
    
    return false;
}
bool dfs(int who)
{
    if(who==boy){
        if(check(girlchess))
            return result=="LOSE";
    }
    else{
        if(check(boychess))
            return result=="WIN";
    }
    int sum=0,satisfy=0;
    for(int i=0;i<can.size();i++)
    {
        int x=can[i].first;
        int y=can[i].second;
        if(!vis[x][y])
        {
            sum++;
            vis[x][y]=1;
            if(who==boy)
                str[x][y]=boychess;
            else
                str[x][y]=girlchess;
            if(dfs(!who))
                satisfy++;
            vis[x][y]=0;
            str[x][y]='.';
            if(who==boy&&satisfy>0)
                return 1;
            if(who==girl&&satisfy<sum)
                return 0;
        }
    }
    if(sum==0) return result=="TIE";
    if(who==boy) return satisfy>0;
    else return satisfy==sum;
}
void solve()
{
    memset(vis,0,sizeof(vis));
    int cx=0,co=0;
    can.clear();
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(str[i][j]=='x') {vis[i][j]=1;cx++;}
            if(str[i][j]=='o') {vis[i][j]=1;co++;}
            if(str[i][j]=='.') {can.push_back(make_pair(i,j));}
        }
    }
    if(cx==co)
        boychess='x',girlchess='o';
    else
        boychess='o',girlchess='x';
    if(dfs(boy))
        printf("YES\n");
    else
        printf("NO\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cin>>result;
        for(int i=0;i<4;i++)
            scanf("%s",str[i]);
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值