Problem B

Another easy problem. Only simulation. But there is a *one-character-bug* in my code. I typed:

kc = pos[3][1] - '0';

But actually, it should be:

kc = pos[3][1] - '1';

And I lost some hundred points XD. Got it passed in the practice room, here is my code:

char pos[5][5];
int M[10][10], TMP[10][10];
int kr, kc;

inline bool valid (int r, int c) {
    return r >= 0 && r < 8 && c >= 0 && c < 8;
}

inline void place (const char* s, int id) {
    int r = s[0] - 'a';
    int c = s[1] - '1';
    M[r][c] = id;
}

inline bool check () {
    int R, C;
    REP (i, 8 ) REP (j, 8 ) if (M[i][j] == 3) R = i, C = j;
    REP (i, 8 ) REP (j, 8 ) {
        if (M[i][j] == 1) {
            if (R == i || C == j) {
                bool flag = true;
                if (R == i) {
                    for (int p = min (C, j) + 1; p < max (C, j); p++) {
                        if (M[i][p] != 0) {
                            flag = false;
                            break;
                        }
                    }
                } else {
                    for (int p = min (R, i) + 1; p < max (R, i); p++) {
                        if (M[p][j] != 0) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (flag)
                    return true;
            }
        } else if (M[i][j] == 2) {
            if (abs (i - R) <= 1 && abs (j - C) <= 1) {
                return true;
            }
        }
    }
    return false;
}

const int mr[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int mc[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

inline bool checkmate () {
    if (!check ()) { return false;}
    REP (k, 8 ) {
        REP (i, 8 ) REP (j, 8 ) TMP[i][j] = M[i][j];
        if (valid (kr + mr[k], kc + mc[k])) {
            M[kr][kc] = 0;
            M[kr + mr[k]][kc + mc[k]] = 3;
        }
        if (!check ()) {
            return false;
        }
        REP (i, 8 ) REP (j, 8 ) M[i][j] = TMP[i][j];
    }
    return true;
}

int main () {
    REP (i, 4) scanf ("%s", pos[i]);

    place (pos[0], 1);
    place (pos[1], 1);
    place (pos[2], 2);
    place (pos[3], 3);

    kr = pos[3][0] - 'a';
    kc = pos[3][1] - '1'; // <- before, this is kc = pos[3][1] - '0';

    if (checkmate ()) puts ("CHECKMATE");
    else puts ("OTHER");

    return 0;
}
Advertisements