For the purposes of this assignment, a map is a two-dimensional array in which each location holds a value of type char. Two locations are considered to be neighbors if one is immediately to the "east", "west", "north", or "south" of the other (i.e., either their row numbers or column numbers differ by one, but not both). A path on a map is a sequence of locations such that consecutive locations in the sequence are neighbors of one another.
A region on a map is a set of locations on that map. Two locations are in the same region if and only if they are connected by a path in which every cell contains the same character.
Provided are the following Java artifacts and sample data file:
Enter input file name: map1.txt The map: A A A C C B A A A C C C B B A C A A C C C B A C C B B B B A A C A C B B A A A B A B B C C C C B Region IDs: 1 1 1 2 2 3 4 4 1 2 2 2 3 3 4 5 1 1 2 2 2 3 4 5 6 7 7 7 7 4 4 5 8 9 7 7 4 4 4 10 8 7 7 11 11 11 11 10 |
The program reads the map from the input file, passes that map to the constructor of MapRegionFinder class, and then uses the resulting object's methods to display the original map followed by an "overlay" that shows, for each map location, in which region it lies. Each region is identified by a positive integer; region numbers form a consecutive range of postive integers starting at one and ending with the number of distinct regions.
You will notice a stubbed method, computeRegions(), in the MapRegionFinder class. (It is a private method, and thus you can rename it if you like.) What it should do is to iterate over every location in the map, and, for each location L that has yet to be identified with a region ID, it should "explore from" that location by calling a separate method that is recursive. The result of that call should be that every location in the same region as L has been marked as being so. (Which is to say that all the elements of the instance variable regionID[][] corresponding to locations in that region have had the same value placed into them.)
The recursive method just mentioned, for which exploreRegion()
is an appropriate name, could work as follows:
It would receive three parameters, two of which identify a location
L (by its row/column coordinates) and the third of which is the
region ID that should be assigned to that location.
After assigning to L its region ID, the method would check each of
L's neighbors and, for each one belonging in the same region as L
but not yet having been assigned a region ID, it would make a
recursive call to "explore from" that neighbor location).
Also include in this class a method called
callCount() that returns how many times the exploreRegion()
method called itself recursively.
Enter input file name: map1.txt The map: A A A C C B A A A C C C B B A C A A C C C B A C C B B B B A A C A C B B A A A B A B B C C C C B Region IDs: 1 1 1 2 2 3 4 4 1 2 2 2 3 3 4 5 1 1 2 2 2 3 4 5 6 7 7 7 7 4 4 5 8 9 7 7 4 4 4 10 8 7 7 11 11 11 11 10 Region IDs (wraparound): 1 1 1 2 2 3 1 1 1 2 2 2 3 3 1 4 1 1 2 2 2 3 1 4 4 5 5 5 5 1 1 4 1 6 5 5 1 1 1 7 1 5 5 2 2 2 2 7 |
You will notice that the first constructor in the MapRegionFinder class has a second parameter, of type boolean. Its purpose is to tell the constructor whether or not the new object should interpret the map that is given to it as having a wraparound nature. In a wraparound map, locations in the same column of the "northernmost" and "southernmost" rows are considered to be neighbors, and likewise elements in the same row of the "westernmost" and "easternmost" columns. Under this interpretation, every location on a map has exactly four neighbors. Of course, when a map is interpreted in a wraparound fashion, it is likely to have fewer distinct regions than one interpreted in the "normal" way.
To test this feature, "uncomment" the relevant code segment in the doStuff() method of MapRegionFinderApp. In that case, the program would produce as output (when given the map1.txt file as input) that shown to the right.
Notice that, when the map is interprted in the wraparound mode, what had been regions 1, 4, and 8 become a single region, as do regions 2 and 11, as do regions 5 and 6, which explains why there were eleven regions when interpreting the map in the normal way but only seven when interpreting it in the wraparound fashion.