algorithm - Updating an grid of "players" by replacing losers with copies of the winner -
i have two-dimensional array of objects. each object has, @ times, (variable) score (i.e. object's score @ time t not object's score @ time t+1). want find efficient algorithm duplicate object greater score neighbour, , place duplicate neighbour's place.1
my first impulse naive solution:
- create copy of array
- set "changewasmade" flag false
- cycle through positions p
- compare scores neighbours n
- if score(p) > score(n), replace n in copied grid copy of p , set "changewasmade" true
- if "changewasmade", discard original grid, , repeat using copy new original; else, return copy
however, n x n array, seems o(n4) (n2 possible iterations of n2 checks), seems pretty slow me. since algorithmic knowledge pretty poor, thought wise ask if there's quicker way this.
update
it's occurred me might quicker make 1 "pass" of replacements, , check newly created (i.e. cloned) objects have highest score of neighbours (i.e. local maximum). if are, great, right thing has happened - if they're not, replace them copy of neighbour highest score. cut down on required iterations (though require book-keeping keep straight!) - there still quicker method?
** footnotes**
- (for context, have read "the quantum thief", setup of prison described in opening pages)
unless i'm reading problem wrong, obvious solution seems be:
- create blank array of same size, call old array
old
, new arraynew
. - for each cell
new[i][j]
in new array, set maximum of 5 possible values:old[i][j]
,old[i-1][j]
,old[i][j-1]
,old[i+1][j]
,old[i][j+1]
. new
updated array timestep t+1.
Comments
Post a Comment