#define _ISOC99_SOURCE #include #include #include typedef struct { int x; int y; } pair; void transform(int n, pair *grille, int grille_n) { int i; for (i = 0; i < grille_n; ++i) { int x = n - 1 - grille[i].y; int y = grille[i].x; grille[i].x = x; grille[i].y = y; } } #define hash(g) ((10000 * (g)->y) + (g)->x) int cmp_grille(const void *_a, const void *_b) { pair *a = (pair*) _a; pair *b = (pair*) _b; return hash(a) - hash(b); } void print_the_shit(int n, int grille_n, pair *grille, char *text) { int limit = n, i; while (limit--) { qsort(grille, grille_n, sizeof(*grille), cmp_grille); for (i = 0; i < grille_n; ++i) { //printf("%i %i %c\n", grille[i].x, grille[i].y, *(text + n * grille[i].y + grille[i].x)); printf("%c", *(text + n * grille[i].y + grille[i].x)); } transform(n, grille, grille_n); } printf("\n"); } int main () { for (;;) { int n, count = 0, i, j; char c; scanf("%i\n", &n); if (!n) return 0; pair *grille = malloc(sizeof(*grille) * (n * n + 1)); char *text = malloc(sizeof(*text) * (n * n + 1)); for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { scanf("%c", &c); if ('O' == c) { grille[count].y = i; grille[count].x = j; ++count; } } scanf("\n"); grille[count].x = grille[count].y = 0; } for (i = 0; i < n; ++i) { scanf("%s", text + i * n); } print_the_shit(n, count, grille, text); } return 0; }