下面的图片是我想要创建的。
我以前问过类似的问题,得到了很好的答案,但这仍然不是我想要的。我知道我必须使用,\usepackage{tikz}
而且可能\usepackage{xintexpr} and \usetikzlibrary{math}
\documentclass[tikz,border=5]{article} \usepackage{tikz} \usepackage{xintexpr} \usetikzlibrary{math} \begin{document}
\begin{tikzpicture}[x=2cm,y=-2cm, node 0/.style={fill=red!20}]
\tikzmath{%
int \i, \j, \m, \n, \t;
\m = 5; \n = 5;
% Initialise board.
for \i in {0,...,\m}{
for \j in {0,...,\n}{
\t{\i,\j} = 0;
};
};
% Create holes.
\t{2,3} = -1;
\t{3,4} = -1;
\t{5,2} = -1;
% Perform calculations.
for \i1 in {0,...,\m}{
for \j1 in {0,...,\n}{
if (\t{\i1,\j1} == -1) then {
\t{\i1,\j1} = 0;
} else {
if (\i1 == 0 || \j1 == 0) then {
\t{\i1,\j1} = 1;
} else {
\i2 = \i1 - 1;
\j2 = \j1 - 1;
\t{\i1,\j1} = \t{\i2,\j1} + \t{\i1,\j2};
};
};
};
};
% Draw nodes.
for \i1 in {0,...,\m}{
for \j1 in {0,...,\n}{
{ \node [circle, fill=blue!20, minimum size=1cm, node \t{\i1,\j1}/.try]
(n-\i1-\j1) at (\j1, \i1) {\t{\i1,\j1}}; };
};
};
% Draw edges.
for \i1 in {0,...,\m}{
for \j1 in {0,...,\n}{
\i2 = \i1 + 1;
\j2 = \j1 + 1;
if (\i1 < \m) then {
if (\t{\i2,\j1} > 0) then {
{ \draw [thick, -stealth] (n-\i1-\j1) -- (n-\i2-\j1); };
};
};
if (\j1 < \n) then {
if (\t{\i1,\j2} > 0) then {
{ \draw [thick, -stealth] (n-\i1-\j1) -- (n-\i1-\j2); };
};
};
};
};
}
\end{tikzpicture}
\end{document}
这段代码包含了我所寻找的想法,但我想要的是改进代码,使其看起来与所包含的图片完全一样。我对乳胶和编程/编码的想法还不熟悉,但我正在努力学习。
另外我的照片需要适合我的家庭作业问题设置
\documentclass[28pt]{article}
\usepackage{fancyhdr}
\usepackage[includeheadfoot,margin=1.0cm]{geometry}
答案1
好的,因此需要一个额外的数据结构来模拟有漏洞的“路线”。
\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[x=2cm, y=-2cm]
\tikzmath{%
int \i, \j, \m, \n, \t, \r;
\m = 6; \n = 6;
% Initialise board.
for \i in {0,...,\m-1}{
for \j in {0,...,\n-1}{
\t{\i,\j} = 0;
};
};
for \i in {0,...,\m-1}{
for \j in {0,...,\n-1}{
\r{\i,\j} = 11;
};
};
% Starting point.
\t{0,0} = 1;
% Create holes in route.
% 01 can go east
% 10 can go south
% 11 can go east and south
\r{2,2} = 10;
\r{3,3} = 10;
\r{4,2} = 01;
% Perform calculations.
for \i1 in {0,...,\m-1}{
for \j1 in {0,...,\n-1}{
if (\i1 == 0 && \j1 == 0) then {
\t{0,0} = 1;
} else {
if (\j1 > 0) then {
\j2 = \j1 - 1;
if (mod(\r{\i1,\j2}, 2) == 1) then {
\t{\i1,\j1} = \t{\i1,\j1} + \t{\i1,\j2};
};
};
if (\i1 > 0) then {
\i2 = \i1 - 1;
if (mod(floor(\r{\i2,\j1} / 10), 2) == 1) then {
\t{\i1,\j1} = \t{\i1,\j1} + \t{\i2,\j1};
};
};
};
};
};
% Draw nodes.
for \i1 in {0,...,\m-1}{
for \j1 in {0,...,\n-1}{
{ \fill (\j1, \i1) circle [radius=0.05]
node [above left] {\t{\i1,\j1}}; };
};
};
% Draw edges.
for \i1 in {0,...,\m-1}{
for \j1 in {0,...,\n-1}{
if (mod(\r{\i1,\j1}, 2) == 1 && \j1 < \n - 1) then {
{ \draw [thick, -stealth, shorten >=0.1cm] (\j1,\i1) -- ++(1, 0); };
};
if (mod(floor(\r{\i1,\j1} / 10), 2) == 1 && \i1 < \m - 1) then {
{ \draw [thick, -stealth, shorten >=0.1cm] (\j1,\i1) -- ++(0,1); };
};
};
};
}
\end{tikzpicture}
\end{document}
答案2
我希望这就是你想要的
\documentclass{standalone}
\usepackage{tikz}
\usepgfmodule{parser}
\begin{document}
\catcode`\^^`=12\relax\pgfparserdef{countpath}{initial}{the character ^^`}{\nextcell}\catcode`\^^`=10\relax
\catcode`\^^M=12\relax\pgfparserdef{countpath}{initial}{the character ^^M}{\nextline}\catcode`\^^M=5\relax
\pgfparserdef{countpath}{all}{the character ;}{\endcountpath}
\pgfparserdef{countpath}{all}{the character +}{\nextcell\drawanode}
\pgfparserdef{countpath}{all}{the character |}{\nextcell\draw(\coordx,-\coordy)+(0,1)--+(0,-1);\expandafter\xdef\csname cp/\coordx/\coordy\endcsname{C}}
\pgfparserdef{countpath}{all}{the character -}{\nextcell\draw(\coordx,-\coordy)+(-1,0)--+(1,0);\expandafter\xdef\csname cp/\coordx/\coordy\endcsname{C}}
\def\countpath{
\begin{tikzpicture}[shorten <=9pt,shorten >=9pt]
\def\coordx{0}\def\coordy{0}
\catcode`\^^M=12\relax\catcode`\^^`=12\relax
\pgfparserparse{countpath}%
}
\def\endcountpath{
\end{tikzpicture}
\pgfparserswitch{final}
}
\def\nextcell{\pgfmathtruncatemacro\coordx{\coordx+1}}
\def\nextline{\pgfmathtruncatemacro\coordy{\coordy+1}\def\coordx{0}}
\def\drawanode{
\def\numberofpaths{0}
\ifnum\coordy=1
\ifnum\coordx=1
\def\numberofpaths{1}
\fi
\fi
% check upper neighbor
\pgfmathtruncatemacro\coordyminusi{\coordy-1}
\expandafter\if\csname cp/\coordx/\coordyminusi\endcsname\relax\else
\pgfmathtruncatemacro\coordyminusii{\coordy-2}
\pgfmathtruncatemacro\numberofpaths{\numberofpaths+\csname cp/\coordx/\coordyminusii\endcsname}
\fi
% check left neighbor
\pgfmathtruncatemacro\coordxminusi{\coordx-1}
\expandafter\if\csname cp/\coordxminusi/\coordy\endcsname\relax\else
\pgfmathtruncatemacro\coordxminusii{\coordx-2}
\pgfmathtruncatemacro\numberofpaths{\numberofpaths+\csname cp/\coordxminusii/\coordy\endcsname}
\fi
\expandafter\xdef\csname cp/\coordx/\coordy\endcsname{\numberofpaths}
\draw(\coordx,-\coordy)node{\numberofpaths};
}
\countpath
+-+-+-+-+-+-+-+ +-+
| | | | | | | |
+-+ +-+-+ +-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+ +-+
| | | | | | | |
+-+ +-+-+ +-+-+ +-+
| | | | | | | |
+-+-+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+ +-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+-+
;
\end{document}