我不得不在Java中进行广度优先搜索以进行分配.我有一个5x5网格的瓷砖(总共24个 - 一个瓷砖留下'空白').搜索的目的是通过向上,向下,向左或向右移动"空白"来重新排列图块,以最终将图块重新排列为正确的顺序.
为了进行此搜索,我创建了一个Arraylist'队列'.我有一个方法,在这个arraylist的索引0处获取状态,找到可以跟随的每个合法移动,然后将它们分别添加到arraylist的末尾.
从理论上讲,这一直持续到最终找到'目标国'.问题是当我运行搜索时,'队列'arraylist继续变得越来越大.今天我把它运行了几个小时仍然没有找到解决方案.
这表明我可能以错误的方式解决了这个问题,并且有一个更好的方法让我在Java中进行广度优先搜索.我知道我的解决方案确实有效(最终),因为当我使用与目标状态没有太大差异的开始状态时,找到正确的路径并不需要太长时间.但是,我已经获得了一个使用的开始状态,不幸的是,它远远没有接近目标状态!
任何提示或技巧将不胜感激!
import sys
def keepsumming(number):
numberlist = []
for digit in str(number):
numberlist.append(int(digit))
total = reduce(add, numberlist)
if total > 9:
keepsumming(total)
if total <= 9:
return total
def add(x,y):
return x+y
keepsumming(sys.argv[1])
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,添加任意数字的各个数字,并保持求和数字,直到结果只有一位数.(例如1048576 = 1 + 0 + 4 + 8 + 5 + 7 + 6 = 31 = 3 + 1 = 4).该功能似乎适用于某些鞋带,但不适用于其他鞋带.例如:
$python csp39.py 29
Run Code Online (Sandbox Code Playgroud)
返回None,但是:
$python csp39.py 30
Run Code Online (Sandbox Code Playgroud)
应该返回3,因为它应该......
任何帮助,将不胜感激!
我正在为一个入门编程课程的项目工作,所以我使用的是基本的javascript.这是我们的第一个有功能的项目,由于某些原因我似乎无法使其工作.我调用了所有变量并在程序启动之前创建了该函数但由于某种原因它跳过了在我的程序中运行该函数.任何帮助,将不胜感激.
这只是我的程序的开始,我不想编写剩下的代码,直到我弄清楚为什么这部分被打破,这就是为什么程序除了关闭窗口没有做任何事情,如果它没有通过测试.
// 1 Declare Variables
var numTrees;
var counter = 0;
var answer = "no";
function treeFunction(answer, counter, numTrees) {
while (answer == "no" && counter < 3) {
if (numTrees == 5, 10) {
answer = "yes";
} else if (numTrees < 5 || numTrees > 10) {
alert("That is an incorrect value.\nThe sample size should be less than 5 or greater than 10.\nPlease try again.");
answer = "no";
numTrees = prompt("Please reenter the amount of trees in …Run Code Online (Sandbox Code Playgroud) 这是用于查找最大子向量和的递归代码
#include <iostream>
using namespace std;
int Max(int a,int b,int c){
return max(a,std::max(b,c));
}
int a[]={31,-41,59,26,-53,58,97,-93,-23,84};
int n=sizeof(a)/sizeof(int);
int maximum3(int l,int u){
if (l>u) return 0;
if (l==u) return std::max(0,a[l]);
int m=(l+u)/2;
int lmax=0;
int sum=0;
int rmax=0;
int sum1=0;
for (int i=m;i>=l;i--){
sum+=a[i];
lmax=std::max(lmax,sum);
}
for (int j=m+1;j<u;j++){
sum1+=a[j];
rmax=std::max(rmax,sum);
}
return Max(lmax+rmax,maximum3(l,m),maximum3(m+1,u));
}
int main(){
cout<<maximum3(0,n-1)<<" ";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它重新运行155而其他非递归方法返回187请帮助
我现在开始用Java编程.我试图将标题中的序列编码为Java中的输出,但我被卡住了!我正在尝试for功能,欢迎任何帮助;)