POJ1568 四子棋 博弈

  1. 必胜:无论对方走什么都可以必胜。
    即:轮己方走时,有true则true,轮对方走时,全true为true。
  2. 竞赛中的题目通常是搜到底然后利用alpha-beta剪枝优化,根据兄弟节点的值及时剪枝。
  3. 人工智能的算法中取max的最大值,min的最小值,再优化,相当于1和-1,这是极大极小值算法。
  4. 充分不必要的bug最为隐晦。

题目链接:http://acm.hust.edu.cn/vjudge/problem/26288

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
char str[5][5];
int X,Y,chess;
//判断一个局面是否结束
bool check(int x,int y){
    int 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 MaxSearch(int x,int y);
int MaxSearch(int x,int y){
    //已经结束
    if(check(x,y)) return -1;
    //平局
    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 tmp=MinSearch(i,j);
                str[i][j]='.';chess--;
                //对方需要找的最差估价,如果当前比之前最差的高,剪枝
                if(tmp == 1) return 1;
            }
    return -1;
}
int MinSearch(int x,int y){
    //已经结束
    if(check(x,y)) return 1;
    //平局
    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 tmp=MaxSearch(i,j);
                str[i][j]='.';chess--;
                //自己需要找的最高估价,如果当前比之前最差的低,剪枝
                if(tmp == -1 || tmp == 0) return -1;
            }
    return 1;
}
bool slove(){
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            //枚举,然后搜索
            if(str[i][j]=='.'){
                str[i][j]='x';chess++;
                int tmp = MinSearch(i,j);
                str[i][j]='.';chess--;
                if(tmp == 1){
                    X=i;
                    Y=j;
                    return true;
                }
            }
    return false;
}
int main(){
    char ch[5];
    while(scanf("%s",ch)!=EOF&&ch[0]!='$'){
        chess=0;
        for(int i=0;i<4;i++){
            scanf("%s",str[i]);
            for(int j=0;j<4;j++)
                chess+=str[i][j]!='.';
        }
        //这一步直接从2S+到0ms,哭~~~
        if(chess<=4){
            printf("#####\n");
            continue;
        }
        if(slove()) printf("(%d,%d)\n",X,Y);
        else printf("#####\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值