The following MWE is the code of my attempt translating my working algorithm in C# to (La)TeX. I don't understand why it does not work. Can you probe the source of problem?
Remarks:
#1is a list of binary elements. Either character1(move upward) or0(move to the right) will be appended to this list recursively. In the beginning it is an empty list.#2represents the remaining number of movements to the right.#3represents the remaining number of movements upward.
\documentclass{article}
\usepackage{ifthen,pgf}
\parindent=0pt
\newcommand{\Populate}[3]%
{%
\ifthenelse{#2=0}% on the most left border?
{%
\ifthenelse{#3=0}% on the most top border?
{%
#1\endgraf% I need to trim the most left comma later!
}%
{%
% move upward
\pgfmathtruncatemacro{\ups}{#3-1}%
\Populate{#1,1}{#2}{\ups}%
}%
}%
{%
\ifthenelse{#2=#3}% on the diagonal border?
{%
% move to the right
\pgfmathtruncatemacro{\rights}{#2-1}%
\Populate{#1,0}{\rights}{#3}%
}%
{%
% move to the right
\pgfmathtruncatemacro{\rights}{#2-1}%
\Populate{#1,0}{\rights}{#3}%
% move upward
\pgfmathtruncatemacro{\ups}{#3-1}%
\Populate{#1,1}{#2}{\ups}%
}%
}%
}
\begin{document}
\Populate{}{4}{4}
\end{document}
It produced a wrong output as follows:
,0,0,0,0,1,1,1,1
,0,0,0,1,1,1,1
,0,0,1,1,1,1
,0,1,1,1,1
The expected output must be as follows: (the leading comma will be removed later)
,0,0,0,0,1,1,1,1
,0,0,0,1,0,1,1,1
,0,0,0,1,1,0,1,1
,0,0,0,1,1,1,0,1
,0,0,1,0,0,1,1,1
,0,0,1,0,1,0,1,1
,0,0,1,0,1,1,0,1
,0,0,1,1,0,0,1,1
,0,0,1,1,0,1,0,1
,0,1,0,0,0,1,1,1
,0,1,0,0,1,0,1,1
,0,1,0,0,1,1,0,1
,0,1,0,1,0,0,1,1
,0,1,0,1,0,1,0,1
The last edit:
For N x N case, use \Populate{0}{N-1}{N} to avoid having an extra job to remove the leading commas above. (I just got this enlightenment)
tikz-pgftag is necessary? ;-) – Paul Gaborit Jul 18 '12 at 07:10