C++#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int N = 16;
const int MAX_DEP = 5;
const int INF = 0x3f3f3f3f;
int g[N][N];
// 四向判五连
int dx[] = {0, 1, 1, 1};
int dy[] = {1, 0, 1, -1};
// 八邻域找候选点
int adx[] = {-1,-1,-1,0,0,1,1,1};
int ady[] = {-1,0,1,-1,1,-1,0,1};
// 双哈希结构体 + 自定义哈希函数
struct HashKey {
ull h1, h2;
bool operator==(const HashKey& o) const {
return h1 == o.h1 && h2 == o.h2;
}
};
struct HashFunc {
size_t operator()(const HashKey& k) const {
return k.h1 ^ (k.h2 << 13);
}
};
unordered_map<HashKey, int, HashFunc> mem;
inline int idx(int x, int y) {
return (x-1)*15 + (y-1);
}
// 快速判落子后是否五连
bool isWin(int x, int y, int col) {
for (int d = 0; d < 4; d++) {
int cnt = 1;
int nx = x + dx[d], ny = y + dy[d];
while (nx >= 1 && nx <=15 && ny >=1 && ny <=15 && g[nx][ny]==col) {
cnt++; nx += dx[d]; ny += dy[d];
}
nx = x - dx[d]; ny = y - dy[d];
while (nx >= 1 && nx <=15 && ny >=1 && ny <=15 && g[nx][ny]==col) {
cnt++; nx -= dx[d]; ny -= dy[d];
}
if (cnt >=5) return true;
}
return false;
}
// 全局棋盘哈希(仅开局/记忆化查询用)
HashKey getHash() {
HashKey res{0,0};
for(int i=1;i<=15;i++){
for(int j=1;j<=15;j++){
int v = g[i][j];
res.h1 = res.h1 * 911382629 + v;
res.h2 = res.h2 * 3571428571ULL + v;
}
}
return res;
}
// 检查当前棋盘是否终局:0无胜负,1黑胜,2白胜
int checkEnd() {
for(int i=1;i<=15;i++){
for(int j=1;j<=15;j++){
if(g[i][j]==0) continue;
if(isWin(i,j,g[i][j])) return g[i][j];
}
}
return 0;
}
// 获取候选落子点(仅棋子八邻域,兜底全空)
vector<pair<int,int>> getCand() {
vector<pair<int,int>> res;
bool vis[N][N]={0}, hasChess=0;
for(int i=1;i<=15;i++){
for(int j=1;j<=15;j++){
if(g[i][j]==0) continue;
hasChess=1;
for(int d=0;d<8;d++){
int nx=i+adx[d], ny=j+ady[d];
if(nx<1||nx>15||ny<1||ny>15) continue;
if(g[nx][ny]==0 && !vis[nx][ny]){
vis[nx][ny]=1;
res.emplace_back(nx,ny);
}
}
}
}
if(!hasChess || res.empty()){
res.clear();
for(int i=1;i<=15;i++)
for(int j=1;j<=15;j++)
if(g[i][j]==0) res.emplace_back(i,j);
}
return res;
}
// αβ剪枝 dfs
// turn=1黑(max) turn=2白(min)
// 返回:当前局面黑最少需要几颗子;-1不可胜
int dfs(int turn, int dep, int alpha, int beta) {
if(dep > MAX_DEP) return -1;
HashKey h = getHash();
if(mem.count(h)) return mem[h];
int end = checkEnd();
if(end == 1) return mem[h] = 0;
if(end == 2) return mem[h] = -1;
vector<pair<int,int>> pts = getCand();
if(turn == 1) {
// 黑方 MAX,找最小步数
int best = INF;
for(auto &p : pts) {
int x=p.first, y=p.second;
g[x][y] = 1;
int sub = dfs(2, dep+1, alpha, beta);
g[x][y] = 0;
if(sub == -1) continue;
best = min(best, sub + 1);
alpha = max(alpha, best);
if(alpha >= beta) break; // αβ剪枝
}
if(best == INF) return mem[h] = -1;
return mem[h] = best;
} else {
// 白方 MIN,只要有一个阻挡让黑无解直接返回-1
int worst = 0;
for(auto &p : pts) {
int x=p.first, y=p.second;
g[x][y] = 2;
int sub = dfs(1, dep+1, alpha, beta);
g[x][y] = 0;
if(sub == -1) {
return mem[h] = -1;
}
worst = max(worst, sub);
beta = min(beta, worst);
if(alpha >= beta) break; // αβ剪枝
}
return mem[h] = worst;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for(int i=1;i<=15;i++)
for(int j=1;j<=15;j++)
cin >> g[i][j];
int endSt = checkEnd();
if(endSt == 1) {
cout << "0 0\n";
return 0;
}
if(endSt == 2) {
// 白已胜利,黑无解
return 0;
}
int minA = dfs(1, 0, -INF, INF);
vector<pair<int,int>> ans;
// 枚举所有空位收集合法第一步
for(int i=1;i<=15;i++){
for(int j=1;j<=15;j++){
if(g[i][j] != 0) continue;
g[i][j] = 1;
int res = dfs(2, 1, -INF, INF);
g[i][j] = 0;
if(res == minA) ans.emplace_back(i,j);
}
}
// 按15*i+j升序
sort(ans.begin(), ans.end(), [](pair<int,int>a,pair<int,int>b){
int va = a.first * 15 + a.second;
int vb = b.first * 15 + b.second;
return va < vb;
});
cout << minA << " " << ans.size() << "\n";
for(auto p : ans) cout << p.first << " " << p.second << "\n";
return 0;
}
求管理员通过