我认为,对我来说,第一步就是将精确表示转换成您可以轻松操作的结构。我同意,多维数组似乎是最合理的模型,因为它非常符合这个概念。
您需要将每个字符读入新的char[]
。你可以使用this question的代码。这是你的第一步。将每个char
放入一个阵列中,并将每个阵列放入一个char[][]
。
第二步是接近检查。我们来看看一些可能性。
---1---1---11
---1-------11
所以你知道你可以在这里访问每个成员。我想不出一个更好的方法来检查每个元素,而不是接近O(n^2)
。不好,但是这是你要建立的东西。
- 检查一行,直到找到一个不是空白的值。
- 从这一点,你会找到一切与它在同一行。这意味着增加当前的索引,直到你打到更多的空白或数组的末尾。
- 接下来是找出下面的内容。您现在知道值开始和完成。因此,您需要返回并检查您的
start
索引和您的finish
索引,每次都沿着多维数组进行检查。
- 对于您点击的每个值,增加一些全局指针。
按照上面的例子中,你会去..
---1---1---11
^-----------------Found a character. Increment count.
---1---1---11
^----------------Only whitespace next to it. Stop looking.
---1---1---11
---1---1---11
^-----------------Found a character below. Increment count.
---1---1---11
---1---1---11
xxxxxxxxxxxxx
^-----------------We're at the end.
---1---1---11
^----------------There was no value after it. Search is complete.
Total Count = 2.
然后你会移动到下一个。现在,为了给出一些完整的例子,我会做最后一个向您展示算法的工作。
---1---1---11
^---------Found a value that isn't whitespace. Increment count.
---1---1---11
^--------Found another value. Increment count.
---1---1---11
^-------At the end of the array. Stop looking.
---1---1---11
---1-------11
^---------Found a value. Increment count.
---1---1---11
---1-------11
xxxxxxxxxxxxx
^---------At the end. Stop looking.
---1---1---11
---1-------11
^--------Found a value. Increment count.
---1---1---11
---1---1---11
xxxxxxxxxxxxx
^--------At the end. Stop looking.
---1---1---11
^-------At the end of the top array. Finished search.
Total Count = 4.