- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好吧,我必须编写一个程序来计算一个骑士(在棋盘上)从 (xb, yb) 到 (xe, ye) 可以走的路线数。我不确定我哪里出错了。好吧,我知道计数不会添加任何东西,并且会在我的代码中保持为 0,但我也觉得我也不会太远。我只是在寻找正确方向的插入力。提前致谢
#include <stdio.h>
#include <stdlib.h>
int numRoutes(xb, yb, xe, ye, n){
int count = 0;
if(n>0){
count = count + numRoutes(xb+1, yb+2, xe, ye, n-1);
count = count + numRoutes(xb+1, yb-2, xe, ye, n-1);
count = count + numRoutes(xb-1, yb+2, xe, ye, n-1);
count = count + numRoutes(xb-1, yb-2, xe, ye, n-1);
count = count + numRoutes(xb+2, yb+1, xe, ye, n-1);
count = count + numRoutes(xb+2, yb-1, xe, ye, n-1);
count = count + numRoutes(xb-2, yb+1, xe, ye, n-1);
count = count + numRoutes(xb-2, yb-1, xe, ye, n-1);
}
return count;
}
int main(int argc, char *argv[]){
int xb, xe, yb, ye, n;
printf("Start coordinate: ");
scanf("%d %d", &xb, &yb);
printf("End coordinate: ");
scanf("%d %d", &xe, &ye);
printf("Length: ");
scanf("%d", &n);
int allRoutes = numRoutes(xb, yb, xe, ye, n);
printf("Number of routes: %d\n", allRoutes);
return 0;
}
最佳答案
这里你从x,y开始,想去tx,ty,最大长度为n。
在递归方法中,您的函数必须:
如果您想知道找到了哪些路由,您必须管理一个全局堆栈并在进入时推送 (x,y),在离开和找到目标时弹出它。此外,当您找到目标时,您应该将路线保存在某处(堆栈的内容)。
一些改进:
visited[N][N]
数组初始化为 0,每次 push() 时置 1,每次 pop() 时置 0。然后你可以拒绝测试这个路线仍然被访问过的位置(防止循环,圆圈......)它可能会查看(不阻止循环,不检查板限制,并使用 pstack()
打印堆栈内容的函数):
int routes(int x, int y, int tx, int ty, int n) {
int nb = 0; // number found
int lng = ABS(x-tx)+ABS(y-ty); // minimal length to reach destination
if (n < lng) {
return(0); // not possible, it will not be a route
}
push(x, y);
// if we are on destination -> 1: we found a route to destination
if ((x == tx) && (y == ty)) {
pstack();
pop();
return(1);
}
// try each move (8 directions)
nb += routes(x+1, y+0, tx, ty, n-1);
nb += routes(x+1, y+1, tx, ty, n-1);
nb += routes(x+1, y-1, tx, ty, n-1);
nb += routes(x+0, y+1, tx, ty, n-1);
nb += routes(x+0, y-1, tx, ty, n-1);
nb += routes(x-1, y+1, tx, ty, n-1);
nb += routes(x-1, y+0, tx, ty, n-1);
nb += routes(x-1, y-1, tx, ty, n-1);
pop();
return(nb);
}
这将为 routes(4, 4, 5, 5, 2);
提供:
. (4,4) (5,4) (5,5)
. (4,4) (5,5)
. (4,4) (4,5) (5,5)
Found 3 routes from (4,4) to (5,5) with length 2
关于c - 在无限棋盘上,骑士从 xb、yb 到 xe、ye 可以走的路线数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156421/
我想递归地遍历一个目录,但我希望 python 在遇到包含超过 100 个文件的目录时从任何单个 listdir 中断。基本上,我正在搜索 (.TXT) 文件,但我想避免使用包含大型 DPX 图像序列
我正在尝试遍历列表(例如 sql 行)并为每一行触发例程。问题是传递给函数的值不会在运行时进行评估,因此根据函数执行所需的时间,它可能会使用下一行中的任何值而不是当前行。 我知道我可以在普通函数中提取
我需要以毫秒为单位的时间来处理大量事务,因此我想要正确且快速的东西。下面的工作会做得最好吗? : iMilli := int((time.Nanoseconds() % 1e6) / 1e3
我有以下目录/文件设置(已简化): Ce +---top.txt +---X0.0 | | | +---Y0.0 | | | | |
我遇到了类似的问题: Connecting to Redis To Go with PHP 基本上,我在 redis 中有这个 uri: redis://myusername:foopassword@
我阅读了下面的主题 Go: multiple value in single-value context 但我不明白这个解释在我的案例中。可能是因为我想使用 interface 在下面的情况下,我得到
我有一个模板,我想使用 text/template 评估各个字段包裹。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足够强大以处理此类评估? type something stru
我编写了简单的服务器程序来从客户端接收数据。我有点不明白有时我从函数中得到错误 read tcp4 IP:PORT i/o timeoutint, err := conn.Read([]byte) 未
我只需要解码和更新 json 对象的特定值。问题是我不知道对象的完整结构。 encoding/json 包“忽略”/截断结构中未提供的字段,因此在编码时这些字段将丢失。 我想知道是否可以只解码我知道的
我正在尝试使用带有 C++ 目标的 ANTLR4 来实现 TSql 解析器。我抓取了语法文件 here .该jar用于制作相应的源文件(因冲突将TSqlParser.cpp中的NULL全部改为null
我在 win7 中使用 python 3.3.3 - 我只想列出网络目录中的所有文件。 import os for root, dirs, files in os.walk("X:\\network\
当我运行 go 脚本 ( go run example.go ) 时出现此错误 /home/travis/.gvm/gos/go1.1.2/src/pkg/github.com/user/exampl
我正在尝试通过 gmail API 发送电子邮件使用 Go但我发现文档非常有缺陷/令人困惑。这一次我看不到收据字段和电子邮件正文。 我不需要上传任何东西,所以我找到了 Simple upload ,
本人是一名专业的windows/.Net开发者,一直在慢慢学习rails/ruby/python/etc。在我有空的时候。在过去 8 年左右的时间里,我也一直在使用各种 Linux 发行版。然而,有一
我想知道是否可以使用 std http 来响应 http 请求打包并仍然保持 go 例程事件(例如运行任务密集型任务)。用例是我需要接收一个 http 请求,然后在几分钟后回调该服务 最佳答案 只需从
我想知道关于指针的最佳实践是什么。我应该在结构上还是在其字段上定义它们。我虽然定义一个指向结构本身的指针是有意义的,但这里有一个我觉得很有趣的例子。如果所有字段都是指针,为什么我不应该使用指向整个结构
我是一名优秀的程序员,十分优秀!