北航 机试 五子棋

题目描述:
输入一个19*19的矩阵,只包含数字0、1、2,表示两人下五子棋的棋牌状态,1、2分别表示两人的棋子,0表示空格。要求判断当前状态下是否有人获胜(横向、竖向或者斜线方向连成5个同色棋子)。题目说明输入样例保证每条线上至多只有连续5个同色棋子,并且保证至多只有1人获胜。如果有人获胜,输出获胜者(1或2)加一个冒号,接着输出获胜的五连珠的第一个棋子的坐标,从上到下从左到右序号最小的为第一个,序号从1开始编号。如果无人获胜,输出no。

代码
#include <stdio.h>

int next[4][2] = {{0,1},{1,0},{1,1},{1,-1}};

int count = 1;

int right_search(int m[19][19],int i,int j){
    if(count == 5){
        return count;
    }
    if(j>=18){
        return count;
    }
    if(m[i][j] == m[i][j+1]){
        count++;
        right_search(m,i,j+1);
    }
    return count;
}
int down_search(int m[19][19],int i,int j){
    if(count == 5){
        return count;
    }
    if(i>=18){
        return count;
    }
    if(m[i][j] == m[i+1][j]){
        count++;
        down_search(m,i+1,j);
    }
    return count;
}

int downright_search(int m[19][19],int i,int j){
    if(count == 5){
        return 1;
    }
    if(j>=18 || i>=18){
        return count;
    }
    if(m[i][j] == m[i+1][j+1]){
        count++;
        downright_search(m,i+1,j+1);
    }
    return count;
}

int leftdown_search(int m[19][19],int i,int j){
    if(count == 5){
        return count;
    }
    if(j<=0 || i>=18){
        return count;
    }
    if(m[i][j] == m[i+1][j-1]){
        count++;
        leftdown_search(m,i+1,j-1);
    }
    return count;
}

void output(int v,int i,int j){
    if(v == 1){
        printf("1:");
    }
    else{
        printf("2:");
    }
    printf("%d,%d",i,j);
}

int main() {

    //input juzheng
    int matrix[19][19];
    FILE* fp;
    if( (fp=fopen("./input","r")) == NULL){
        puts("error");
        return 0;
    }
    int c;
    for(int i=0;i<19;i++){
        for(int j=0;j<19;j++){

            fscanf(fp,"%d",&c);

            matrix[i][j] = c;
        }
    }

    for(int i=0;i<19;i++){
        for(int j=0;j<19;j++){
            if(matrix[i][j] == 0)
                continue;
            count = 1;
            if(right_search(matrix,i,j) == 5){
//                //output
//                for(int k=0;k<5;k++){
//                    printf("%d",matrix[i][j+k]);
//                }
                output(matrix[i][j],i,j);
                return 0;

            }

            count =1;
            if(downright_search(matrix,i,j) == 5){
                //output
//                for(int k=0;k<5;k++){
//                    printf("%d",matrix[i+k][j+k]);
//                }

                output(matrix[i][j],i,j);
                return 0;

            }

            count =1;
            if(down_search(matrix,i,j) == 5){

//                for(int k=0;k<5;k++){
//                    printf("%d %d",matrix[i+k][j]);
//                }

                output(matrix[i][j],i,j);
                return 0;
            }

            count =1;
            if(leftdown_search(matrix,i,j) == 5){

//                for(int k=0;k<5;k++){
//                    printf("%d",matrix[i+1][j-1]);
//                }

                output(matrix[i][j],i,j);
                return 0;
            }
        }
    }
    printf("no\n");
    return 0;
}
样例输入
1 2 2 1 2 2 0 1 0 0 0 0 0 0 0 0 0 0 0
1 2 0 2 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值