#include <stdio.h>
typedef struct data {
const char *element;
int visited;
} Data;
#define Size 3
Data Board[Size][Size] = {
{{ "XY", 0 }, { "YX", 0 },{ "XX", 0 }},
{{ "XX", 0 }, { "YY", 0 },{ "XY", 0 }},
{{ "YX", 0 }, { "XY", 0 },{ "XX", 0 }}
};
#define PathLen (Size*Size)
int Path[PathLen];
Data *pos(int *x, int *y){
if(*x < 0) *x += Size;
if(*x >= Size) *x -= Size;
if(*y < 0) *y += Size;
if(*y >= Size) *y -= Size;
return &Board[*y][*x];
}
void neighbor(int x, int y, int wx, int wy, int level);
void search_path(int x, int y, int level){
Path[level] = Size * y + x;
if(level == PathLen - 1){
int i;
for(i=0;i<PathLen;++i){
int x = Path[i] % Size;
int y = Path[i] / Size;
if(i == PathLen - 1)
printf("%s\n", Board[y][x].element);
else
printf("%s->", Board[y][x].element);
}
} else {
neighbor(x, y, x - 1, y, level);//origin -> left
neighbor(x, y, x + 1, y, level);//origin -> right
neighbor(x, y, x, y - 1, level);//origin -> up
neighbor(x, y, x, y + 1, level);//origin -> down
}
}
//subroutine
//origin(x,y) -> neighbor(wx,wy)
void neighbor(int x, int y, int wx, int wy, int level){
Data *wk;
wk = pos(&wx,&wy);
if(wk->visited == 0 &&
(Board[y][x].element[0] == Board[wy][wx].element[0] ||
Board[y][x].element[1] == Board[wy][wx].element[1])){
wk->visited = 1;
search_path(wx, wy, level + 1);
wk->visited = 0;
}
}
int main(void){
int x = 0, y = 0, level = 0;
Board[0][0].visited = 1;
search_path(x, y, level);
return 0;
}
/*
XY->XX->YX->YY->XY->XX->YX->XX->XY
XY->XX->YX->YY->XY->XX->YX->XX->XY
XY->XX->YX->YY->XY->XX->XY->XX->YX
XY->XX->XY->XX->YX->XX->XY->YY->YX
XY->XX->XY->XX->YX->YY->XY->XX->YX
XY->XX->YX->XX->XY->YY->XY->XX->YX
XY->XX->YX->XX->XY->YY->YX->XX->XY
XY->XX->YX->XX->XY->XX->YX->YY->XY
*/