0

A text file is given for the computer to solve a puzzle problem. In a given text file, say

  b   bab    abba
 aa     a    baba    b     

The largest block of text is the "target puzzle", where all pieces of the puzzle are separated by white space. In the final result, the algorithm should be able to parse the "pieces" or the puzzle from the "target" puzzle. The largest block of text is implicitly the target puzzle.

How could I parse these pieces and puzzle, given the fact that they span over multiple lines? One could easily parse the separated blocks on one line, but how could I connect the parsed pieces of each line into a "block" object, or somethings similar. Right now I am working with 2D arrays, any help with this specific parsing problem is greatly appreciated.

1 Answers1

0

I think, step one for me would be to get the exact representation into some structure that you can easily manipulate. I agree, that a multidimensional array seems like the most reasonable model, as it closely fits the concept.

You need to read in each character into a new char[]. You can do that using the code from this question. That's your step one. Getting each char into an array and each array into a char[][].

Step two is the proximity checking. Let's look at some possibilities.

---1---1---11
---1-------11

So you know that you can access each member here. I can't think of a better way to check each element than something close to O(n^2). Not nice, but it's something for you to build on.

  • Go through a line until you hit a value that is not white space.
  • From this point, you're going to find everything on the same line as it. This means incrementing the current index until you either hit more white space, or the end of the array.
  • Next is finding out what's below. You now know where the values start and finish. So you need to go back and check between your start index and your finish index, each time going down the multi dimensional array.
  • For each value you hit, increment some global pointer.

Following the example above, you'd go..

---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.

Then you'd move onto the next one. Now, for the sake of giving some complete examples, I'll do the last one to show you the algorithm at work.

---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.
Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89