Java Reference
In-Depth Information
Consider how findOrg identifies organism 1 . In main , when i = 0 and j = 1 , G[0][1] is 1 , so the call
findOrg(G, 0, 1, ...) will be made with G as follows:
0 1 0 1 1 1 0
0 0 1 1 0 0 0
1 1 0 1 0 0 1
1 0 1 0 0 1 1
1 1 0 0 0 1 0
In findOrg , since G[0][1] is 1 , it will be set to 2 , and the four calls to findOrg will be made as follows:
findOrg(G, -1, 1, ...); //immediate return since i < 0
findOrg(G, 0, 2, ...); //immediate return since G[0][2] is 0
findOrg(G, 1, 1, ...); //immediate return since G[1][1] is 0
findOrg(G, 0, -1, ...); //immediate return since j < 0
All of these calls return immediately, so only G[0][1] is marked with a 2 .
Next, consider how findOrg identifies organism 3. In main , when i = 2 and j = 0 , G[2][0] is 1 , so the call
findOrg(G, 2, 0, ...) will be made with G as follows (organism 2 would already have been labeled with 3 ):
0 2 0 3 3 3 0
0 0 3 3 0 0 0
1 1 0 3 0 0 1
1 0 1 0 0 1 1
1 1 0 0 0 1 0
(Remember that, during this phase, the label of an organism is 1 more than the number of the organism.) For this
example, we will use the notation N, E, S and W (rather than subscripts) to indicate a grid position to the north, east,
south, and west, respectively. At this stage, orgCount is 3 so that the cells will be labeled with 4 .
The following are the calls generated to findOrg from the initial findOrg(2, 0, ...) (for clarity, we omit the first
argument, G ):
findOrg(2, 0, ...) //G[2][0] is labeled with 4
findOrg(N...) //returns immediately since G[N] is 0
findOrg(E...) //G[E] is 1, relabeled with 4, gives rise to 4 calls
findOrg(N...) //returns immediately since G[N] is 0
findOrg(E...) //returns immediately since G[E] is 0
findOrg(S...) //returns immediately since G[S] is 0
findOrg(W...) //returns immediately since G[W] is 4
findOrg(S...) //G[S] is 1, relabeled with 4, gives rise to 4 calls
findOrg(N...) //returns immediately since G[N] is 4
findOrg(E...) //returns immediately since G[E] is 0
findOrg(S...) //G[S] is 1, relabeled with 4, gives rise to 4 calls
findOrg(N...) //returns immediately since G[N] is 4
findOrg(E...) //G[E] is 1, relabeled with 4, gives rise to 4 calls
findOrg(N...) //returns immediately since G[N] is 0
findOrg(E...) //returns immediately since G[E] is 0
findOrg(S...) //returns immediately since G[S] is outside grid
findOrg(W...) //returns immediately since G[W] is 4
findOrg(S...) //returns immediately since G[S] is outside grid
Search WWH ::




Custom Search