您的位置:首页 > 编程语言 > Go语言

ACM-ICPC 4836 Gomoku

2014-04-30 17:09 183 查看
暴力枚举了所有情况......不知道有没有更好的做法

/*author: birdstorm*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

#define MAXN 1005
#define N 205
#define INF 1<<30
#define eps 1.0e-10

#define For(i,m,n) for(i=(m);i<n;i++)
#define MAX(x,y) (x)>(y)?(x):(y)
#define MIN(x,y) (x)<(y)?(x):(y)

int a[20][20], goalx, goaly;

int over()
{
int i, j, temp;
For(i,0,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i][j+1]==temp&&a[i][j+2]==temp&&a[i][j+3]==temp&&a[i][j+4]==temp) return 1;
}
For(j,0,15) For(i,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j]==temp&&a[i+2][j]==temp&&a[i+3][j]==temp&&a[i+4][j]==temp) return 1;
}
For(i,0,11) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j+1]==temp&&a[i+2][j+2]==temp&&a[i+3][j+3]==temp&&a[i+4][j+4]==temp) return 1;
}
For(i,4,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i-1][j+1]==temp&&a[i-2][j+2]==temp&&a[i-3][j+3]==temp&&a[i-4][j+4]==temp) return 1;
}
return 0;
}

int judge(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(over()){
goalx=i; goaly=j;
ans++;
}
a[i][j]=0;
}
return ans;
}

int judgetie(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(judge(player)>=2&&judge(-player)==0){
goalx=i;
goaly=j;
return 1;
}
a[i][j]=0;
}
return 0;
}

main()
{
int i, j, k, n, p, q, x, y;
int black, white, count, next;
while(scanf("%d",&n),n){
For(i,0,15) For(j,0,15) a[i][j]=0;
black=white=count=0;
For(i,0,n){
scanf("%d%d%d",&x,&y,&p);
q=p==0?-1:1;
a[x][y]=q;
if(q==1) black++;
else white++;
}
if(black<white||black>white+1){
puts("Invalid.");
continue;
}
next=black==white?1:-1;
if(judge(next)>=1) printf("Place %s at (%d,%d) to win in 1 move.\n",next==1?"black":"white",goalx,goaly);
else if(judge(-next)>=2) puts("Lose in 2 moves.");
else if(judgetie(next)) printf("Place %s at (%d,%d) to win in 3 moves.\n",next==1?"black":"white",goalx,goaly);
else puts("Cannot win in 3 moves.");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: