我有一组递归的if语句,它们是用Java编写的,我尝试将其用于Clojure语法,但出现ArrayOutOfBound错误。该函数接受row = 0,col = 0和多维字符数组。
这是Java代码:
public static boolean solveMaze(int r, int c, char[][] maze) {
//check for boundaries
if(r < 0 || c < 0 || r >= maze.length || c >= maze[0].length)
return false;
//base case, did i reach the finish?
if(maze[r][c] == '@')
return true;
//check if I'm at a valid point
if(maze[r][c] != '-')
//I have hit a wall, not a valid solution
return false;
//must be on a path, may be the right path
//leave a bread crumb
maze[r][c] = '!';
//check above
if(solveMaze(r-1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check below
if(solveMaze(r+1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check left
if(solveMaze(r,c-1, maze)) {
maze[r][c] = '+';
return true;
}
//check right
if(solveMaze(r,c+1, maze)) {
maze[r][c] = '+';
return true;
}
//if I reach this point...
return false;
}
这是我在clojure中提供的代码超出范围错误的代码:
(defn solveMaze [r c m]
(def tt true)
;(def che false)
;check to verify boundaries
(if (or (or (or (< r 0) (< c 0)) (>= r (count m))) (>= c (count (str (m 0)))))
false
true
)
;Check to know if the @ symbol has been found
(if (= (get-in m[r c]) "@")
true
false
)
;Checking if current index is a valid one
(if (not= (get-in m[r c]) "-")
;Wall hit
false
true
)
;found a path, leave a breadcrumb
(def temp_map (assoc-in m[r c] "!"))
;Check North
(if (= (solveMaze (dec r) c m) true)
(def temp_map (assoc-in m [r c] "+"))
true
;false
)
;Check South
(if (= (solveMaze (inc r) c m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;Check East
(if (= (solveMaze r (dec c) m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;Check West
(if (= (solveMaze r (inc c) m) true)
(def temp_map (assoc-in m[r c] "+"))
true
;false
)
;If code gets here
(= tt false)
)
请问我在做错什么吗???我已经尝试过使用clojure的多个if构造,但是它不起作用。
答案 0 :(得分:0)
您需要阅读Clojure的基础知识:
答案 1 :(得分:0)
使用条件代替if来解决这个问题。现在,代码的执行方式是先评估if,然后转到下一行以执行其余代码。使用cond时,如果表达式的计算结果为true,则它将返回该值,而不会计算其他表达式。