//
// 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;
}
POJ 1568 Find the Winning Move(极大极小搜索+alpha-beta剪枝)
最新推荐文章于 2019-09-20 21:16:28 发布