关闭

Fuzhou 2011 Xiangqi

382人阅读 评论(0) 收藏 举报

今天跟人做这个,超级模拟,wa了七次……

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int ch_x[4] = {-1, 0, 1, 0};
const int ch_y[4] = {0, 1, 0, -1};
const int ho_x[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
const int ho_y[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
int board[20][20];      //1: red, 2: black
int blackGeneral_x, blackGeneral_y;
int total_red, red_x[10], red_y[10];
char kind[10];

int JudgeLine(int i, int x, int y)
{   //Count how many pieces there are between (red_x[i], red_y[i]) and (x, y)
    int res = 0;
    if(red_x[i] == x) {
        if(red_y[i] < y) {
            for(int j = red_y[i] + 1; j != y; j++) {
                if(board[x][j] != 0)
                    res++;
            }
        }
        else {
            for(int j = red_y[i] - 1; j != y; j--) {
                if(board[x][j] != 0)
                    res++;
            }
        }
        return res;
    }
    else if(red_y[i] == y) {
        if(red_x[i] < x) {
            for(int j = red_x[i] + 1; j != x; j++) {
                if(board[j][y] != 0)
                    res++;
            }
        }
        else {
            for(int j = red_x[i] - 1; j != x; j--) {
                if(board[j][y] != 0)
                    res++;
            }
        }
        return res;
    }
    return -1;
}

bool JudgeChariot(int i, int x, int y)
{   //chariot can eat black general
    if(JudgeLine(i, x, y) == 0)
        return true;
    return false;
}

bool JudgeHorse(int i, int x, int y)
{   //whether horse can eat black general
    for(int j = 0; j < 8; j++) {
        int tmpx = red_x[i] + ho_x[j];
        int tmpy = red_y[i] + ho_y[j];
        int footx = red_x[i] + ch_x[j / 2];
        int footy = red_y[i] + ch_y[j / 2];
        if(tmpx < 1 || tmpx > 10 || tmpy < 1 || tmpy > 9)
            continue;
        if(footx < 1 || footx > 10 || footy < 1 || footy > 9)
            continue;
        if(board[footx][footy] != 0)
            continue;
        if(tmpx == x && tmpy == y)
            return true;
    }
    return false;
}

bool JudgeCannon(int i, int x, int y)
{   //whether connon can eat black general
    if(JudgeLine(i, x, y) == 1)
        return true;
    return false;
}

int main()
{
    //freopen("data.in", "rb", stdin);
    while(scanf("%d%d%d", &total_red, &blackGeneral_x, &blackGeneral_y) != EOF) {
        if(!total_red && !blackGeneral_x && !blackGeneral_y)
            break;
        memset(board, 0, sizeof(board));
        for(int i = 0; i < total_red; i++) {
            char tmp;
            cin >> tmp >> red_x[i] >> red_y[i];
            kind[i] = tmp;
            board[red_x[i]][red_y[i]] = 1;
        }
  
        bool isCheckmate = true;
        for(int i = 0; i < 4; i++) {
            int x = blackGeneral_x + ch_x[i];
            int y = blackGeneral_y + ch_y[i];
            if(!((x >= 1 && x <= 3 && y >= 4 && y <= 6) || (x >= 8 && x <= 10 && y >= 4 && y <= 6)))
                continue;
            int tmp = board[x][y];
            board[x][y] = 2;        //black general moves to this coodinate
            bool win = false;
            for(int j = 0; j < total_red; j++) {
                if(board[red_x[j]][red_y[j]] != 1)  //whether the piece of red has been eaten by black general
                    continue;
                switch(kind[j]) {
                    case 'G': win = JudgeChariot(j, x, y);  //red general can attack black general in the same way as chariot
                              break;
                    case 'R': win = JudgeChariot(j, x, y);
                              break;
                    case 'H': win = JudgeHorse(j, x, y);
                              break;
                    case 'C': win = JudgeCannon(j, x, y);
                              break;
                }
                if(win)
                    break;
            }
            if(!win) {
                isCheckmate = false;
                break;
            }
            board[x][y] = tmp;
        }
        
        if(isCheckmate)
            printf("YES\n");
        else
            printf("NO\n");
    }
    
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:94106次
    • 积分:2007
    • 等级:
    • 排名:第19167名
    • 原创:111篇
    • 转载:1篇
    • 译文:1篇
    • 评论:26条
    文章分类
    最新评论