POJ 1568 Find the Winning Move(极大极小搜索+alpha-beta剪枝)

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


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=100000000;
char str[5][5];
int X,Y,chess;
int MinSearch(int x,int y,int alpha);
int MaxSearch(int x,int y,int beta);
bool check(int x,int y)
{
    int tot;
    tot=0;
    for(int i=0;i<4;i++){
        if(str[x][i]=='o') tot++;
        else if(str[x][i]=='x') tot--;
    }
    if(tot==4||tot==-4) return true;
    
    tot=0;
    for(int i=0;i<4;i++){
        if(str[i][y]=='o') tot++;
        else if(str[i][y]=='x') tot--;
    }
    if(tot==4||tot==-4) return true;
    
    tot=0;
    for(int i=0;i<4;i++){
        if(str[i][i]=='o') tot++;
        else if(str[i][i]=='x') tot--;
    }
    if(tot==4||tot==-4) return true;
    
    tot=0;
    for(int i=0;i<4;i++)
    {
        if(str[i][3-i]=='o') tot++;
        else if(str[i][3-i]=='x') tot--;
    }
    if(tot==4||tot==-4) return true;
    
    return false;
}
int MinSearch(int x,int y,int alpha)
{
    int ans=INF;
    if(check(x,y)) return ans;
    if(chess==16) return 0;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(str[i][j]=='.')
            {
                str[i][j]='o';chess++;
                int temp=MaxSearch(i,j,ans);
                str[i][j]='.';chess--;
                ans=min(ans,temp);
                if(ans<=alpha) return ans;
            }
        }
    }
    return ans;
}
int MaxSearch(int x,int y,int beta)
{
    int ans=-INF;
    if(check(x,y)) return ans;
    if(chess==16) return 0;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(str[i][j]=='.')
            {
                str[i][j]='x';chess++;
                int temp=MinSearch(i,j,ans);
                str[i][j]='.';chess--;
                ans=max(ans,temp);
                if(ans>=beta) return ans;
            }
        }
    }
    return ans;
}
void solve()
{
    int alpha=-INF;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(str[i][j]!='.') chess++;
        }
    }
    if(chess<=4) {printf("#####\n");return ;}
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(str[i][j]=='.')
            {
                str[i][j]='x';chess++;
                int ans=MinSearch(i,j,alpha);
                str[i][j]='.';chess--;
                if(ans==INF)
                {
                    printf("(%d,%d)\n",i,j);
                    return ;
                }
            }
        }
    }
    printf("#####\n");
}
int main()
{
    char ch[5];
    while(scanf("%s",ch)&&ch[0]!='$')
    {
        chess=0;
        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、付费专栏及课程。

余额充值