你只需要改变“前进”是什么意思的想法(可能还有“完成”的意思)。例如,只需“向下”即可使用:
var iter = matrix.MakeCustomIterator(
new int[] { 0, 0 },
(coll, cur) => coll[cur[0]][cur[1]],
(cur) => cur[0] > 2 || cur[1] > 2,
// Increase the row, but stay on the same column
(cur) => new int[] { cur[0] + 1, cur[1] });
“走”:
var iter = matrix.MakeCustomIterator(
new int[] { 0, 0 },
(coll, cur) => coll[cur[0]][cur[1]],
(cur) => cur[0] > 2 || cur[1] > 2,
// Stay on the same row, but increase the column
(cur) => new int[] { cur[0], cur[1] + 1 });
“顺其自然”会更难,但可行:
var iter = matrix.MakeCustomIterator(
new int[] { 0, 0 },
(coll, cur) => coll[cur[0]][cur[1]],
(cur) => cur[0] > 2,
// Stay on the same row if we can, otherwise move to next row
(cur) => cur[1] == 2 ? new int[] { cur[0] + 1, 0 }
: new int[] { cur[0], cur[1] + 1});