#include <iostream> #include <string> #include <cstdlib> #include <ctime> using…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
#include #include #include #include
using namespace std;
int currBotColor; // 我所执子颜色(1为黑,-1为白,棋盘状态亦同) int gridInfo[7][7] = { 0 }; // 先x后y,记录棋盘状态 int blackPieceCount = 2, whitePieceCount = 2; int startX, startY, resultX, resultY; static int delta[24][2] = { { 1,1 },{ 0,1 },{ -1,1 },{ -1,0 }, { -1,-1 },{ 0,-1 },{ 1,-1 },{ 1,0 }, { 2,0 },{ 2,1 },{ 2,2 },{ 1,2 }, { 0,2 },{ -1,2 },{ -2,2 },{ -2,1 }, { -2,0 },{ -2,-1 },{ -2,-2 },{ -1,-2 }, { 0,-2 },{ 1,-2 },{ 2,-2 },{ 2,-1 } };
// 判断是否在地图内 inline bool inMap(int x, int y) { if (x < 0 || x > 6 || y < 0 || y > 6) return false; return true; }
// 向Direction方向改动坐标,并返回是否越界 inline bool MoveStep(int& x, int& y, int Direction) { x = x + delta[Direction][0]; y = y + delta[Direction][1]; return inMap(x, y); }
// 在坐标处落子,检查是否合法或模拟落子 bool ProcStep(int x0, int y0, int x1, int y1, int color) { if (color == 0) return false; if (x1 == -1) // 无路可走,跳过此回合 return true; if (!inMap(x0, y0) || !inMap(x1, y1)) // 超出边界 return false; if (gridInfo[x0][y0] != color) return false; int dx, dy, x, y, currCount = 0, dir; int effectivePoints[8][2]; dx = abs((x0 - x1)), dy = abs((y0 - y1)); if ((dx == 0 && dy == 0) || dx > 2 || dy > 2) // 保证不会移动到原来位置,而且移动始终在5×5区域内 return false; if (gridInfo[x1][y1] != 0) // 保证移动到的位置为空 return false; if (dx == 2 || dy == 2) // 如果走的是5×5的外围,则不是复制粘贴 gridInfo[x0][y0] = 0; else { if (color == 1) blackPieceCount++; else whitePieceCount++; }
gridInfo[x1][y1] = color;
for (dir = 0; dir < 8; dir++) // 影响邻近8个位置
{
x = x1 + delta[dir][0];
y = y1 + delta[dir][1];
if (!inMap(x, y))
continue;
if (gridInfo[x][y] == -color) {
effectivePoints[currCount][0] = x;
effectivePoints[currCount][1] = y;
currCount++;
gridInfo[x][y] = color;
}
}
if (currCount != 0) {
if (color == 1) {
blackPieceCount += currCount;
whitePieceCount -= currCount;
}
else {
whitePieceCount += currCount;
blackPieceCount -= currCount;
}
}
return true;
} int EvaluateBoard() { return currBotColor == 1 ? blackPieceCount - whitePieceCount : whitePieceCount - blackPieceCount; } void CopyBoard(int src[7][7], int dest[7][7]) { for (int i = 0; i < 7; ++i) for (int j = 0; j < 7; ++j) dest[i][j] = src[i][j]; } void GenerateMoves(int color, int moves[][4], int& moveCount) { moveCount = 0; for (int y0 = 0; y0 < 7; y0++) { for (int x0 = 0; x0 < 7; x0++) { if (gridInfo[x0][y0] != color) continue; for (int dir = 0; dir < 24; dir++) { int x1 = x0 + delta[dir][0]; int y1 = y0 + delta[dir][1]; if (!inMap(x1, y1)) continue; if (gridInfo[x1][y1] != 0) continue; moves[moveCount][0] = x0; moves[moveCount][1] = y0; moves[moveCount][2] = x1; moves[moveCount][3] = y1; moveCount++; } } } }
int AlphaBeta(int depth, int alpha, int beta, bool isMax, int color) { if (depth == 0) { return EvaluateBoard(); }
int moves[100][4], moveCount = 0;
GenerateMoves(color, moves, moveCount);
if (moveCount == 0)
return EvaluateBoard(); // 无路可走,返回当前估值
int backupGrid[7][7];
int backupBlack = blackPieceCount;
int backupWhite = whitePieceCount;
if (isMax) {
int maxEval = -100000;
for (int i = 0; i < moveCount; i++) {
int x0 = moves[i][0], y0 = moves[i][1];
int x1 = moves[i][2], y1 = moves[i][3];
CopyBoard(gridInfo, backupGrid);
int tempBlack = blackPieceCount;
int tempWhite = whitePieceCount;
ProcStep(x0, y0, x1, y1, color);
int eval = AlphaBeta(depth - 1, alpha, beta, false, -color);
maxEval = max(maxEval, eval);
alpha = max(alpha, eval);
// 恢复现场
CopyBoard(backupGrid, gridInfo);
blackPieceCount = tempBlack;
whitePieceCount = tempWhite;
if (beta <= alpha)
break; // β剪枝
}
return maxEval;
}
else {
int minEval = 100000;
for (int i = 0; i < moveCount; i++) {
int x0 = moves[i][0], y0 = moves[i][1];
int x1 = moves[i][2], y1 = moves[i][3];
CopyBoard(gridInfo, backupGrid);
int tempBlack = blackPieceCount;
int tempWhite = whitePieceCount;
ProcStep(x0, y0, x1, y1, color);
int eval = AlphaBeta(depth - 1, alpha, beta, true, -color);
minEval = min(minEval, eval);
beta = min(beta, eval);
// 恢复现场
CopyBoard(backupGrid, gridInfo);
blackPieceCount = tempBlack;
whitePieceCount = tempWhite;
if (beta <= alpha)
break; // α剪枝
}
return minEval;
}
} void GetBestMove(int& nextX, int& nextY, int& nextX1, int& nextY1) { int moves[100][4], moveCount = 0; GenerateMoves(currBotColor, moves, moveCount);
int bestScore = -100000;
int backupGrid[7][7];
int backupBlack = blackPieceCount;
int backupWhite = whitePieceCount;
for (int i = 0; i < moveCount; i++) {
int x0 = moves[i][0], y0 = moves[i][1];
int x1 = moves[i][2], y1 = moves[i][3];
CopyBoard(gridInfo, backupGrid);
int tempBlack = blackPieceCount;
int tempWhite = whitePieceCount;
ProcStep(x0, y0, x1, y1, currBotColor);
// alpha = -∞, beta = +∞,从我方角度开始(极大层)
int score = AlphaBeta(2, -100000, 100000, false, -currBotColor); // 可调节搜索深度
if (score > bestScore) {
bestScore = score;
nextX = x0;
nextY = y0;
nextX1 = x1;
nextY1 = y1;
}
// 恢复现场
CopyBoard(backupGrid, gridInfo);
blackPieceCount = tempBlack;
whitePieceCount = tempWhite;
}
} int main() { istream::sync_with_stdio(false);
int x0, y0, x1, y1;
// 初始化棋盘
gridInfo[0][0] = gridInfo[6][6] = 1; //黑
gridInfo[6][0] = gridInfo[0][6] = -1; //白
// 分析自己收到的输入和自己过往的输出,并恢复状态
int turnID;
currBotColor = -1; // 假定我方是白方
cin >> turnID;
for (int i = 0; i < turnID - 1; i++) {
// 根据这些输入输出逐渐恢复状态到当前回合
cin >> x0 >> y0 >> x1 >> y1;
if (x1 >= 0)
ProcStep(x0, y0, x1, y1, -currBotColor); // 模拟对方落子
else
currBotColor = 1; // 第一回合收到坐标是-1, -1,-1, -1说明我是黑方
cin >> x0 >> y0 >> x1 >> y1;
if (x1 >= 0)
ProcStep(x0, y0, x1, y1, currBotColor); // 模拟己方落子
}
// 看看自己本回合输入,即对方上一轮的决策结果
cin >> x0 >> y0 >> x1 >> y1;
if (x1 >= 0)
ProcStep(x0, y0, x1, y1, -currBotColor); // 模拟对方落子
else
currBotColor = 1;
//此时gridInfo[][]里存储的就是当前棋盘的所有棋子信息
// 找出合法落子点
int beginPos[1000][2], possiblePos[1000][2], posCount = 0, choice, dir;
// 看我方可否将(x0,y0)的棋子移动至(x1,y1)
for (y0 = 0; y0 < 7; y0++)
for (x0 = 0; x0 < 7; x0++) {
if (gridInfo[x0][y0] != currBotColor) //若(x0,y0)不是我方的棋子,则忽略
continue;
for (dir = 0; dir < 24; dir++) {
x1 = x0 + delta[dir][0]; //考察可能的落子点(x1,y1)
y1 = y0 + delta[dir][1];
if (!inMap(x1, y1)) //若(x1,y1)不在棋盘里,则不能落子
continue;
if (gridInfo[x1][y1] != 0) //若(x1,y1)不是空位,则不能落子
continue;
beginPos[posCount][0] = x0;
beginPos[posCount][1] = y0;
possiblePos[posCount][0] = x1;
possiblePos[posCount][1] = y1;
posCount++;
}
}
//上述循环结束后,beginPos存的是可取的合法棋子位置,possiblePos的是合法的落子位置
// 做出决策
int nextX = -1, nextY = -1, nextX1 = -1, nextY1 = -1;
if (posCount > 0) {
GetBestMove(nextX, nextY, nextX1, nextY1);
startX = nextX;
startY = nextY;
resultX = nextX1;
resultY = nextY1;
}
else {
startX = -1;
startY = -1;
resultX = -1;
resultY = -1;
}
// 决策结束,输出结果(你只需修改以上部分)
//将我方(startX,startY)的棋子移动至(resultX,resultY)
cout << startX << " " << startY << " " << resultX << " " << resultY;
return 0;
} 程序运行会崩溃使用中文回复
您的程序在运行时崩溃,可能是由于以下原因:
数组越界访问:在 ProcStep 函数中,effectivePoints 数组被定义为 int effectivePoints[8][2],但在循环中可能会向该数组写入多于 8 个元素,导致数组越界访问。
未初始化的变量:在 main 函数中,turnID 变量未初始化,可能导致未定义的行为。
输入处理错误:在读取输入时,未检查输入的有效性,可能导致程序崩溃。
为了解决这些问题,建议您:
检查数组访问:确保在向 effectivePoints 数组写入数据时,不会超过其定义的大小。
初始化变量:在使用变量之前,确保它们已被正确初始化。
验证输入:在读取输入时,添加适当的检查,确保输入数据的有效性。
通过以上步骤,您应该能够解决程序崩溃的问题。