主要是把各offset存起来遍历,避免写重复代码。
#include <cstdio>
#include <cstring>
int chess[16][16]; // 棋盘上-1为没下,0为A,1为B
int offset[4][2][2] = {
{
{ 1, 0 }, { -1, 0 }
},
{
{ 0, 1 }, { 0, -1 }
},
{
{ 1, 1 }, { -1, -1 }
},
{
{ 1, -1 }, { -1, 1 }
}
};
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
memset(chess, -1, sizeof(chess));
int is_a = 0; // 当前局是否为A下棋
int win_step = 0;
for (int step = 1; step <= n; ++step) {
int x, y;
scanf("%d%d", &x, &y);
if (win_step) continue; // 如果已经有胜负的,剩下的就只读入
chess[x][y] = is_a;
for (int idx = 0; idx < 4; ++idx) { // index
int count = 1; // -4
for (int dir = 0; dir < 2; ++dir) { // direction
int ox = offset[idx][dir][0];
int oy = offset[idx][dir][1];
for (int tx = x + ox, ty = y + oy;
1 <= tx && tx <= 15 && 1 <= ty && ty < 15;
tx += ox, ty += oy) {
if (chess[tx][ty] == is_a) {
count++;
} else {
break;
}
}
}
if (count >= 5) {
win_step = step;
}
}
is_a = !is_a;
}
if (!win_step) {
printf("Tie");
} else {
printf("%s %d", is_a ? "A" : "B", win_step);
}
return 0;
}