27
社区成员
发帖
与我相关
我的任务
分享
struct node {
int x, y, s, t;
}w[101];
int map[11][11];
int tot; //用于表示棋盘中己方棋子数。
int dx[] = { -1,-1,1,1 }, dy[] = { -1,1,-1,1 };
bool vis[11][11]; //当完整连跳未完成,被"拿掉"的子的vis设为1
int cnt, num; //cnt为最大吃子数,num为合理走法数
int wc; //同cnt,表示最大吃子数
#include <iostream>
#include <cstring>
#include<stack>
#include<queue>
using namespace std;
// 声明一个结构体类型 Node表示棋子,属性为x,y坐标
struct Node
{
int x; //横坐标
int y; //纵坐标
};
struct NodeStep
{
Node ndstt; //起点
Node ndend; //终点
Node ndeat; //吃子
int seq;
};
typedef stack<NodeStep> StepStack; //走法栈
typedef deque<NodeStep> StepQueue; //走法队列
typedef queue<StepQueue> QStepQueue; //走法队列的队列,用于保存候选招法或是移动走法
//常量二维数组, 其中和棋盘格对应坐标存储的是编号1到50
int NOArray[10][10] = { {0,1,0,2,0,3,0,4,0,5},
{6,0,7,0,8,0,9,0,10,0},
{0,11,0,12,0,13,0,14,0,15},
{16,0,17,0,18,0,19,0,20,0},
{0,21,0,22,0,23,0,24,0,25},
{26,0,27,0,28,0,29,0,30,0},
{0,31,0,32,0,33,0,34,0,35},
{36,0,37,0,38,0,39,0,40,0},
{0,41,0,42,0,43,0,44,0,45},
{46,0,47,0,48,0,49,0,50,0} };
//棋子数据,其中保存四种类型的值:0:空,1:白兵,2:白王,3:黑棋子
int ChessArray[10][10] = { {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,3,0,3,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,3,0,3,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,3,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
int HoldArray[10][10] = { 0 };
//这里的分析只针对吃子,nd为要分析的节点
bool AnalyseNode(Node nd, StepStack &stacktmp,int seq)
{
//hasfound为二值变量,为真表示找到了对于当前节点的吃子走法
bool hasfound = false;
//这两个数组可用于标示方位
int xd[] = { -1,1,-1,1 };
int yd[] = { -1,-1,1,1 };
NodeStep ndstep;
if (ChessArray[nd.x][nd.y] == 1)
{
//分别对左上,左下,右上,右下判断,如果可吃子,压入节点栈
for (int i = 0; i < 4; i++)
{
if ((nd.x + 2 * xd[i]) > -1 and (nd.x + 2 * xd[i]) < 10 and (nd.y + 2 * yd[i]) > -1 and (nd.y + 2 * yd[i]) < 10)
{
if (ChessArray[nd.x + 2 * xd[i]][nd.y + 2 * yd[i]] == 0 and ChessArray[nd.x + xd[i]][nd.y + yd[i]] == 3 and HoldArray[nd.x + xd[i]][nd.y + yd[i]] == 0)
{
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
ndstep.ndend.x = nd.x + 2 * xd[i];
ndstep.ndend.y = nd.y + 2 * yd[i];
ndstep.ndeat.x = nd.x + xd[i];
ndstep.ndeat.y = nd.y + yd[i];
ndstep.seq = seq;
stacktmp.push(ndstep);
hasfound = true;
}
}
}
}
else if (ChessArray[nd.x][nd.y] == 2)
{
Node ndtmp;
//分别对左上,左下,右上,右下判断,如果可吃子,压入走法栈
for (int i = 0; i < 4; i++)
{
ndtmp.x = nd.x+xd[i];
ndtmp.y = nd.y+yd[i];
//定义状态变量canloc表示是否可以落子,初始为false;
bool canloc = false;
//将起始位置记录在步法ndstep中
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
//先跳过空格
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndtmp.x += xd[i];
ndtmp.y += yd[i];
continue;
}
if (ChessArray[ndtmp.x][ndtmp.y] == 1 or ChessArray[ndtmp.x][ndtmp.y] == 2)
break;
if (ChessArray[ndtmp.x][ndtmp.y] == 3 and HoldArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndeat.x = ndtmp.x;
ndstep.ndeat.y = ndtmp.y;
canloc = true;
ndtmp.x += xd[i], ndtmp.y += yd[i];
break;
}
else
break;
}
if (canloc)
{
//在跳过对方一个棋子后,所有空格都是可行步,如果其后的棋子不为空或者
//超出边界,不会将该走法压入走法栈
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndend.x = ndtmp.x;
ndstep.ndend.y = ndtmp.y;
stacktmp.push(ndstep);
hasfound = true;
ndtmp.x += xd[i];
ndtmp.y += yd[i];
}
else
break;
}
}
}
}
return hasfound;
}
//无子可吃时,将不吃子走法压入栈
void Move(Node nd, StepQueue &stacktmp)
{
//这两个数组可用于标示方位
int xd[] = { -1,1,-1,1 };
int yd[] = { -1,-1,1,1 };
NodeStep ndstep;
if (ChessArray[nd.x][nd.y] == 1)
{
//分别对左上,左下,右上,右下判断,如果为空,压入节点栈
for (int i = 0; i < 4; i+=2)
{
if ((nd.x + xd[i]) > -1 and (nd.x + xd[i]) < 10 and (nd.y + yd[i]) > -1 and (nd.y + yd[i]) < 10)
{
if (ChessArray[nd.x + xd[i]][nd.y + yd[i]] == 0)
{
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
ndstep.ndend.x = nd.x + xd[i];
ndstep.ndend.y = nd.y + yd[i];
stacktmp.push_back(ndstep);
}
}
}
}
else if (ChessArray[nd.x][nd.y] == 2)
{
Node ndtmp;
//分别对左上,左下,右上,右下判断,如果为空,压入走法栈
for (int i = 0; i < 4; i++)
{
ndtmp.x = nd.x + xd[i];
ndtmp.y = nd.y + yd[i];
//将起始位置记录在步法ndstep中
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
//为空格时压入栈
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndend.x = ndtmp.x;
ndstep.ndend.y = ndtmp.y;
stacktmp.push_back(ndstep);
ndtmp.x += xd[i];
ndtmp.y += yd[i];
continue;
}
else
break;
}
}
}
}
//主函数
int main()
{
QStepQueue ways_eat; //保存候选招法和移动走法的变量
StepQueue ways_move;
StepStack stepwait; //未完成走法栈
StepQueue stepfinish; //已完成走法队列
Node nodecur; //定义当前节点变量
NodeStep stepcur; //定义当前走法
int n = 0;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (ChessArray[x][y] == 1 or ChessArray[x][y] == 2)
{
//为根节点赋值
nodecur.x = x;
nodecur.y = y;
//清空栈和队列
while (!stepwait.empty())
stepwait.pop();
stepfinish.clear();
//分析可行走法,如果存在吃子走法,将当前可行吃子走法压入stepwait
//首先判断吃子走法,因为如果候选招法不为空的话,根据有吃必吃,没有必要搜索移动的走法
while (true)
{
if (AnalyseNode(nodecur, stepwait,++n))
{
//从未走走法栈中选择一个执行,并将走到的节点处走法压入未走走法栈
stepcur = stepwait.top();
stepwait.pop();
//已被执行的走法,相应吃子位置为真,存入已完成走法队列
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 1;
stepfinish.push_back(stepcur);
//节点更新为走法的终点位置
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = (ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = 0;
///HoldArray[stepcur.ndend.x][stepcur.ndend.y] = 1;
nodecur = stepcur.ndend;
}
else
{
//如果ways_eat中的走法队列长度小于stepfinish,清空ways_eat中的
//走法队列,将stepfinish存入,如果ways_eat中的走法队列长度等于
//stepfinish,将stepfinish加入ways_eat
if (ways_eat.empty() and !(stepfinish.empty()))
ways_eat.push(stepfinish);
else if (!ways_eat.empty())
{
if (ways_eat.front().size() < stepfinish.size())
{
while (!ways_eat.empty())
ways_eat.pop();
ways_eat.push(stepfinish);
}
else if ( ways_eat.front().size() == stepfinish.size())
ways_eat.push(stepfinish);
}
//回溯至stepwait中的可行走法
while (!stepfinish.empty())
{
stepcur = stepfinish.back();
stepfinish.pop_back();
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 0;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = (ChessArray[stepcur.ndend.x][stepcur.ndend.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = 0;
//找到分支走法
if (!stepwait.empty() and stepcur.ndstt.x == stepwait.top().ndstt.x and stepcur.ndstt.y == stepwait.top().ndstt.y and stepcur.seq == stepwait.top().seq)
{
stepcur = stepwait.top();
stepwait.pop();
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 1;
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = (ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = 0;
stepfinish.push_back(stepcur);
nodecur = stepcur.ndend;
break;
}
}
if (stepwait.empty() and stepfinish.empty())
break;
}
}
}
}
}
cout << ways_eat.empty() << endl;
n = 0;
if (ways_eat.empty())
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (ChessArray[x][y] == 1 or ChessArray[x][y] == 2)
{
//为根节点赋值
nodecur.x = x;
nodecur.y = y;
Move(nodecur, ways_move);
}
}
}
cout << "合理不吃子走法有:" << endl;
while (!ways_move.empty())
{
cout << "\n步法" << ++n << ":" << endl;
NodeStep tempstep = ways_move.front();
ways_move.pop_front();
cout << NOArray[tempstep.ndstt.x][tempstep.ndstt.y] << "-->" << NOArray[tempstep.ndend.x][tempstep.ndend.y] << "\n";
}
}
else
{
cout << "合理吃子走法有:" << endl;
while (!ways_eat.empty())
{
cout << "\n步法" << ++n << ":" << endl;
StepQueue spqueue = ways_eat.front();
ways_eat.pop();
while (!spqueue.empty())
{
NodeStep tempstep = spqueue.front();
spqueue.pop_front();
cout << NOArray[tempstep.ndstt.x][tempstep.ndstt.y] << "-->" << NOArray[tempstep.ndend.x][tempstep.ndend.y] << "\n";
}
}
}
}
#include <iostream>
#include <cstring>
#include<stack>
#include<queue>
using namespace std;
// 声明一个结构体类型 Node表示棋子,属性为x,y坐标
struct Node
{
int x; //横坐标
int y; //纵坐标
};
struct NodeStep
{
Node ndstt; //起点
Node ndend; //终点
Node ndeat; //吃子
};
typedef stack<NodeStep> StepStack; //走法栈
typedef deque<NodeStep> StepQueue; //走法队列
typedef queue<StepQueue> QStepQueue; //走法队列的队列,用于保存候选招法或是移动走法
//常量二维数组, 其中和棋盘格对应坐标存储的是编号1到50
int NOArray[10][10] = { {0,1,0,2,0,3,0,4,0,5},
{6,0,7,0,8,0,9,0,10,0},
{0,11,0,12,0,13,0,14,0,15},
{16,0,17,0,18,0,19,0,20,0},
{0,21,0,22,0,23,0,24,0,25},
{26,0,27,0,28,0,29,0,30,0},
{0,31,0,32,0,33,0,34,0,35},
{36,0,37,0,38,0,39,0,40,0},
{0,41,0,42,0,43,0,44,0,45},
{46,0,47,0,48,0,49,0,50,0} };
//棋子数据,其中保存四种类型的值:0:空,1:白兵,2:白王,3:黑棋子
int ChessArray[10][10] = { {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,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,1,0,0},
{0,0,0,0,0,0,0,0,0,0}, };
//吃子状态二维数组,二值数组,只能有真和假两个状态,初始为0
int HoldArray[10][10] = { 0 };
//这里的分析只针对吃子,nd为要分析的节点
bool AnalyseNode(Node nd, StepStack &stacktmp)
{
//hasfound为二值变量,为真表示找到了对于当前节点的吃子走法
bool hasfound = false;
//这两个数组可用于标示方位
int xd[] = { -1,1,-1,1 };
int yd[] = { -1,-1,1,1 };
NodeStep ndstep;
if (ChessArray[nd.x][nd.y] == 1)
{
//分别对左上,左下,右上,右下判断,如果可吃子,压入节点栈
for (int i = 0; i < 4; i++)
{
if ((nd.x + 2 * xd[i]) > -1 and (nd.x + 2 * xd[i]) < 10 and (nd.y + 2 * yd[i]) > -1 and (nd.y + 2 * yd[i]) < 10)
{
if (ChessArray[nd.x + 2 * xd[i]][nd.y + 2 * yd[i]] == 0 and ChessArray[nd.x + xd[i]][nd.y + yd[i]] == 3 and HoldArray[nd.x + xd[i]][nd.y + yd[i]] == 0)
{
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
ndstep.ndend.x = nd.x + 2 * xd[i];
ndstep.ndend.y = nd.y + 2 * yd[i];
ndstep.ndeat.x = nd.x + xd[i];
ndstep.ndeat.y = nd.y + yd[i];
stacktmp.push(ndstep);
hasfound = true;
}
}
}
}
else if (ChessArray[nd.x][nd.y] == 2)
{
Node ndtmp;
//分别对左上,左下,右上,右下判断,如果可吃子,压入走法栈
for (int i = 0; i < 4; i++)
{
ndtmp.x = nd.x+xd[i];
ndtmp.y = nd.y+yd[i];
//定义状态变量canloc表示是否可以落子,初始为false;
bool canloc = false;
//将起始位置记录在步法ndstep中
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
//先跳过空格
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndtmp.x += xd[i];
ndtmp.y += yd[i];
continue;
}
if (ChessArray[ndtmp.x][ndtmp.y] == 1 or ChessArray[ndtmp.x][ndtmp.y] == 2)
break;
if (ChessArray[ndtmp.x][ndtmp.y] == 3 and HoldArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndeat.x = ndtmp.x;
ndstep.ndeat.y = ndtmp.y;
canloc = true;
ndtmp.x += xd[i], ndtmp.y += yd[i];
break;
}
else
break;
}
if (canloc)
{
//在跳过对方一个棋子后,所有空格都是可行步,如果其后的棋子不为空或者
//超出边界,不会将该走法压入走法栈
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndend.x = ndtmp.x;
ndstep.ndend.y = ndtmp.y;
stacktmp.push(ndstep);
hasfound = true;
ndtmp.x += xd[i];
ndtmp.y += yd[i];
}
else
break;
}
}
}
}
return hasfound;
}
//无子可吃时,将不吃子走法压入栈
void Move(Node nd, StepQueue &stacktmp)
{
//这两个数组可用于标示方位
int xd[] = { -1,1,-1,1 };
int yd[] = { -1,-1,1,1 };
NodeStep ndstep;
if (ChessArray[nd.x][nd.y] == 1)
{
//分别对左上,左下,右上,右下判断,如果为空,压入节点栈
for (int i = 0; i < 4; i+=2)
{
if ((nd.x + xd[i]) > -1 and (nd.x + xd[i]) < 10 and (nd.y + yd[i]) > -1 and (nd.y + yd[i]) < 10)
{
if (ChessArray[nd.x + xd[i]][nd.y + yd[i]] == 0)
{
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
ndstep.ndend.x = nd.x + xd[i];
ndstep.ndend.y = nd.y + yd[i];
stacktmp.push_back(ndstep);
}
}
}
}
else if (ChessArray[nd.x][nd.y] == 2)
{
Node ndtmp;
//分别对左上,左下,右上,右下判断,如果为空,压入走法栈
for (int i = 0; i < 4; i++)
{
ndtmp.x = nd.x + xd[i];
ndtmp.y = nd.y + yd[i];
//将起始位置记录在步法ndstep中
ndstep.ndstt.x = nd.x;
ndstep.ndstt.y = nd.y;
//为空格时压入栈
while (ndtmp.x > -1 and ndtmp.x<10 and ndtmp.y>-1 and ndtmp.y < 10)
{
if (ChessArray[ndtmp.x][ndtmp.y] == 0)
{
ndstep.ndend.x = ndtmp.x;
ndstep.ndend.y = ndtmp.y;
stacktmp.push_back(ndstep);
ndtmp.x += xd[i];
ndtmp.y += yd[i];
continue;
}
else
break;
}
}
}
}
//主函数
int main()
{
QStepQueue ways_eat; //保存候选招法和移动走法的变量
StepQueue ways_move;
StepStack stepwait; //未完成走法栈
StepQueue stepfinish; //已完成走法队列
Node nodecur; //定义当前节点变量
NodeStep stepcur; //定义当前走法
int n = 0;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if ((ChessArray[x][y] == 1 or ChessArray[x][y] == 2) and HoldArray[x][y] == 0)
{
//为根节点赋值
nodecur.x = x;
nodecur.y = y;
//清空栈和队列
while (!stepwait.empty())
stepwait.pop();
stepfinish.clear();
//分析可行走法,如果存在吃子走法,将当前可行吃子走法压入stepwait
//首先判断吃子走法,因为如果候选招法不为空的话,根据有吃必吃,没有必要搜索移动的走法
while (true)
{
if (AnalyseNode(nodecur, stepwait))
{
//从未走走法栈中选择一个执行,并将走到的节点处走法压入未走走法栈
stepcur = stepwait.top();
stepwait.pop();
//已被执行的走法,相应吃子位置为真,存入已完成走法队列
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 1;
stepfinish.push_back(stepcur);
//节点更新为走法的终点位置
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = (ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = 0;
HoldArray[stepcur.ndend.x][stepcur.ndend.y] = 1;
nodecur = stepcur.ndend;
}
else
{
//如果ways_eat中的走法队列长度小于stepfinish,清空ways_eat中的
//走法队列,将stepfinish存入,如果ways_eat中的走法队列长度等于
//stepfinish,将stepfinish加入ways_eat
if (ways_eat.empty() and !(stepfinish.empty()))
ways_eat.push(stepfinish);
else if (!ways_eat.empty())
{
if (ways_eat.front().size() < stepfinish.size())
{
while (!ways_eat.empty())
ways_eat.pop();
ways_eat.push(stepfinish);
}
else if ( ways_eat.front().size() == stepfinish.size())
ways_eat.push(stepfinish);
}
//回溯至stepwait中的可行走法
while (!stepfinish.empty())
{
stepcur = stepfinish.back();
stepfinish.pop_back();
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 0;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = (ChessArray[stepcur.ndend.x][stepcur.ndend.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = 0;
//找到分支走法
if (!stepwait.empty() and stepcur.ndstt.x == stepwait.top().ndstt.x and stepcur.ndstt.y == stepwait.top().ndstt.y)
{
stepcur = stepwait.top();
stepwait.pop();
HoldArray[stepcur.ndeat.x][stepcur.ndeat.y] = 1;
ChessArray[stepcur.ndend.x][stepcur.ndend.y] = (ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] == 1) ? 1 : 2;
ChessArray[stepcur.ndstt.x][stepcur.ndstt.y] = 0;
stepfinish.push_back(stepcur);
nodecur = stepcur.ndend;
break;
}
}
if (stepwait.empty() and stepfinish.empty())
break;
}
}
}
}
}
cout << ways_eat.empty() << endl;
if (ways_eat.empty())
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (ChessArray[x][y] == 1 or ChessArray[x][y] == 2)
{
//为根节点赋值
nodecur.x = x;
nodecur.y = y;
Move(nodecur, ways_move);
}
}
}
cout << "合理不吃子走法有:" << endl;
while (!ways_move.empty())
{
cout << "\n步法" << ++n << ":" << endl;
NodeStep tempstep = ways_move.front();
ways_move.pop_front();
cout << NOArray[tempstep.ndstt.x][tempstep.ndstt.y] << "-->" << NOArray[tempstep.ndend.x][tempstep.ndend.y] << "\n";
}
}
else
{
cout << "合理吃子走法有:" << endl;
while (!ways_eat.empty())
{
cout << "\n步法" << ++n << ":" << endl;
StepQueue spqueue = ways_eat.front();
ways_eat.pop();
while (!spqueue.empty())
{
NodeStep tempstep = spqueue.front();
spqueue.pop_front();
cout << NOArray[tempstep.ndstt.x][tempstep.ndstt.y] << "-->" << NOArray[tempstep.ndend.x][tempstep.ndend.y] << "\n";
}
}
}
}
/***PS:合理走法均指可吃子最多的走法***/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int map[11][11]; //二维数组即棋子组成的棋盘
struct node {
int x, y, s, t; //x,y棋子代表坐标,s代表棋子当前所有走法中最大吃子数,t代表该棋子合理走法数
}w[101];
int tot; //用于表示棋盘中己方棋子数
int dx[] = { -1,-1,1,1 }, dy[] = { -1,1,-1,1 }; //配合出四个方向(x-1,y-1),(x-1,y+1),(x+1,y-1),(x+1,y+1)
int cnt, num; //cnt为最大吃子数,num为合理走法数
bool vis[11][11]; //当完整连吃未完成,被"拿掉"的子的vis设为1
int wc; //同cnt,表示最大吃子数
//获取最大值函数
int max(int x, int y)
{
return x > y ? x: y;
}
//dfs函数专用于王吃子,目的是更新最大吃子数cnt和合理走法数num
void dfs(int x, int y, int c, int f)
{
int flag = 0;
//printf("\n对王棋(%d,%d):\n", x, y);
if (f == 1)
wc = max(wc, c); //wc为最大吃子数,c为当前已吃子数
if (f == 2)
if (c == wc) //当c==wc时,合理步法num+1
{
cout << "**\n";
num++;
}
//在一个方向上,根据王可空格吃规则判断是否可吃子,可吃则将被吃棋子"拿出",继续递归,判断是否可连吃
for (int xx = x - 1, yy = y - 1; xx > 1 && yy > 1; xx--, yy--)
if (!flag)
{
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2) //若遇到对方棋子
{
if (!vis[xx][yy]) //不是被"吃过"的
{
for (int i = xx - 1, j = yy - 1; i >= 1 && j >= 1; i--, j--) //继续往下判断
if (map[i][j] == 0) //下一格为空
printf("--1--吃掉(%d,%d),%d ", xx, yy,f),vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0,flag++; //设为"已被吃",防止后吃;递归dfs,此时c+1
else
{
flag = 1;
break;
}
}
else break;
}
}
else
{
flag = 1; break;
}
}
else break;
for (int xx = x + 1, yy = y - 1,flag = 0; xx < 10 && yy>1; xx++, yy--)
if (!flag)
{
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx + 1, j = yy - 1; i <= 10&&j >= 1; i++, j--)
if (map[i][j] == 0)
printf("--2--吃掉(%d,%d),%d ", xx, yy, f), vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0,flag++;
else
{
flag = 1; break;
}
}
else break;
}
}
else
{
flag = 1; break;
}
}
else break;
for (int xx = x - 1, yy = y + 1,flag = 0; xx > 1 && yy < 10; xx--, yy++)
if (!flag)
{
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx - 1, j = yy + 1; i >= 1 && j <= 10; i--, j++)
if (map[i][j] == 0)
printf("--3--吃掉(%d,%d),%d ", xx, yy, f), vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0,flag++;
else break;
}
else
{
flag = 1; break;
}
}
}
else
{
flag = 1; break;
}
}
else break;
for (int xx = x + 1, yy = y + 1,flag = 0; xx < 10 && yy < 10; xx++, yy++)
if (!flag)
{
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx + 1, j = yy + 1; i <= 10 && j <= 10; i++, j++)
if (map[i][j] == 0)
printf("--4--吃掉(%d,%d),%d ", xx, yy, f), vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0,flag++;
else
{
flag = 1; break;
}
}
else break;
}
}
else
{
flag = 1; break;
}
}
else break;
return;
}
//fs函数专用于兵吃子,目的是更新最大吃子数cnt和合理走法数num
void fs(int x, int y,int c,int f)
{
//printf("\n对兵棋(%d,%d):\n", x, y);
if (f == 1)
wc = max(wc, c); //wc为最大吃子数,c为当前已吃子数
if (f == 2)
if (c == wc) //当c==wc时,合理步法num+1
num++;
for (int i = 0; i <= 3; i++) //四个方向上(顺序为左上,右上,左下,右下)判断是否可吃子,可吃子递归函数fs,判断吃子后所在位置是否可继续吃子
{
printf("\n(%d,%d)的--%d,%d方向:\n",x,y, dx[i], dy[i]);
int xx = x, yy = y;
int x1 = x + dx[i], y1 = y + dy[i];
int x2 = x + dx[i] * 2, y2 = y + dy[i] * 2;
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (x2 < 1 || y2 < 1 || x2>10 || y2>10) continue;
if ((map[x2][y2] == 0 && map[x1][y1] == 2) || (map[x2][y2]==1 && vis[x2][y2]==1 && map[x1][y1] ==2)) //连续单跳吃,连续次数为sum
{
if (!vis[x1][y1])
{
printf("\n--%d,%d方向--(%d,%d)吃掉(%d,%d),从(%d,%d)开始:\n",dx[i],dy[i],xx,yy, x1, y1,x2,y2);
vis[x1][y1] = 1; fs(x2, y2, c+1,f); vis[x1][y1] = 0;
}
}
}
return;
}
//获取(x,y)处己方棋子的合理走法数(合理走法可有多个,但满足"吃多数棋子"的规则)
void xc(int x, int y)
{
//为己方普通兵时,获取最大连续单跳吃次数cnt、合理走法数num
if (map[x][y] == 1)
{
int flag = 0;
cnt = 0, num = 0; //cnt记录最大连续单跳吃次数,num为合理走法数
//判断四个方向上是否可以单跳吃,并用flag记录(1为可单跳吃)
for (int i = 0; i <= 3; i++) //四个方向(左上,右上,左下,右下) 若有对方子&&下一格空 --> 可单跳吃(flag=1)
{
int x1 = x + dx[i], y1 = y + dy[i];
int x2 = x + dx[i] * 2, y2 = y + dy[i] * 2;
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (x2 < 1 || y2 < 1 || x2>10 || y2>10) continue;
if (map[x2][y2] == 0 && map[x1][y1] == 2)
{
flag = 1;
break;
}
}
//求出不吃子合理走法num
for (int i = 0; i <= 1; i++) //两个方向(左上,右上) 为空 --> 合理走法数num++
{
int x1 = x + dx[i], y1 = y + dy[i];
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (!map[x1][y1]) num++;
}
//不能单跳吃,结束返回不吃子走法数num
if (!flag)
{
printf("兵棋(%d,%d)的最多吃子数为:0\n", x, y);
return;
}
//可单跳吃,获取兵棋最大连续吃子数cnt和合理走法数num
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
wc = 0;
fs(x, y, 0, 1); //调用兵吃子函数fs,参数f为1时更新最大吃子数wc
num = 0;
cnt = wc; //cnt为最大连续吃子数
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
fs(x, y, 0, 2); //调用fs函数,参数f为2时更新具有最大吃子数cnt的合理步法数num
printf("兵棋(%d,%d)的最多吃子数为:%d\n", x, y, cnt);
}
//为己方王时,获取王最大连续吃子数cnt、合理走法数num
if (map[x][y] == 3)
{
int flag = 0;
cnt = 0, num = 0;
//为王时,若不吃子,获取走法数num
for (int i = 0; i <= 3; i++)
{
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || yy < 1 || xx>10 || yy>10) continue;
while (map[xx][yy] == 0)
{
num++;
xx += dx[i], yy += dy[i];
if (xx < 1 || yy < 1 || xx>10 || yy>10) break;
}
}
//为王时,若可吃子,获取最大连续吃子数cnt和合理走法数num
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
wc = 0;
dfs(x, y, 0, 1); //调用王吃子函数dfs,参数f为1时更新最大吃子数wc
if (!wc)
{
printf("\n王棋(%d,%d)的最多吃子数为:0\n", x, y);
return; //若王无可吃子走法,结束返回,num为不吃子合理走法数
}
num = 0;
cnt = wc; //cnt为最大连续吃子数
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
dfs(x, y, 0, 2); //调用dfs函数,参数f为2时更新具有最大吃子数cnt的合理步法数num
printf("\n王棋(%d,%d)的最多吃子数为:%d\n", x, y,cnt);
}
}
int main()
{
//freopen("chess.in","r",stdin);
//freopen("chess.out","w",stdout);
int chess_in1[10][10] = { {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,0,0,0,0,0},
{0,0,0,0,2,0,2,0,2,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,2,0,2,0},
{0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,1,0,0,0},
{0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0} };
int chess_in2[10][10] = { {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,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} };
char c;
cout << "棋子矩阵:(0为空,1为己方棋子,2为敌棋)\n";
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in1[i][j] << " ";
cout << "\n";
}
cout << "判断王矩阵:(0非王,1为王棋)\n";
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in2[i][j] << " ";
cout << "\n";
}
//1.第一次输入10x10矩阵,记录到map[i][j]中,0为空,1为己方棋,2为对方棋
//2.当为己方棋时,记录到w[tot],属性x,y代表坐标
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
map[i][j] = chess_in1[i-1][j-1]; //1.<--
if (map[i][j] == 1)
w[++tot].x = i, w[tot].y = j; //2.<--
}
//第二次输入10x10矩阵,当为己方棋且为王时,改map[][]为3
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
if (map[i][j] == 1 && chess_in2[i-1][j-1] == 1)
map[i][j] = 3;
}
cout << "棋盘:(0为空,1为己方兵棋,2为敌棋,3为己方王棋)\n";
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
cout << map[i][j] << " ";
cout << "\n";
}
int maxt = 0; //记录己方所有棋当中的最多吃子数
//1.遍历每一个己方棋子,利用xc函数,传入位置参数x,y,找出该棋子的最大吃子数cnt赋给s,合理走法数num赋给t
//2.记录所有己方棋子走法中吃子最多的个数
for (int i = 1; i <= tot; i++)
{
xc(w[i].x, w[i].y); //1.<--
w[i].s = cnt, w[i].t = num;
maxt = max(maxt, w[i].s); //2.<--
}
cout << "最多吃子数为:" << maxt<<endl;
//找出当前局面有ans中合理走法
int ans = 0;
for (int i = 1; i <= tot; i++)
{
if (w[i].s == maxt)
ans += w[i].t;
}
printf("合理走法数为:%d\n", ans);
//给出有合理走法的棋子坐标,若一个棋子有多个合理走法则输出多次
for (int i = 1; i <= tot && ans != 0; i++)
{
if (w[i].s == maxt)
for (int j = 1; j <= w[i].t; j++)
printf("具有合理走法棋子坐标:(%d,%d)\n", w[i].x, w[i].y);
}
return 0;
}
//freopen("chess.in","r",stdin);
//freopen("chess.out","w",stdout);
int chess_in1[10][10] = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,2,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,2,0,1,0,2,0,0,0},
{0,0,0,0,0,0,0,2,0,0},
{0,0,2,0,2,0,1,0,0,0},
{0,0,0,0,0,0,0,2,0,0},
{2,0,1,0,0,0,0,0,0,0},
{0,1,0,2,0,0,0,2,0,0},
{0,0,0,0,0,0,0,0,0,0}};
int chess_in2[10][10] = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,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,1,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}};
cout << "棋子矩阵:(0为空,1为己方棋子,2为敌棋)\n";
int i;
for (i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in1[i][j] << " ";
cout << "\n";
}
cout << "判断王矩阵:(0非王,1为王棋)\n";
for (i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in2[i][j] << " ";
cout << "\n";
}
//1.第一次输入10x10矩阵,记录到map[i][j]中,0为空,1为己方棋,2为对方棋
//2.当为己方棋时,记录到w[tot],属性x,y代表坐标
for (i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
map[i][j] = chess_in1[i-1][j-1]; //1.<--
if (map[i][j] == 1)
w[++tot].x = i, w[tot].y = j; //2.<--
}
}
//第二次输入10x10矩阵,当为己方棋且为王时,改map[][]为3
for (i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (map[i][j] == 1 && chess_in2[i-1][j-1] == 1)
map[i][j] = 3;
}
}
cout << "棋盘:(0为空,1为己方兵棋,2为敌棋,3为己方王棋)\n";
for (i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
cout << map[i][j] << " ";
cout << "\n";
}
/***PS:合理走法均指可吃子最多的走法***/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int map[11][11]; //二维数组即棋子组成的棋盘
struct node {
int x, y, s, t; //x,y棋子代表坐标,s代表棋子当前所有走法中最大吃子数,t代表该棋子合理走法数
}w[101];
int tot; //用于表示棋盘中己方棋子数
int dx[] = { -1,-1,1,1 }, dy[] = { -1,1,-1,1 }; //配合出四个方向(x-1,y-1),(x-1,y+1),(x+1,y-1),(x+1,y+1)
int cnt, num; //cnt为最大吃子数,num为合理走法数
bool vis[11][11]; //当完整连吃未完成,被"拿掉"的子的vis设为0
int wc; //同cnt,表示最大吃子数
//获取最大值函数
int max(int x, int y)
{
return x > y ? x: y;
}
//dfs函数专用于王吃子,目的是更新最大吃子数cnt和合理走法数num
void dfs(int x, int y, int c, int f)
{
//printf("\n对王棋(%d,%d):\n", x, y);
if (f == 1)
wc = max(wc, c); //wc为最大吃子数,c为当前已吃子数
if (f == 2)
if (c == wc) //当c==wc时,合理步法num+1
num++;
//在一个方向上,根据王可空格吃规则判断是否可吃子,可吃则将被吃棋子"拿出",继续递归,判断是否可连吃
for (int xx = x - 1, yy = y - 1; xx > 1 && yy > 1; xx--, yy--)
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2) //若遇到对方棋子
{
if (!vis[xx][yy]) //不是被"吃过"的
{
for (int i = xx - 1, j = yy - 1; i >= 1 && j >= 1; i--, j--) //继续往下判断
if (map[i][j] == 0) //下一格为空
/*printf("--1--吃掉(%d,%d)", xx, yy),*/ vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0; //设为"已被吃",防止后吃;递归dfs,此时c+1
else break;
}
else break;
}
}
else break;
for (int xx = x + 1, yy = y - 1; xx < 10 && yy>1; xx++, yy--)
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx + 1, j = yy - 1; i <= 10, j >= 1; i++, j--)
if (map[i][j] == 0)
/*printf("--2--吃掉(%d,%d)", xx, yy),*/ vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0;
else break;
}
else break;
}
}
else break;
for (int xx = x - 1, yy = y + 1; xx > 1 && yy < 10; xx--, yy++)
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx - 1, j = yy + 1; i >= 1 && j <= 10; i--, j++)
if (map[i][j] == 0)
/*printf("--3--吃掉(%d,%d)", xx, yy),*/ vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0;
else break;
}
else break;
}
}
else break;
for (int xx = x + 1, yy = y + 1; xx < 10 && yy < 10; xx++, yy++)
if (map[xx][yy] != 1)
{
if (map[xx][yy] == 2)
{
if (!vis[xx][yy])
{
for (int i = xx + 1, j = yy + 1; i <= 10 && j <= 10; i++, j++)
if (map[i][j] == 0)
/*printf("--4--吃掉(%d,%d)\n", xx, yy),*/ vis[xx][yy] = 1, dfs(i, j, c + 1, f), vis[xx][yy] = 0;
else break;
}
else break;
}
}
else break;
return;
}
//fs函数专用于兵吃子,目的是更新最大吃子数cnt和合理走法数num
void fs(int x, int y,int c,int f)
{
//printf("\n对兵棋(%d,%d):\n", x, y);
if (f == 1)
wc = max(wc, c); //wc为最大吃子数,c为当前已吃子数
if (f == 2)
if (c == wc) //当c==wc时,合理步法num+1
num++;
for (int i = 0; i <= 3; i++) //四个方向上(顺序为左上,右上,左下,右下)判断是否可吃子,可吃子递归函数fs,判断吃子后所在位置是否可继续吃子
{
int xx = x, yy = y;
int x1 = x + dx[i], y1 = y + dy[i];
int x2 = x + dx[i] * 2, y2 = y + dy[i] * 2;
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (x2 < 1 || y2 < 1 || x2>10 || y2>10) continue;
if (map[x2][y2] == 0 && map[x1][y1] == 2) //连续单跳吃,连续次数为sum
{
if (!vis[x1][y1])
{
//printf("\n(%d,%d)吃掉(%d,%d)\n",xx,yy, x1, y1);
vis[x1][y1] = 1; fs(x2, y2, c+1,f); vis[x1][y1] = 0;
}
else break;
}
}
return;
}
//获取(x,y)处己方棋子的合理走法数(合理走法可有多个,但满足"吃多数棋子"的规则)
void xc(int x, int y)
{
//为己方普通兵时,获取最大连续单跳吃次数cnt、合理走法数num
if (map[x][y] == 1)
{
int flag = 0;
cnt = 0, num = 0; //cnt记录最大连续单跳吃次数,num为合理走法数
//判断四个方向上是否可以单跳吃,并用flag记录(1为可单跳吃)
for (int i = 0; i <= 3; i++) //四个方向(左下,左上,右下,右上) 若有对方子&&下一格空 --> 可单跳吃(flag=1)
{
int x1 = x + dx[i], y1 = y + dy[i];
int x2 = x + dx[i] * 2, y2 = y + dy[i] * 2;
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (x2 < 1 || y2 < 1 || x2>10 || y2>10) continue;
if (map[x2][y2] == 0 && map[x1][y1] == 2)
{
flag = 1;
break;
}
}
//求出不吃子合理走法num
for (int i = 0; i <= 1; i++) //两个方向(左下,左上) 为空 --> 合理走法数num++
{
int x1 = x + dx[i], y1 = y + dy[i];
if (x1 < 1 || y1 < 1 || x1>10 || y1>10) continue;
if (!map[x1][y1]) num++;
}
//不能单跳吃,结束返回不吃子走法数num
if (!flag)
{
printf("兵棋(%d,%d)的最多吃子数为:0\n", x, y);
return;
}
//可单跳吃,获取兵棋最大连续吃子数cnt和合理走法数num
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
wc = 0;
fs(x, y, 0, 1); //调用兵吃子函数fs,参数f为1时更新最大吃子数wc
num = 0;
cnt = wc; //cnt为最大连续吃子数
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
fs(x, y, 0, 2); //调用fs函数,参数f为2时更新具有最大吃子数cnt的合理步法数num
printf("兵棋(%d,%d)的最多吃子数为:%d\n", x, y, cnt);
}
//为己方王时,获取王最大连续吃子数cnt、合理走法数num
if (map[x][y] == 3)
{
int flag = 0;
cnt = 0, num = 0;
//为王时,若不吃子,获取走法数num
for (int i = 0; i <= 3; i++)
{
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || yy < 1 || xx>10 || yy>10) continue;
while (map[xx][yy] == 0)
{
num++;
xx += dx[i], yy += dy[i];
if (xx < 1 || yy < 1 || xx>10 || yy>10) break;
}
}
//为王时,若可吃子,获取最大连续吃子数cnt和合理走法数num
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
wc = 0;
dfs(x, y, 0, 1); //调用王吃子函数dfs,参数f为1时更新最大吃子数wc
if (!wc)
{
printf("\n王棋(%d,%d)的最多吃子数为:0\n", x, y);
return; //若王无可吃子走法,结束返回,num为不吃子合理走法数
}
num = 0;
cnt = wc; //cnt为最大连续吃子数
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
dfs(x, y, 0, 2); //调用dfs函数,参数f为2时更新具有最大吃子数cnt的合理步法数num
printf("\n王棋(%d,%d)的最多吃子数为:%d\n", x, y,cnt);
}
}
int main()
{
//freopen("chess.in","r",stdin);
//freopen("chess.out","w",stdout);
int chess_in1[10][10] = { {0,0,0,0,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,2,0},{0,0,0,0,0,0,0,0,0,0},{0,0,2,0,1,0,2,0,0,0},{0,0,0,0,0,0,0,2,0,0}\
,{0,0,2,0,2,0,1,0,0,0},{0,0,0,0,0,0,0,2,0,0},{2,0,2,0,0,0,0,0,0,0},{0,1,0,1,0,0,0,2,0,0},{0,0,0,0,0,0,0,0,0,0} };
int chess_in2[10][10] = { {0,0,0,0,0,0,0,0,0,0},{0,0,0,0,1,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,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0} };
char c;
cout << "棋子矩阵:(0为空,1为己方棋子,2为敌棋)\n";
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in1[i][j] << " ";
cout << "\n";
}
cout << "判断王矩阵:(0非王,1为王棋)\n";
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
cout << chess_in2[i][j] << " ";
cout << "\n";
}
//1.第一次输入10x10矩阵,记录到map[i][j]中,0为空,1为己方棋,2为对方棋
//2.当为己方棋时,记录到w[tot],属性x,y代表坐标
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
map[i][j] = chess_in1[i-1][j-1]; //1.<--
if (map[i][j] == 1)
w[++tot].x = i, w[tot].y = j; //2.<--
}
//第二次输入10x10矩阵,当为己方棋且为王时,改map[][]为3
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
if (map[i][j] == 1 && chess_in2[i-1][j-1] == 1)
map[i][j] = 3;
}
cout << "棋盘:(0为空,1为己方兵棋,2为敌棋,3为己方王棋)\n";
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
cout << map[i][j] << " ";
cout << "\n";
}
int maxt = 0; //记录己方所有棋当中的最多吃子数
//1.遍历每一个己方棋子,利用xc函数,传入位置参数x,y,找出该棋子的最大吃子数cnt赋给s,合理走法数num赋给t
//2.记录所有己方棋子走法中吃子最多的个数
for (int i = 1; i <= tot; i++)
{
xc(w[i].x, w[i].y); //1.<--
w[i].s = cnt, w[i].t = num;
maxt = max(maxt, w[i].s); //2.<--
}
cout << "最多吃子数为:" << maxt<<endl;
//找出当前局面有ans中合理走法
int ans = 0;
for (int i = 1; i <= tot; i++)
{
if (w[i].s == maxt)
ans += w[i].t;
}
printf("合理走法数为:%d\n", ans);
//给出有合理走法的棋子坐标,若一个棋子有多个合理走法则输出多次
for (int i = 1; i <= tot && ans != 0; i++)
{
if (w[i].s == maxt)
for (int j = 1; j <= w[i].t; j++)
printf("具有合理走法棋子坐标:(%d,%d)\n", w[i].x, w[i].y);
}
return 0;
}