Hey guys!

I've got a small problem that I can't figure out. I have 2D arrays of arbitrary sizes, and they contain different characters representing different stuff (like 'a', 'b', 'c' etc.). Now, the problem is, I need to find horizontal and vertical lines of same characters and make new arrays from them without using the same coordinate twice. For example:

0 1 2 3 4 . . .
0 a a a a a
1   a
2   a a a a
3   a
4   a
.
.

This would result like this:

Array 1:
  0 1 2 3 4
0 a a a a a
1
2
3
4

Array 2:
  0 1 2 3 4
0 
1   a
2   a
3   a
4   a

Array 3:
  0 1 2 3 4
0 
1
2     a a a
3
4

It doesn't matter how the resulting arrays are formed, as long as they are, and they don't share a coordinate. How should I go on doing this?

Thanks in advance :)

Recommended Answers

All 2 Replies

Hey guys!

I've got a small problem that I can't figure out. I have 2D arrays of arbitrary sizes, and they contain different characters representing different stuff (like 'a', 'b', 'c' etc.). Now, the problem is, I need to find horizontal and vertical lines of same characters and make new arrays from them without using the same coordinate twice. For example:

0 1 2 3 4 . . .
0 a a a a a
1   a
2   a a a a
3   a
4   a
.
.

This would result like this:

Array 1:
  0 1 2 3 4
0 a a a a a
1
2
3
4

Array 2:
  0 1 2 3 4
0 
1   a
2   a
3   a
4   a

Array 3:
  0 1 2 3 4
0 
1
2     a a a
3
4

It doesn't matter how the resulting arrays are formed, as long as they are, and they don't share a coordinate. How should I go on doing this?

Thanks in advance :)

Let's start out by calling the first array the source array. And the new arrays will be numbered, like array1, array2, etc. And we will need a third array for use as a temporary scratch pad array which we can discard when we are done. We'll call this scratch pad array the reference array. Also, I will use the word "print" to mean inputting data to an array, like "Print "a" into array1".

Now I'm not sure if you meant that the source array could have mixed kinds, like A's and B's etc. You only show A's being used in the source array. In any case, we will start with just one kind of letter in the source array, and worry about complications later.

So the basic idea would be to just copy a horizontal line from the source array into array1. Then copy a vertical line, then continue to alternate between copying horizontal and vertical lines, or else if you did all of the horizontal lines first then that would severely chop up the vertical space to much. Better to share the space between horizontal and vertical lines.

So now when you copy the first horizontal line into array1, you also copy that line into the reference array. Then when you go to copy the next vertical line into array2, before you print each letter into array2 you check the reference array to see if that coordinate is already used, and if so, don't print that letter and just skip to the next letter in line. However, if you find that coordinate is unused, then go ahead and print that letter into array2, and also into the reference array. Then copy the next horizontal line in the source array into array3, but check the reference array for each coordinate first, etc.

When you are done, the reference array will equal all of the new arrays laid on top of each other, as if in layers (although it can be discarded). And if you have mixed letters in your source array then go back and do each type letter the same way.

Thanks, MandrewP! The even distribution between horizontal and vertical segments is a nice feature, even though it's not mandatory for my purpose, it's really nice to have it anyway. I also got some code examples from gamedev.net for this, so I think I'm good to go. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.