C 自动判断五子棋胜负的程序
从前有一天,ly与lyon在讨论人工智能里面的好,他们提到了五子棋。当然,这里说的五子棋是指无禁手(不知道什么是禁手的也不用管了,跟这题没关系)的五子棋:
黑先下,黑白轮流下,最先在横竖斜任一方向上形成连续的5个子(或以上)就算赢。
对此,ly和lyon都有自己的一套判断局势的算法,并且根据自己的想法各写了一个判断局况的程序。然而,他们都觉得自己的程序要比对方的优秀,所以,
他们稍作改良,做成了自动决策的对局程序,并拿出来互相pk。目前需要一个自动判断胜负的程序,即最先出现5连子的判胜。
输入格式
第1行输入两个数n和m,用空格分开,n为棋盘横纵坐标的最大值,m为步数:
1<=n<=1000,0<m<=n*n
第2行到第m+1行为第一步到第m步的坐标,每行两个数,用空格分开:
x和y,1<=x,y<=n
输入保证不存在重复下子。
(出题人LRC)
输出格式
输出首次分出胜负那一步的序号(第一步为1),如果走完了都没有分出胜负,输出“baga”。
输入样例
5 11
3 3
2 3
2 4
4 3
4 2
3 4
1 5
3 2
5 1
1 1
1 2
输出样例
9
#include<stdio.h>
int main(void)
{
int n,k,j,t,a1=0,b1=0,flag=0;
int m,i;
int a[1000],b[1000];
int c[1000][1000];
scanf("%d%d",&n,&m);
for(t=1;t<=n;t++)
for(j=1;j<=n;j++)
c[t][j]=0;
for(i=1;i<=m;i++)
scanf("%d%d",&a[i],&b[i]);
for(i=1;i<=m;i++)
{
if(i%2!=0)
{ a1=a[i];b1=b[i];c[a1][b1]=1; }//黑棋置为1;
else if(i%2==0)
{ a1=a[i];b1=b[i];c[a1][b1]=2; }//白棋置为2
//判断五子是否在横线上
if((c[a1][b1]==1&&c[a1+1][b1]==1&&c[a1+2][b1]==1&&c[a1+3][b1]==1&&c[a1+4][b1]==1)||
(c[a1][b1]==2&&c[a1+1][b1]==2&&c[a1+2][b1]==2&&c[a1+3][b1]==2&&c[a1+4][b1]==2)||
(c[a1][b1]==1&&c[a1-1][b1]==1&&c[a1+1][b1]==1&&c[a1+2][b1]==1&&c[a1+3][b1]==1)||
(c[a1][b1]==2&&c[a1-1][b1]==2&&c[a1+1][b1]==2&&c[a1+2][b1]==2&&c[a1+3][b1]==2)||
(c[a1][b1]==1&&c[a1-2][b1]==1&&c[a1-1][b1]==1&&c[a1+1][b1]==1&&c[a1+2][b1]==1)||
(c[a1][b1]==2&&c[a1-2][b1]==2&&c[a1-1][b1]==2&&c[a1+1][b1]==2&&c[a1+2][b1]==2)||
(c[a1][b1]==1&&c[a1-3][b1]==1&&c[a1-2][b1]==1&&c[a1-1][b1]==1&&c[a1+1][b1]==1)||
(c[a1][b1]==2&&c[a1-3][b1]==2&&c[a1-2][b1]==2&&c[a1-1][b1]==2&&c[a1+1][b1]==2)||
(c[a1][b1]==1&&c[a1-1][b1]==1&&c[a1-2][b1]==1&&c[a1-3][b1]==1&&c[a1-4][b1]==1)||
(c[a1][b1]==2&&c[a1-1][b1]==2&&c[a1-2][b1]==2&&c[a1-3][b1]==2&&c[a1-4][b1]==2))
{flag=1; break;}
//判断五子是否在竖线上
else if((c[a1][b1]==1&&c[a1][b1+1]==1&&c[a1][b1+2]==1&&c[a1][b1+3]==1&&c[a1][b1+4]==1)||
(c[a1][b1]==2&&c[a1][b1+1]==2&&c[a1][b1+2]==2&&c[a1][b1+3]==2&&c[a1][b1+4]==2)||
(c[a1][b1]==1&&c[a1][b1-1]==1&&c[a1][b1+1]==1&&c[a1][b1+2]==1&&c[a1][b1+3]==1)||
(c[a1][b1]==2&&c[a1][b1-1]==2&&c[a1][b1+1]==2&&c[a1][b1+2]==2&&c[a1][b1+3]==2)||
(c[a1][b1]==1&&c[a1][b1+1]==1&&c[a1][b1+2]==1&&c[a1][b1-1]==1&&c[a1][b1-2]==1)||
(c[a1][b1]==2&&c[a1][b1+1]==2&&c[a1][b1+2]==2&&c[a1][b1-1]==2&&c[a1][b1-2]==2)||
(c[a1][b1]==1&&c[a1][b1+1]==1&&c[a1][b1-1]==1&&c[a1][b1-2]==1&&c[a1][b1-3]==1)||
(c[a1][b1]==2&&c[a1][b1+1]==2&&c[a1][b1-1]==2&&c[a1][b1-2]==2&&c[a1][b1-3]==2)||
(c[a1][b1]==1&&c[a1][b1-1]==1&&c[a1][b1-2]==1&&c[a1][b1-3]==1&&c[a1][b1-4]==1)||
(c[a1][b1]==2&&c[a1][b1-1]==2&&c[a1][b1-2]==2&&c[a1][b1-3]==2&&c[a1][b1-4]==2))
{flag=1;break;}
//判断五子是否在斜线上即 \
else if((c[a1][b1]==1&&c[a1+1][b1+1]==1&&c[a1+2][b1+2]==1&&c[a1+3][b1+3]==1&&c[a1+4][b1+4]==1)||
(c[a1][b1]==2&&c[a1+1][b1+1]==2&&c[a1+2][b1+2]==2&&c[a1+3][b1+3]==2&&c[a1+4][b1+4]==2)||
(c[a1][b1]==1&&c[a1-1][b1-1]==1&&c[a1+1][b1+1]==1&&c[a1+2][b1+2]==1&&c[a1+3][b1+3]==1)||
(c[a1][b1]==2&&c[a1-1][b1-1]==2&&c[a1+1][b1+1]==2&&c[a1+2][b1+2]==2&&c[a1+3][b1+3]==2)||
(c[a1][b1]==1&&c[a1+1][b1+1]==1&&c[a1+2][b1+2]==1&&c[a1-1][b1-1]==1&&c[a1-2][b1-2]==1)||
(c[a1][b1]==2&&c[a1+1][b1+1]==2&&c[a1+2][b1+2]==2&&c[a1-1][b1-1]==2&&c[a1-2][b1-2]==2)||
(c[a1][b1]==1&&c[a1+1][b1+1]==1&&c[a1-1][b1-1]==1&&c[a1-2][b1-2]==1&&c[a1-3][b1-3]==1)||
(c[a1][b1]==2&&c[a1+1][b1+1]==2&&c[a1-1][b1-1]==2&&c[a1-2][b1-2]==2&&c[a1-3][b1-3]==2)||
(c[a1][b1]==1&&c[a1-1][b1-1]==1&&c[a1-2][b1-2]==1&&c[a1-3][b1-3]==1&&c[a1-4][b1-4]==1)||
(c[a1][b1]==2&&c[a1-1][b1-1]==2&&c[a1-2][b1-2]==2&&c[a1-3][b1-3]==2&&c[a1-4][b1-4]==2))
{flag=1;break;}
//判断五子是否在斜线上即 /
else if((c[a1][b1]==1&&c[a1+1][b1-1]==1&&c[a1+2][b1-2]==1&&c[a1+3][b1-3]==1&&c[a1+4][b1-4]==1)||
(c[a1][b1]==2&&c[a1+1][b1-1]==2&&c[a1+2][b1-2]==2&&c[a1+3][b1-3]==2&&c[a1+4][b1-4]==2)||
(c[a1][b1]==1&&c[a1-1][b1+1]==1&&c[a1+1][b1-1]==1&&c[a1+2][b1-2]==1&&c[a1+3][b1-3]==1)||
(c[a1][b1]==2&&c[a1-1][b1+1]==2&&c[a1+1][b1-1]==2&&c[a1+2][b1-2]==2&&c[a1+3][b1-3]==2)||
(c[a1][b1]==1&&c[a1+1][b1-1]==1&&c[a1+2][b1-2]==1&&c[a1-1][b1+1]==1&&c[a1-2][b1+2]==1)||
(c[a1][b1]==2&&c[a1+1][b1-1]==2&&c[a1+2][b1-2]==2&&c[a1-1][b1+1]==2&&c[a1-2][b1+2]==2)||
(c[a1][b1]==1&&c[a1+1][b1-1]==1&&c[a1-1][b1+1]==1&&c[a1-2][b1+2]==1&&c[a1-3][b1+3]==1)||
(c[a1][b1]==2&&c[a1+1][b1-1]==2&&c[a1-1][b1+1]==2&&c[a1-2][b1+2]==2&&c[a1-3][b1+3]==2)||
(c[a1][b1]==1&&c[a1-1][b1+1]==1&&c[a1-2][b1+2]==1&&c[a1-3][b1+3]==1&&c[a1-4][b1+4]==1)||
(c[a1][b1]==2&&c[a1-1][b1+1]==2&&c[a1-2][b1+2]==2&&c[a1-3][b1+3]==2&&c[a1-4][b1+4]==2))
{flag=1;break;}
}
if(flag==1)
printf("%d",i);
else printf("baga");
return 0;
}
//////////哪位大神能帮我看下,我拜他为师!!!为什么无法编译
[ 本帖最后由 公子小白2 于 2012-12-9 22:35 编辑 ]