中国象棋 SPJ

· · 个人记录

#include <bits/stdc++.h>
#define PII pair<int, int>
#define fi first
#define se second

using namespace std;
const int N = 15;

/*
帅:  1
红车:2
红马:3
红炮:4
红卒:5
红士:6
红相:7
将:  8
黑车:9
黑马:10
黑炮:11
黑卒:12
黑士:13
黑象:14
*/

bool kinggo(int x, int y, int gx, int gy, int col) ;
bool shigo(int x, int y, int gx, int gy, int col)  ;
bool elpgo(int x, int y, int gx, int gy, int col)  ;
bool horgo(int x, int y, int gx, int gy, int col)  ;
bool solgo(int x, int y, int gx, int gy, int col)  ; 
bool cargo(int x, int y, int gx, int gy, int col)  ;
bool cango(int x, int y, int gx, int gy, int col)  ;

struct node {
    int type, nx, ny;
    int init(int _type, int _x, int _y) {
        type = _type;
        nx = _x;
        ny = _y;
    }
    int can_go(int x, int y) {
        if(type == 0) return false;
        if(type == 1 || type == 8) return kinggo(nx, ny, x, y, type);//帅 
        if(type == 6 || type == 13) return shigo(nx, ny, x, y, type);//士
        if(type == 7 || type == 14) return elpgo(nx, ny, x, y, type);//象
        if(type == 3 || type == 10) return horgo(nx, ny, x, y, type);//马 
        if(type == 5 || type == 12) return solgo(nx, ny, x, y, type);//卒 
        if(type == 2 || type == 9 ) return cargo(nx, ny, x, y, type);//车
        if(type == 4 || type == 11) return cango(nx, ny, x, y, type);//炮 
    }
};

int plate[N][N] = {
    {2, 3, 7, 6, 1, 6, 7, 3, 2},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 4, 0, 0, 0, 0, 0, 4, 0},
    {5, 0, 5, 0, 5, 0, 5, 0, 5},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {12, 0, 12, 0, 12, 0, 12, 0, 12},
    {0, 11, 0, 0, 0, 0, 0, 11, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {9, 10, 14, 13, 8, 13, 14, 10, 9}
};

bool isBlack(int col) {return col > 7;}
bool isRed(int col) {return 1 <= col && col <= 7;}
int getcol(node n) {
    if(n.type > 7) return 2;
    if(1 <= n.type && n.type <= 7) return 1;
    return 0;
}

node p[N][N];

bool kinggo(int x, int y, int gx, int gy, int col) {//将/帅 
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走 
    int mhdis = abs(gx - x) + abs(gy - y);
    //走出九宫格或者不是只走了一格 
    if(col == 1) {//红 
        if(mhdis != 1 || !(gy <= 6 && gx <= 3 && gy >= 4 && gx >= 1)) return false;
    } else {//黑 
        if(mhdis != 1 || !(gy <= 6 && gx <= 10 && gy >= 4 && gx >= 8)) return false;
    } 
    return true;
}

bool shigo(int x, int y, int gx, int gy, int col) {//士 
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走 
    int mhdis = abs(gx - x) + abs(gy - y);
    int qbdis = max(abs(gx - x), abs(gy - y));
    if(mhdis != 2 || qbdis != 1) return false;//判对角线 
    if(isRed(col)) {//走出九宫格 
        if(!(gy <= 6 && gx <= 3 && gy >= 4 && gx >= 1)) return false;
    } else {
        if(!(gy <= 6 && gx <= 10 && gy >= 4 && gx >= 8)) return false;
    }
    return true;
}

bool elpgo(int x, int y, int gx, int gy, int col) {//象 
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走 
    int mhdis = abs(gx - x) + abs(gy - y);
    int qbdis = max(abs(gx - x), abs(gy - y));
    if(mhdis != 4 || qbdis != 2) return false;
    int mx = (x + gx) >> 1;
    int my = (y + gy) >> 1;
    if(p[mx][my].type != 0) return false;//塞象眼
    if(isRed(col)) {//跨过楚河汉界 
        if(gx > 5) return false;
    } else {
        if(gx <= 5) return false;
    }
    return true;
}

bool horgo(int x, int y, int gx, int gy, int col) {
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走 
    int mhdis = abs(gx - x) + abs(gy - y);
    int qbdis = max(abs(gx - x), abs(gy - y));
    if(mhdis != 3 || qbdis != 2) return false;
    int bx, by;//马腿位置 
    if(abs(gx - x) == 2) {
        bx = (gx + x) >> 1;
        by = y;
    } else {
        bx = x;
        by = (gy + y) >> 1;
    }
    if(p[bx][by].type != 0) return false;//撇马腿 
    return true;
}

bool solgo(int x, int y, int gx, int gy, int col) {
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走 
    bool over = false;//是否过河 
    if(isRed(col) && x > 5) over = true;
    if(isBlack(col) && x < 6) over = true;
    int mhdis = abs(gx - x) + abs(gy - y);
    if(mhdis != 1) return false;
    if(!over) {//没过河,只能向对面走 
        if(isRed(col) && gx - x != 1) return false;
        if(isBlack(col) && x - gx != 1) return false;
    } else {//过河了,不能往回走 
        if(isRed(col) && gx - x == -1) return false;
        if(isBlack(col) && gx - x == 1) return false;
    }
    return true;
}

vector<PII> pos;

void get_ft(int x, int y, int gx, int gy) {//满足车/炮的走子规则之后的位置
    pos.clear();
    if(x < gx) {
        x++;
        while(x < gx) {
            pos.push_back(make_pair(x, y));
            x++;
        }
    } else if(gx < x) {
        gx++;
        while(gx < x) {
            pos.push_back(make_pair(gx, y));
            gx++;
        }
    } else if(y < gy) {
        y++;
        while(y < gy) {
            pos.push_back(make_pair(x, y));
            y++;
        }
    } else {
        gy++;
        while(gy < y) {
            pos.push_back(make_pair(x, gy));
            gy++;
        }
    }
} 

bool cargo(int x, int y, int gx, int gy, int col) {
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走
    if(gx != x && gy != y) return false;
    int ecnt = 0;
    get_ft(x, y, gx, gy);
    for(int i = 0; i < pos.size(); i++) {
        int dx = pos[i].fi, dy = pos[i].se;
        if(p[dx][dy].type == 0) continue;
        return false;
    }
    return true;
}

bool cango(int x, int y, int gx, int gy, int col) {
    if(gx <= 0 || gy <= 0 || gx > 10 || gy > 9) return false;
    if(getcol(p[gx][gy]) == getcol(p[x][y])) return false;//同种不能走
    if(gx != x && gy != y) return false;
    int ecnt = 0;
    get_ft(x, y, gx, gy);
    for(int i = 0; i < pos.size(); i++) {
        int dx = pos[i].fi, dy = pos[i].se;
        if(p[dx][dy].type == 0) continue;
        ecnt++;
    }
    if(p[gx][gy].type == 0) {
        if(ecnt != 0) return false;
    } else {
        if(ecnt != 1) return false;
    }
    return true;
}

void choosepos(int &rx, int &ry) {
    rx = rand() % 10 + 1;
    ry = rand() % 9 + 1;
    while(!isBlack(p[rx][ry].type)) {
        rx = rand() % 10 + 1;
        ry = rand() % 9 + 1;
    }
}

vector<PII> rfn;

void get_rfn() {
    for(int i = 1; i <= 10; i++) {
        for(int j = 1; j <= 9; j++) rfn.push_back(make_pair(i, j));
    }
    random_shuffle(rfn.begin(), rfn.end());
}

int main() {
    srand(unsigned(time(0)));
    get_rfn();
    for(int i = 1; i <= 10; i++) {
        for(int j = 1; j <= 9; j++) {
            p[i][j].init(plate[i - 1][j - 1], i, j);
        }
    }

    int step = 1;
    while(step <= 100) {
        //走棋 (px, py) -> (gx, gy) 
        int px, py, gx, gy;
        cin >> px >> py >> gx >> gy;
        if(!p[px][py].can_go(gx, gy) || !isRed(p[px][py].type)){
            cout << "wrong movement on step " << step << endl;
            return 0;
        }
        else {
            int res = p[px][py].type;
            p[px][py].init(0, px, py);
            if(p[gx][gy].type == 8) {
                if(step <= 50) {
                    cout << "AC:killed in " << step << " steps!";
                    return 0;
                }
                else {
                    double scr;
                    if(step <= 60) scr = 0.9;
                    else if(step <= 70) scr = 0.8;
                    else if(step <= 80) scr = 0.7;
                    else if(step <= 90) scr = 0.6;
                    else scr = 0.5;
                    cout << "killed in " << step << " steps, got " << (int)scr * 100 << " percent of scores!";
                    return 0;
                }
            }
            p[gx][gy].init(res, gx, gy);
        }

        //系统走棋
        int rx, ry, go_x = 0, go_y = 0;
        while(go_x == 0 && go_y == 0) {
            choosepos(rx, ry);
            for(int i = 0; i < rfn.size(); i++) {
                int cx = rfn[i].fi, cy = rfn[i].se;
                if(p[rx][ry].can_go(cx, cy)) {
                    go_x = cx;
                    go_y = cy;
                    break;
                }
            }
        }
        cout << rx << " " << ry << " " <<  go_x << " " << go_y << endl;
        if(p[go_x][go_y].type == 1) {
            cout << "You are killed by AI!";
            return 0;
        }
        p[go_x][go_y].type = p[rx][ry].type;
        p[rx][ry].type = 0;
        step++; 
    }

    cout << "WA:too many steps!" << endl;
    return 0;
}