这道题是很有意思的问题,但很遗憾数据是错的。从讨论区可看到很多朋友对此题疑惑,特此分享我认为合理的解法。
数据的错误:
题干样例数据就是错的,正式的10组测试数据中也有7组是错的(特别感谢sms123提供的数据)。这里以样例数据为例说明:
| 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 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 2 | 0 | ① | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | 1 | 2 | ❶ | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | 1 | 1 | 1 | 2 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | ❷ | 1 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 2 | 2 | 0 | 1 | 1 | 0 | 2 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 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 |
样例输出的答案有遗漏,这个局面黑子落在7 11位置也是3步必胜的(图中❶所示)。黑子落在7 11位置则白棋必须从6 12位置堵这个冲四(图中①所示),然后黑第二步落在10 11位置(图中❷所示),第三步黑棋8 11和10 9两个点必得其一,故第三步黑必能成5。
合理的算法:
这题的解法主体思路应该是搜索,规则判断仅作为辅助。这是因为靠规则判断获胜的条件随着步数的增加变得难以想象的复杂(指的是无遗漏的正确判断哦),而加入极其繁琐的规则达成的结果可能仅仅相当于多搜一步,所以合理的解决思路应着重于优化搜索的速度。辅以少量的规则判断,减少搜索所需的层数。
搜索算法一个合适的框架是迭代加深,因为题目要求我们找到最少获胜步数,每次搜索直接限制这个步数,迭代加深正好能得到答案。
一些优化:
哈希表:避免搜索重复局面必须的,棋类游戏常用的哈希方法使用一张随机数表,按子的分布情况异或得到哈希值。但随着迭代加深越搜越深,有些之前的哈希记录深度不够不能使用,只能重搜。
冲四的判断:如果此时棋盘上有个点,某一方在这里落子能立刻成五,这种情况是冲四(这个定义可能跟专业的五子棋说法有点不同)。若轮我们走棋我们有这样的点则我们立刻获胜,而若是对手有这样的点我们必须立刻堵这个点,而不能考虑其他走法。若对手有两个以上这样的点我们应该认输。利用冲四判断可以少搜一步达到相同的效果,冲四这个规则还不算太复杂。
距离过远的剪枝:主观感觉不应该再远离战场的地方去走棋,就算可能赢那也应该不是最优走法。所以判断了落子点的两格之内应该要有子,无论是自己的子还是对方的子。但这个剪枝的正确性并未证明,欢迎补充证明或提出反例。
其它优化:
以下两个优化我的代码并未实现,真要实现还需很多行。
搜索时按局面优劣排序:可以很抽象的给局面评估一个分数,先搜索得分优秀的局面。这个评分质量无关于程序的正确性,只关于程序的速度。这是因为alpha-beta剪枝仍然存在,只是区间太窄了退化为单个bool的判断,这个排序加强了alpha-beta剪枝的效果。关于alpha-beta剪枝的原理可以查阅其它资料。
活三的判断:前面的冲四判断其实就是在用规则判断下一步必胜的情况,而活三相当于在冲四的基础上用规则多判断一步达到两步,若要正确实现两步棋的必胜判断,对这种情况可行的定义可能是“如果放任不管,对方补一手可以使局面出现两个对方可以成五的落子点。也即下一手棋对方可以一下冲两个四”(这个定义跟专业五子棋的说法非常不同,但请注意其实传统的活四也可以看作是在两头的两个点同时冲四。所以这样定义最能避免遗漏获胜情况)。面对活三也不一定要防御,可以走连续冲四获胜。其实活三已经有点复杂了,活三还仅仅是“两步棋获胜”的规则判断而已,所以还是建议重点做搜索而不是做规则。
特别感谢:
特别感谢sms123提供的数据,有这些数据才敢判断此题的真实情况。
附上代码,代码很水,也并不能AC此题,仅供参考或娱乐,只能搜5步以内的答案(第8组测试数据正确答案是6步,要搜非常久)。如果想知道代码的输出结果具体是怎么走,可以把局面改一步再输进去模拟跟它走棋。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
typedef unsigned long long U64;
char board[MAXN];
bool in_stack[MAXN];
int g_deep_limit;
int near_stack[MAXN], save_stack[MAXN];
int g_near_cnt, g_save_cnt;
U64 g_hash, save_hash[MAXN];
U64 hash_data[MAXN][2];
const int MOVE_DET[4] = {1, 15, 16, 17};
const int HSIZE = (1 << 20);
const U64 LMASK = (HSIZE - 1ull);
const U64 HMASK = ~LMASK;
const U64 H16MASK = ~((1ull << 16) - 1ull);
const int BLACK_1 = 1;
const int WHITE_2 = 2;
const int MULTI_POINT_NEG_1 = -1;
U64 hash_table[HSIZE];
int black_win_point, white_win_point;
void insert_hash(int lower_black_win, int upper_black_win)
{
U64 h = (g_hash & H16MASK);
h |= (lower_black_win & 255ull);
h |= ((upper_black_win & 255ull) << 8);
hash_table[g_hash & LMASK] = h;
}
void get_hash(int &lower_black_win, int &upper_black_win)
{
static U64 hit_times = 0, query_times = 0;
U64 h = hash_table[g_hash & LMASK];
if ((h & HMASK) == (g_hash & HMASK))
{
lower_black_win = (h & 255ull);
upper_black_win = ((h >> 8) & 255ull);
++hit_times;
}
else
{
lower_black_win = 0;
upper_black_win = 255;
}
if (++query_times % 100000 == 0)
{
// printf("hash hit %lld / %lld\n", hit_times, query_times);
}
}
void update_stack(int pos)
{
int y = pos / 16, x = pos % 16;
int dy, dx, dpos;
for (dy = max(y - 2, 1); dy <= min(y + 2, 15); dy++)
for (dx = max(x - 2, 1); dx <= min(x + 2, 15); dx++)
{
dpos = 16 * dy + dx;
if (!in_stack[dpos])
{
near_stack[g_near_cnt++] = dpos;
in_stack[dpos] = true;
}
}
}
void set_win_point(int pos, int col)
{
if (col == BLACK_1)
{
if (black_win_point == 0)
black_win_point = pos;
else
black_win_point = MULTI_POINT_NEG_1;
}
else
{
if (white_win_point == 0)
white_win_point = pos;
else
white_win_point = MULTI_POINT_NEG_1;
}
}
void update_win_point(int pos, int col)
{
for (int i = 0; i < 4; i++)
{
int left = -1, right = 1;
while (board[pos + MOVE_DET[i] * left] == col)
left--;
while (board[pos + MOVE_DET[i] * right] == col)
right++;
if (board[pos + MOVE_DET[i] * left] == 0)
{
int left2 = left - 1;
while (board[pos + MOVE_DET[i] * left2] == col)
left2--;
if (right - left2 >= 6)
{
set_win_point(pos + MOVE_DET[i] * left, col);
}
}
if (board[pos + MOVE_DET[i] * right] == 0)
{
int right2 = right + 1;
while (board[pos + MOVE_DET[i] * right2] == col)
right2++;
if (right2 - left >= 6)
{
set_win_point(pos + MOVE_DET[i] * right, col);
}
}
}
}
bool is_win_point(int pos, int col)
{
for (int i = 0; i < 4; i++)
{
int left = -1, right = 1;
while (board[pos + MOVE_DET[i] * left] == col)
left--;
while (board[pos + MOVE_DET[i] * right] == col)
right++;
if (right - left >= 6)
return true;
}
return false;
}
void do_move(int pos, int col)
{
save_stack[g_save_cnt] = g_near_cnt;
save_hash[g_save_cnt] = g_hash;
g_save_cnt++;
board[pos] = col;
if (col == BLACK_1 && white_win_point == pos)
white_win_point = 0;
else if (col == WHITE_2 && black_win_point == pos)
black_win_point = 0;
g_hash ^= hash_data[pos][col - 1];
update_stack(pos);
update_win_point(pos, col);
}
void undo_move(int pos)
{
g_save_cnt--;
g_hash = save_hash[g_save_cnt];
if (board[pos] == 1)
black_win_point = 0;
else
white_win_point = 0;
board[pos] = 0;
if (is_win_point(pos, BLACK_1))
set_win_point(pos, BLACK_1);
if (is_win_point(pos, WHITE_2))
set_win_point(pos, WHITE_2);
while (g_near_cnt > save_stack[g_save_cnt])
{
int pos = near_stack[--g_near_cnt];
in_stack[pos] = false;
}
}
bool search_white_lose(int deep);
bool search_black_win(int deep)
{
if (black_win_point != 0)
return true;
if (white_win_point == MULTI_POINT_NEG_1)
return false;
if (deep + 1 >= g_deep_limit)
return false;
int lower_black_win, upper_black_win;
get_hash(lower_black_win, upper_black_win);
if (lower_black_win >= g_deep_limit)
return false;
if (upper_black_win < g_deep_limit)
return true;
bool is_win = false;
for (int i = 0; i < g_near_cnt; i++)
{
int pos = near_stack[i];
if (board[pos] != 0)
continue;
if (white_win_point != 0 && pos != white_win_point)
continue;
do_move(pos, BLACK_1);
if (!is_win)
{
is_win = search_white_lose(deep + 1);
}
undo_move(pos);
if (is_win)
break;
}
if (is_win)
upper_black_win = g_deep_limit - 1;
else
lower_black_win = g_deep_limit;
insert_hash(lower_black_win, upper_black_win);
return is_win;
}
bool search_white_lose(int deep)
{
if (deep >= g_deep_limit)
return false;
if (white_win_point != 0)
return false;
if (black_win_point == MULTI_POINT_NEG_1)
return true;
if (black_win_point == 0 && deep + 1 >= g_deep_limit)
return false;
int lower_black_win, upper_black_win;
get_hash(lower_black_win, upper_black_win);
if (lower_black_win >= g_deep_limit)
return false;
if (upper_black_win < g_deep_limit)
return true;
bool is_white_win = false;
for (int i = 0; i < g_near_cnt; i++)
{
int pos = near_stack[i];
if (board[pos] != 0)
continue;
if (black_win_point != 0 && pos != black_win_point)
continue;
do_move(pos, WHITE_2);
if (!is_white_win)
{
is_white_win = !search_black_win(deep);
}
undo_move(pos);
if (is_white_win)
break;
}
if (is_white_win)
lower_black_win = g_deep_limit;
else
upper_black_win = g_deep_limit - 1;
insert_hash(lower_black_win, upper_black_win);
return !is_white_win;
}
int main()
{
int i, j, y, x, c;
srand(11827);
for (i = 0; i < MAXN; i++)
{
for (j = 0; j < 10; j++)
{
hash_data[i][j % 2] <<= 13;
hash_data[i][j % 2] ^= rand();
}
}
// printf("%llX %llX\n", hash_data[140][0], hash_data[220][1]);
memset(board, '#', sizeof(board));
g_hash = g_near_cnt = g_save_cnt = 0;
for (y = 1; y <= 15; y++)
for (x = 1; x <= 15; x++)
{
int pos = y * 16 + x;
scanf("%d", &c);
board[pos] = c;
if (c != 0)
{
g_hash ^= hash_data[pos][c - 1];
update_stack(pos);
}
}
int stime = clock();
int res[MAXN], rcnt = 0;
g_deep_limit = 1;
for (i = 0; i < g_near_cnt; i++)
{
int pos = near_stack[i];
if (board[pos] == 0)
{
if (is_win_point(pos, BLACK_1))
{
res[rcnt++] = pos;
set_win_point(pos, BLACK_1);
}
if (is_win_point(pos, WHITE_2))
{
set_win_point(pos, WHITE_2);
}
}
}
if (rcnt == 0)
{
while (!search_black_win(0))
{
// printf("search failed limit=%d\n", g_deep_limit);
g_deep_limit++;
}
for (i = 0; i < g_near_cnt; i++)
{
int pos = near_stack[i];
if (board[pos] == 0)
{
do_move(pos, BLACK_1);
if (search_white_lose(1))
{
res[rcnt++] = pos;
}
undo_move(pos);
}
}
}
printf("%d %d\n", g_deep_limit, rcnt);
for (i = 0; i < rcnt; i++)
{
printf("%d %d\n", res[i] / 16, res[i] % 16);
}
// printf("time = %d ms\n",clock()-stime);
return 0;
}