【发布时间】:2022-01-22 12:46:27
【问题描述】:
给你一个 NxN (2
N = 3, T = 3
...
.H.
...
Output: 2
N = 4, T = 3
...H
.H..
....
H...
Output: 6
N = 3, T = 2
.HH
HHH
HH.
Output: 0
D 表示向下移动,R 表示向右移动。对于第一种,两种方式是 DDRR 和 RRDD。对于第二种,六种方式分别是DDRDRR、DDRRDR、DDRRRD、RRDDDR、RRDDRD和RRDRDD。对于最后一个,没有办法让它到右下角。
我想到的一件事是可以使用 DFS 解决这个问题,但我不知道如何针对这个问题实施它。任何帮助都会非常有帮助。
我已经尝试过这种递归方法,但我很确定我完全走错了路。
public static void step(char[][] graph, int[] pos, int turns, int max)
{
if(turns > max) return;
else if(pos[0] == graph.length-1 && pos[1]==graph.length-1)
{
count++;
return;
}
else if(pos[0] == graph.length && graph[pos[0]+1][pos[1]] == 'H') return;
else if(pos[1] == graph.length && graph[pos[0]][pos[1]+1] == 'H') return;
else if(graph[pos[0]+1][pos[1]] == 'H' && graph[pos[0]][pos[1]+1] == 'H') return;
else {
step(graph, new int[]{pos[0]+1, pos[1]}, turns+1, max);
step(graph, new int[]{pos[0], pos[1]+1}, turns+1, max);
}
}
【问题讨论】:
-
这似乎是某种功课。你能提供你到目前为止制作的代码吗?
-
请阅读How to Ask。这里的一个基本要求是展示你编写的代码并解释什么是行不通的。我们不会为你做作业。
-
我已经附上了我到目前为止所做的代码。我一开始没有附上它,因为我很确定我以错误的方式接近它。