我一直在研究8皇后问题,但我遇到了困难.我不想要代码.我会喜欢指导和指导,以便了解如何使用回溯递归来解决这个问题.
该程序应该通过在ASCII中绘制皇后的位置来枚举N皇后问题的所有解决方案,就像这里的两个解决方案一样.
到目前为止,我的伪代码是:
void queen(int n){
for( int i = 0; i < n; i++){
place queen[ i ] on row i;
for(int j = 0 ; j < n ; j++){
if( queen[ i ] is not in the same column as queen[0] through queen[ i - 1 ] &&
queen[ i ] is not on the same major diagonal with queen[0] through queen[ i -1 ] &&
queen[ i ] is not on the same minor …Run Code Online (Sandbox Code Playgroud) 我正在尝试为以下任务制作算法:
我已经多次使用Google搜索,但我能找到的只是关于如何转换矩阵,如何进行几何变换,如何将字符串转换为另一个字符串以及类似内容的想法.
有人有什么想法吗?
我有两个字符串str1和str2.是否有任何算法可以用于使用递归打印出两个字符串的所有交错?
更新:
public class Interleave {
private String resultString[] = new String[10];
private String[] interStr(String str1, String str2){
int n = ((Factorial.factorial(str1.length() + str2.length())) / (Factorial.factorial(str1.length()) * Factorial.factorial(str2.length())));
//n is number of interleavings based on (str1.length()+str2.length())! / (str1.length()! * str2.length()!)
if(str1.length() == 0){
resultString[0] = str2;
return resultString;
}
if(str2.length() == 0){
resultString[0] = str1;
return resultString;
}
else{
for(int i = 0; i < n; i++){
resultString[i]= str1.substring(0, 1) + interStr(str1.substring(1), str2.substring(1));
}
}
return …Run Code Online (Sandbox Code Playgroud) private int array[][] = new int[5][5];
private void arrayIteration(){
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = 10;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将迭代方法更改为递归执行相同任务的方法吗?
编辑(这是我尝试过的,但它只是在玩我想做的事情):
private void arrayRecursion(){
if(){
array[i][i] = 10; // Base
return;
}
for(int i = 0; i < array.length; i++){
arrayRecursion();
}
}
Run Code Online (Sandbox Code Playgroud)