c++ - What is the significance of initializing direction arrays below with given values when developing chess program? -
i new competitive programming, , noticed frequently, many of great coders have these 4 lines in code (particularly in involving arrays):
int di[] = { 1, -1, 0, 0, 1, -1, 1, -1 }; int dj[] = { 0, 0, 1, -1, 1, -1, -1, 1 }; int dik[] = { -2, -2, -1, 1, 2, 2, 1, -1 }; int djk[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
what signify , technique used for?
this technique encode directions arrays - every pair of di[i],dj[i]
different direction.
if imagine have piece @ location x,y, , want add onto x , y value move nearby location, 1,0 east, -1,0 west, 0,1 south, 0,-1 north , on.
(here have said top left 0,0 , bottom right 4,4 , shown move each index of arrays make central point, x, @ 2,2.)
..... .536. .1x0. .724. .....
the way set up, if ^1
(^
being bitwise xor) on index opposite direction - 0 , 1 opposites, 2 , 3 opposites , on. (another way set go clockwise starting @ north - ^4
gets opposite direction.)
now can test directions given point looping on di
, dj
arrays, instead of needing write out each direction on own line (for 8 in total!) (just don't forget bounds checking :) )
dik
, djk
form knights directions instead of adjacent directions. here, ^1
flip along 1 axis, ^4
give opposite knight leap.
.7.6. 0...5 ..k.. 1...4 .2.3.
Comments
Post a Comment