C++ 在c+中实现暴力+;
我有一个问题是关于一个包含2名玩家的游戏的优化。 所以我们有一个无向连通图,它有许多边和一些顶点。 每个玩家都必须移除边,如果一个顶点被隔离,他的分数将增加 一个,然后你必须删除另一个边缘。该图用所谓的 “邻接矩阵”。对于数组中的每一个顶点,在两个顶点之间都有一个连接。例如,如果我们有一个三角形,我们有以下数组: 0 1第一行 10第二排 10第三排 行对应于顶点,列对应于其与其他顶点的可能连接。我们被指派使用递归函数来应用 蛮力。我花了很多心思来编写这个函数的代码。我 实现了一些代码并编译和运行了它。我完全不知道为什么没有 功能。此函数的我的代码:C++ 在c+中实现暴力+;,c++,brute-force,array-algorithms,C++,Brute Force,Array Algorithms,我有一个问题是关于一个包含2名玩家的游戏的优化。 所以我们有一个无向连通图,它有许多边和一些顶点。 每个玩家都必须移除边,如果一个顶点被隔离,他的分数将增加 一个,然后你必须删除另一个边缘。该图用所谓的 “邻接矩阵”。对于数组中的每一个顶点,在两个顶点之间都有一个连接。例如,如果我们有一个三角形,我们有以下数组: 0 1第一行 10第二排 10第三排 行对应于顶点,列对应于其与其他顶点的可能连接。我们被指派使用递归函数来应用 蛮力。我花了很多心思来编写这个函数的代码。我 实现了一些代码并编译和运
class Matrix {
public:
int berekenZet (bool** array, bool player);
int berekenScore (bool** array, int rij, int kolom);
bool checkRij (bool** array, int rij);
void setGetal (int nummer) {getal = nummer;};
Matrix ();
private:
int getal;
int maxScoreA;
int huidigScore;
int maxverschil;
int maxScoreB;
int tellerHuidig;
矩阵::矩阵(){
}
}
int矩阵::berekenZet(布尔**数组,布尔播放器){
智力得分=0;
int-bestescore=-5;
int-slechtstescore=5;
智力的东西;
某物=0;
for(int i=0;i 对于(intj=i+1;j,在您的示例中,您只有5条边,因此分数只输出5次
对于(int i=0;i
对于(int j=i+1;j
迭代所有可能的边和条件,如果(数组[i][j])过滤掉真实边,则循环将只执行| E |次
另外,在您设置的循环中
然后
因此,一次只删除一条边。您可能希望再执行一个循环,一次删除多条边,以获得蛮力实现
编辑:您似乎不会在每次通话时都重置huidigScore。我建议您按如下方式更改助手功能:
上面的函数将huidigScore作为私有变量。现在,代替以下代码段:
if(player==true){
分数=分数+贝雷肯分数(数组,i,j);
}
否则{
分数=分数-贝雷肯分数(数组,i,j);
}
谢谢你的回复,我理解你的解释,但是我仍然不明白为什么它只输出5次“分数”,因为我非常感谢你的回复,我理解你的解释,但是我仍然不明白为什么它输出“分数”因为每个函数都有一个双for循环,所以只跳过了5次。我知道它跳过了所有错误的数组元素,因为if(数组[I][j])条件。我认为在第五个函数调用中,持续的2个数组元素应该设置为false。但是aray=true;语句应该确保当一个值返回到前一个函数时,它可以在最后一个函数之前的第四个函数中删除这些数组元素。我不知道。请原谅我带来的不便,我已经e在原始问题中发布了代码的其余部分。这包括berekenScore和checkRij,它们包含在berekenScoreOk中。我发现了一个直接的问题。您似乎没有重置全局变量huidigScore。我建议您改为将huidigScore作为参数(通过引用传递)到berekenScore,在函数开始时将其设置为零。Oke我有一个类,包括huidigScore的私有类和所有三个函数:berekenScore berekenZet en checkRij。我将用类代码更新我的原始问题。但是huidigScore确实可能有问题。你建议我如何通过引用通过huidigScorence到berekenScore?我是否应该在berekenZet中初始化huidigScore,将其设置为0,然后通过引用将其传递到berekenScore?
huidigScore = 0;
maxScoreA = 0;
maxScoreB = 0;
bool Matrix::checkRij (bool** array, int rij) {
for (int i=0; i<getal; i++) {
if (array[rij][i] == true)
return false;
}
return true;
int Matrix::berekenScore (bool** Array, int rij, int kolom) {
if (checkRij (Array, rij) == true )
huidigScore ++;
if (checkRij (Array, kolom) == true)
huidigScore ++;
return huidigScore;
int Matrix::berekenZet (bool** array, bool player) {
int score = 0;
int bestescore = -5;
int slechtstescore = 5;
int something;
something = 0;
for(int i = 0; i < getal; i++){
for(int j = i + 1; j <getal; j++){
huidigScore = 0;
if(array[i][j]){
something++;
array[i][j] = false;
array[j][i] = false;
score = 0;
if(player == true){
score = score + berekenScore(array, i ,j );
}
else{
score = score - berekenScore(array, i, j);
}
cout << "player" << player << endl;
cout << "score" << score << endl;
if(huidigScore == 0)
score = score + berekenZet(array, !player);
else
score = score + berekenZet(array, player);
if(player == true && score > bestescore)
bestescore = score;
else if(player == false && score < slechtstescore)
slechtstescore = score;
array[i][j] == true;
array[j][i] == true;
} //if
}//for
}//for
if(player == true && something != 0){
cout << "bestescore" << bestescore << endl;
return bestescore;
}// if outside of double for loop
else if(player == false && something != 0){
cout << "slechtstescore" << slechtstescore << endl;
return slechtstescore;
} // else if outside of double for loop
else if(something = 0){
cout << "bestescore" << bestescore << endl;
return 0;
} // determine whether array was empty when function was called
cout << "Aantal takken: " << takken << endl;
a = matrix.berekenZet(Array, player);
array[i][j] = false;
array[j][i] = false;
array[i][j] == true;
array[j][i] == true;
int Matrix::berekenScore (bool** Array, int rij, int kolom)
{
int huidigScore = 0;
if (checkRij (Array, rij) == true )
huidigScore ++;
if (checkRij (Array, kolom) == true)
huidigScore ++;
return huidigScore;
}
if(player == true){
score = score + berekenScore(array, i ,j );
}
else{
score = score - berekenScore(array, i, j);
}
cout << "player" << player << endl;
cout << "score" << score << endl;
int huidigScore = berekenScore(array, i ,j )
if(player == true){
score = score + huidigScore;
}
else{
score = score - huidigScore;
}
cout << "player" << player << endl;
cout << "score" << score << endl;