C++ 查找与总和=10相邻的元素选择
说明: 给定的矩阵[x][y],具有x行和y列数。填充了0到5(含)之间的随机数 寻找解决方案的说明:该解决方案被认为是一组相互相邻的矩阵元素(不考虑对角邻域),数量之和为10。矩阵的每个元素可用于一次决策。解决方案可以有任意位数。该决定必须结束除零以外的任何数字 例如: 给定 解决方案1:(1-2-3-4) 我试着这样做,但这是错误的,我不知道什么时候我必须停止, 解决方案这是一个包含很多索引的类,请帮助我C++ 查找与总和=10相邻的元素选择,c++,algorithm,recursion,C++,Algorithm,Recursion,说明: 给定的矩阵[x][y],具有x行和y列数。填充了0到5(含)之间的随机数 寻找解决方案的说明:该解决方案被认为是一组相互相邻的矩阵元素(不考虑对角邻域),数量之和为10。矩阵的每个元素可用于一次决策。解决方案可以有任意位数。该决定必须结束除零以外的任何数字 例如: 给定 解决方案1:(1-2-3-4) 我试着这样做,但这是错误的,我不知道什么时候我必须停止, 解决方案这是一个包含很多索引的类,请帮助我 void xxx(int colCount, int rowCount, int cu
void xxx(int colCount, int rowCount, int currentRow, int currentCol, int** matrix, int sum, Solution *solution, int solCount) {
sum += matrix[currentRow][currentCol];
matrix[currentRow][currentCol] = -1;
if(sum > 10){
sum - = matrix[currentRow][currentCol];
return;
} else if(sum == 10){
solution[solCount].additem(currentRow, currentCol);
return xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution, solCount+1);
} else {
//UP
if( currentRow > 0 && matrix [currentRow - 1][currentCol] != -1){
xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution,solCount);
}
//LEFT
if(currentCol > 0 && matrix [currentRow][currentCol-1] != -1){
xxx(5,5,currentRow, currentCol - 1, matrix, sum, solution,solCount);
}
//DOWN
if(currentRow + 1 < colCount && matrix[currentRow + 1][currentCol] != -1){
xxx(5,5,currentRow + 1, currentCol, matrix, sum, solution,solCount);
}
//RIGHT
if(currentCol + 1 < rowCount && matrix[currentRow][currentCol + 1] != -1){
xxx(5,5,currentRow, currentCol + 1, matrix, sum, solution,solCount);
}
}
}
void xxx(int colCount、int rowCount、int currentRow、int currentCol、int**matrix、int sum、Solution*Solution、int solCount){
总和+=矩阵[currentRow][currentCol];
矩阵[currentRow][currentCol]=-1;
如果(总和>10){
总和-=矩阵[currentRow][currentCol];
返回;
}否则如果(总和=10){
解决方案[solCount]。添加项(currentRow,currentCol);
返回xxx(5,5,currentRow-1,currentCol,矩阵,和,解,solCount+1);
}否则{
//向上
如果(currentRow>0&&matrix[currentRow-1][currentCol]!=-1){
xxx(5,5,currentRow-1,currentCol,矩阵,和,解,solCount);
}
//左
如果(currentCol>0&&matrix[currentRow][currentCol-1]!=-1){
xxx(5,5,currentRow,currentCol-1,矩阵,和,解,solCount);
}
//向下
if(currentRow+1
该解决方案为常规BFS或DFS,可接受的解决方案的路径和为10。不过,简而言之:@Assares尝试先勾勒/勾勒出您想要使用的算法,然后尝试对其进行编程。test30给了您一个有用的提示,尽管可能还有其他解决方法。
0 **1** 2 3 4 5
1 **2** 3 4 5 0
2 **3** **4** 5 1 2
void xxx(int colCount, int rowCount, int currentRow, int currentCol, int** matrix, int sum, Solution *solution, int solCount) {
sum += matrix[currentRow][currentCol];
matrix[currentRow][currentCol] = -1;
if(sum > 10){
sum - = matrix[currentRow][currentCol];
return;
} else if(sum == 10){
solution[solCount].additem(currentRow, currentCol);
return xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution, solCount+1);
} else {
//UP
if( currentRow > 0 && matrix [currentRow - 1][currentCol] != -1){
xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution,solCount);
}
//LEFT
if(currentCol > 0 && matrix [currentRow][currentCol-1] != -1){
xxx(5,5,currentRow, currentCol - 1, matrix, sum, solution,solCount);
}
//DOWN
if(currentRow + 1 < colCount && matrix[currentRow + 1][currentCol] != -1){
xxx(5,5,currentRow + 1, currentCol, matrix, sum, solution,solCount);
}
//RIGHT
if(currentCol + 1 < rowCount && matrix[currentRow][currentCol + 1] != -1){
xxx(5,5,currentRow, currentCol + 1, matrix, sum, solution,solCount);
}
}
}