CH3-UVA1589

UVA1589

这题是题模拟题, 模拟题最重要的是考虑清楚细节.我一开始的思路没有错.但是读入数据的时候出错了,调了半天.用C来读取数据或写程序要注意的细节真的很多.如果效率上过得去,尽量使用C++来写.

//
// Created by sixleaves on 16/11/24.
//

#include 
#include 

int board[11][10] = {0};

#define MAX(x, y) ((x) > (y)? (x) : (y))

bool aviableGPos(int x, int y) {

    if (x >= 1 && x <=3 && y >=4 && y <=6) return true;
    if (x >= 8 && x <= 10 && y >=4 && y <=6) return true;
    return false;
}

bool aviableBoardPos(int x, int y) {

    if (x >=1 && x <=10 && y >=1 && y <=9) return true;
    return false;
}

bool isleg(int hx, int hy, int gx, int gy) {

    // 竖直方向
    int d = hx - gx < 0? gx -hx : hx -gx;
    if (d == 2) {
        // 马再将下面
        if (hx > gx) {

            if (board[hx - 1][hy] != 0) return true;
            return false;
        }else {
            if (board[hx + 1][hy] !=0) return true;
            return false;
        }

    }else { // 水平方向
        d = hy - gy < 0? gy -hy : hy -gy;
        if (2 == d) {
            // 马在将水平方向的右侧
            if (hy > gy) {
                if (board[hx][hy - 1] != 0) return true;
                return false;
            } else {
                if (board[hx][hy + 1] != 0) return true;
                return false;
            }
        }
        return true;
    }

}


// == -1表示该位置不合法. 0表示该位置不会将死, 1表示该位置会被将死
int checkmate(int x, int y, int lastX, int lastY) {

    int ischeckmate = 0;
    char eatPiece ;
    // 将军的位置是合法的
    if (!aviableGPos(x, y)) return -1;
    else {
        // 吃掉棋子或者移动到该位置
        eatPiece = board[x][y];
        board[x][y] = 0;
    }

    for (int i = 1; i <= 10; i++) {

        for (int j = 1; j <= 9; j++) {

            switch (board[i][j]) {
                // chariot
                case 'R': {

                    if (x == i) {
                        int maxC = MAX(j, y);
                        int minC = j + y - maxC;
                        ischeckmate = 1;
                        for (int k = minC + 1; k < maxC; k++) if (board[i][k] != 0) ischeckmate = 0;
                    }

                    if (y == j) {
                        int maxC = MAX(x, i);
                        int minC = i + x - maxC;
                        ischeckmate = 1;
                        for (int k = minC + 1; k < maxC; k++) if (board[k][j] != 0) ischeckmate = 0;
                    }

                    // 如果能将死, 则需要悔棋
                    if (ischeckmate == 1) {

                        board[x][y] = eatPiece;
                        return 1;
                    }

                    break;
                }
                case 'H': {

                    int dx[] = {2, 2, -2, -2, 1, -1, 1, -1};
                    int dy[] = {1, -1, 1, -1, 2, -2, -2, 2};

                    for (int k = 0; k < 8; k++) {



                        int hx = i + dx[k];
                        int hy = j + dy[k];
                        if (hx == x && hy == y) {
                            // check horse leg
                            ischeckmate = 0;
                            if (!isleg(i, j, x, y))
                                ischeckmate = 1;
                            break;
                        }

                    }


                    if (ischeckmate == 1) {

                        board[x][y] = eatPiece;
                        return 1;
                    }

                    break;
                }
                case 'G': {

                    if (y == j) {
                        int maxC = MAX(x, i);
                        int minC = i + x - maxC;
                        ischeckmate = 1;
                        for (int k = minC + 1; k < maxC; k++) if (board[k][j] != 0) ischeckmate = 0;
                    }


                    if (ischeckmate == 1) {
                        board[x][y] = eatPiece;
                        return 1;
                    }

                    break;
                }
                case 'C': {

                    if (x == i) {
                        int maxC = MAX(j, y);
                        int minC = j + y - maxC;
                        int count = 0;
                        for (int k = minC + 1; k < maxC; k++) if (board[i][k] != 0) count++;
                        if (count == 1) ischeckmate = 1;
                    }

                    if (y == j) {
                        int maxC = MAX(x, i);
                        int minC = i + x - maxC;
                        int count = 0;
                        for (int k = minC + 1; k <  maxC; k++) if (board[k][j] != 0) count++;
                        if (count == 1) ischeckmate = 1;
                    }


                    if (ischeckmate == 1) {
                        board[x][y] = eatPiece;
                        return 1;
                    }

                    break;
                }
                default:
                    break;
            }

        }
    }

    board[x][y] = eatPiece;
    return ischeckmate;

}

char readchar() {
    char ch;
    for (;;) {
        ch = getchar();
        if (ch != ' ' && ch != '\r' && ch != '\n') return ch;
    }
}

int main() {

    int n, x,y;

    while ((scanf("%d%d%d", &n, &x, &y) == 3) && n && x && y) {

        memset(board, 0, sizeof(board));

        for (int i = 0; i < n; i++) {
            char ch;
            int chx, chy;
            ch = readchar();
            scanf("%d%d",&chx, &chy);
            board[chx][chy] = ch;
        }

        bool ischeckMate = true;
        int dx[] = {1, -1, 0, 0};
        int dy[] = {0, 0, 1, -1};

        // 先检查是否是fly

        for (int i = 0; i < 4; i++) {
            // 试走, 如果不是死期, 则证明这步是可以走的.那么不是死局
            int flag = checkmate(x + dx[i], y +dy[i], x, y);
            if (0 == flag) ischeckMate = false;
        }

        if (ischeckMate) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

你可能感兴趣的:(CH3-UVA1589)