我正在尝试编写一个简单的最小冲突伪代码来解决 n 皇后问题。以下 MWE 显示了所有使用的包,但无法重现我遇到的错误...不知何故。
\documentclass{article}
\usepackage{amsmath}
\usepackage{graphics}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\Procedure{Min-Conflicts}{$n$}
\State {board $\gets$ queens on unique row and column} \Comment{\textit{The initial state that approximates a solution. Only diagonal attacks are conflicts.}}
\For {$t$ in max\_steps} \Comment{\textit{If constant, algorithm runs in $\mathcal{O}(1)$ time}}
\For {a random row in board} \Comment{\textit{A random subspace.}}
\State queen $\gets$ position in row with least conflicts \Comment{\textit{Greedily selects best state for subspace.}}
\EndFor
\EndFor
\If {\textsc{Is\_Solution}(board)}:
\State \Return board
\Else:
\State \Return \textsc{Min-Conflicts}($n$)
\EndProcedure
\end{algorithmic}
\end{document}
我想展示一个输出示例,但我意外地按了 ESC 键关闭了我的 pdf 预览(它会在按 ESC 键关闭预览之前更新)。我正在为我的 IDE 使用完整的 MIKTEX 安装和 TEXSTUDIO。
这些是我的以下错误:
Missing \endcsname inserted. \Procedure (first line with \Procedure on it) Package algorithmicx Error: Some blocks are not closed!!!. \end{algorithmic} (Next line) Missing \endcsname inserted. \Procedure (5 lines later) Extra \endcsname. \Procedure (same line) Missing number, treated as zero. \EndProcedure (11 lines later) Package algorithmicx Error: Some blocks are not closed!!!. \end{algorithmic} (next line)
我不知道为什么会出现这些问题。我甚至修改了我在其他地方编写的现有算法,没有任何问题。
答案1
您的最终\If
...\Else
条件没有附带\EndIf
。尽管使用了noend
选项,但您仍然需要\EndIf
在代码中使用:
\documentclass{article}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\Procedure{Min-Conflicts}{$n$}
\State {board $\gets$ queens on unique row and column} \Comment{\textit{The initial state that approximates a solution. Only diagonal attacks are conflicts.}}
\For {$t$ in max\_steps} \Comment{\textit{If constant, algorithm runs in $\mathcal{O}(1)$ time}}
\For {a random row in board} \Comment{\textit{A random subspace.}}
\State queen $\gets$ position in row with least conflicts \Comment{\textit{Greedily selects best state for subspace.}}
\EndFor
\EndFor
\If {\textsc{Is\_Solution}(board)}:
\State \Return board
\Else:
\State \Return \textsc{Min-Conflicts}($n$)
\EndIf
\EndProcedure
\end{algorithmic}
\end{document}