Created
February 9, 2009 21:47
-
-
Save orbekk/61033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "tour.h" | |
#include <stdio.h> | |
#define IN_BOUND(x,y) ((x)>=0 && (y)>=0 && (x) < size && (y) < size) | |
int movesx[] = { -2, -1, 1, 2, 2, 1, -1, -2 }; | |
int movesy[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; | |
int tour_length_f1(int size, int startx, int starty, int *moves) { | |
int curx = startx; | |
int cury = starty; | |
int board[size][size]; | |
for (int i=0; i<size; i++) | |
for (int j=0; j<size; j++) | |
board [i][j] = 0; | |
board[startx][starty] = 1; | |
int *move = moves; | |
int thismove; | |
int length; | |
for (length=1; length <= size*size; length++) { | |
thismove = *move; | |
int try; | |
for (try=0; try<8; try++) { | |
thismove = (thismove + 1) & 7; // termos-hack % 8; | |
int tryx = curx + movesx[thismove]; | |
int tryy = cury + movesy[thismove]; | |
if (IN_BOUND(tryx,tryy) && board[tryx][tryy] == 0) { | |
board[tryx][tryy] = length + 1; | |
curx = tryx; | |
cury = tryy; | |
break; | |
} | |
} | |
if (try == 8) | |
break; | |
move++; | |
} | |
return length; | |
} | |
int tour_length_f2(int size, int startx, int starty, int *directions) { | |
int curx = startx; | |
int cury = starty; | |
int *thisdir = directions; | |
int board[size][size]; | |
for (int i=0; i<size; i++) | |
for (int j=0; j<size; j++) { | |
board [i][j] = *thisdir; | |
thisdir++; | |
} | |
int length; | |
for (length=1; length <= size*size; length++) { | |
int try; | |
int thismove = board[curx][cury]; | |
for (try=0; try<8; try++) { | |
thismove = (thismove + 1) & 7; | |
int tryx = curx + movesx[thismove]; | |
int tryy = cury + movesy[thismove]; | |
if (IN_BOUND(tryx, tryy) && board[tryx][tryy] >= 0) { | |
board[curx][cury] = -length; | |
curx = tryx; | |
cury = tryy; | |
break; | |
} | |
} | |
if (try == 8) | |
break; | |
} | |
/* if (length == size*size) { */ | |
/* board[curx][cury] = 64; */ | |
/* printf("\n\nfitness: %d\n", length); */ | |
/* for (int i=0; i<size; i++) { */ | |
/* for (int j=0; j<size; j++) { */ | |
/* printf("%4d", -board[i][j]); */ | |
/* } */ | |
/* printf("\n\n"); */ | |
/* } */ | |
/* } */ | |
return length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment