类似五子棋游戏,N*N的棋盘,黑白双方谁先达成一条线(横竖斜都可以)M个子获胜。注意必须是整整M个子,不能少也不能多
初始状态落定一些棋子,黑棋(先落子)比白棋多一个
编写程序判断,黑棋是否已经满足条件,如果满足输出1
如果黑棋不满足条件,白棋可以在任意位置落一子,如果能满足条件,则输出2
如果白棋所有位置均不能满足,则输出-1
输入
N,M
N*N棋盘数据,1为黑子,2为白子
输出
获胜一方
4
5 5
1 0 2 0 0
0 1 2 0 0
0 0 1 0 0
0 0 2 1 0
0 2 0 0 1
7 5
0 0 0 2 2 0 0
1 1 1 1 1 1 1
0 1 2 1 2 2 2
0 0 2 1 2 1 0
0 0 1 1 2 0 0
0 2 2 1 1 0 0
2 1 2 0 2 2 0
7 5
0 0 0 0 2 0 0
1 1 1 1 1 1 2
0 1 2 0 2 0 0
0 0 2 0 2 1 0
0 1 0 1 2 0 0
0 2 2 1 1 0 0
2 0 2 0 0 0 0
7 5
0 0 0 0 0 0 0
1 1 2 0 0 0 0
0 1 2 0 2 0 0
0 0 2 2 0 1 0
0 1 0 1 1 0 0
0 2 2 1 1 0 0
2 0 2 0 0 0 0
#1 1
#2 1
#3 2
#4 2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int T, M, N, answer, data[50][50], d[4][2] = { { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 } };
bool check(int type, int x, int y) {
for (int i = 0; i < 4; i++) {
int count = 1;
int currentX = x + d[i][0];
int currentY = y + d[i][1];
while (true) {
if (currentX >= 0 && currentX < N && currentY < N && data[currentX][currentY] == type) {
count++;
currentX += d[i][0];
currentY += d[i][1];
} else {
break;
}
}
currentX = x - d[i][0];
currentY = y - d[i][1];
while (true) {
if (currentX >= 0 && currentX < N && currentY >= 0 && data[currentX][currentY] == type) {
count++;
currentX -= d[i][0];
currentY -= d[i][1];
} else {
break;
}
}
if (count == M) {
return true;
}
}
return false;
}
int main(int argc, char** argv) {
freopen("sample_input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for (int test_case = 1; test_case <= T; ++test_case) {
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &data[i][j]);
}
}
answer = -1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (data[i][j] == 1 && check(1, i, j)) {
answer = 1;
goto finish;
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (data[i][j] == 0 && check(2, i, j)) {
answer = 2;
goto finish;
}
}
}
finish: printf("#%d %d\n", test_case, answer);
}
return 0;
}