#include #include #include #define a_MIN_A 'a' - 'A' struct pos { int x; int y; }; pos figs[8][2]; pos pows[2][8]; int posfig[256]; int lastpow[2]; char names[] = { 'K', 'Q', 'R', 'R', 'B', 'B', 'N', 'N' }; void sortWhite(); int main(void) { char str[500]; int ch; int isBlack; struct pos *act; posfig['K'] = 0; posfig['Q'] = 1; posfig['R'] = 2; posfig['B'] = 4; posfig['N'] = 6; lastpow[0] = lastpow[1] = 0; for (int i = 0; i < 8; ++i) for (int j = 0; j < 2; ++j) { figs[i][j].x = -1; pows[j][i].x = -1; } for (int i = 0; i < 8; ++i) { scanf("%s", str); //printf("_0_%c_ ", getchar()); getchar(); for (int j = 0; j < 8; ++j) { //printf("_1_%c_ ", getchar()); //printf("_2_%c_ ", getchar()); getchar(); getchar(); ch = getchar(); if ((ch == '.') || (ch == ':')) { //printf("_3_%c_ ", getchar()); getchar(); continue; } if (islower(ch)) { isBlack = 1; ch -= a_MIN_A; } else { isBlack = 0; } if (ch == 'P') { act = &pows[isBlack][lastpow[isBlack]++]; //printf("P %d, %d %d\n", i, j, isBlack); } else { if (figs[posfig[ch]][isBlack].x == -1) { act = &figs[posfig[ch]][isBlack]; } else { act = &figs[posfig[ch]+1][isBlack]; } } act->x = j; act->y = i; //printf("_4_%c_ ", getchar()); getchar(); } while ((ch = getchar()) != '\n'); } sortWhite(); for (int j = 0; j < 2; ++j) { printf("%s: ", (j) ? "Black" : "White"); bool first = true; for (int i = 0; i < 8; ++i) { if (figs[i][j].x != -1) { if (!first) printf(","); printf("%c%c%d", names[i], figs[i][j].x + 'a', 8 - figs[i][j].y); first = false; } } for (int i = 0; i < 8; ++i) { if (pows[j][i].x != -1) { if (!first) printf(","); //printf("%d %d", pows[j][i].x , pows[j][i].y); printf("%c%d", pows[j][i].x + 'a', 8 - pows[j][i].y); first = false; } } printf("\n"); } return 0; } int cmp(const void *a, const void *b) { const pos *A = (const pos*)a; const pos *B = (const pos*)b; if (A->y > B->y) return -1; if (A->y == B->y) if (A->x < B->x) return -1; return 1; return 1; } void swapWhFig(int i, int j) { pos tmp; tmp.x = figs[i][0].x; tmp.y = figs[i][0].y; figs[i][0].x = figs[j][0].x; figs[i][0].y = figs[j][0].y; figs[j][0].x = tmp.x; figs[j][0].y = tmp.y; } void sortWhite() { qsort(pows[0], sizeof(pos), 8, cmp); for (int i = 2; i < 8; i+=2) { if (cmp(&figs[i][0], &figs[i+1][0]) > 0) { swapWhFig(i, i+1); } } }