题目描述:
输入一个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