• POJ 4001 xiangqi(模拟)


    原题链接:

    题意简述:黑方只有一个棋子,将。红方有四种棋子,现在红方已走一步,轮到黑方走了,请问黑方是否必败。

    解题思路:黑方最多有四种走法,分别判断这四个位置上黑方是否会输,如果全都会输,则必败。

    错误报告:这题需要注意不能用%c或者getchar()读入,因为数据似乎不标准,这样读会出错,保险的做法是将字符当作字符串读入。另外只需要考虑全面一点,就可以了,要仔细一点。

    代码示例:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    int n,rr,cc;
    int gr,gc;
    const int maxn = 15;
    struct Node{
        int r,c;
        char type;
        bool flag;
    }qizi[maxn];
    int Map[15][15];
    bool Read(){
        memset(Map,0,sizeof Map);
        scanf("%d%d%d",&n,&rr,&cc);
        if(n == 0 && rr == 0 && cc == 0)  return false;
        for(int i = 1;i <= n;i++){
            cin >>qizi[i].type>>qizi[i].r>>qizi[i].c;
            //scanf("%c%d%d",&qizi[i].type,&qizi[i].r,&qizi[i].c);
            qizi[i].flag = true;
            if(qizi[i].type == 'G') gr = qizi[i].r,gc = qizi[i].c;
            Map[qizi[i].r][qizi[i].c] = i;
            //printf("%c %d %d
    ",qizi[i].type,qizi[i].r,qizi[i].c);
        }
        return true;
    }
    bool survival(int r,int c){
       // printf("%d %d
    ",r,c);
        for(int j = 1;j <= n;j++){
            if(qizi[j].flag == false)   continue;
            char t = qizi[j].type;
            int xr = qizi[j].r;
            int xc = qizi[j].c;
           // printf("%c %d %d
    ",t,xr,xc);
            if(t == 'G'){
                bool flag = true;
                for(int i = r+1;i < xr;i++)
                    if(Map[i][c])   flag = false;
                if(flag && xc == c) return false;
            }else if(t == 'R'){
                bool flag = true;
                if(xc == c){
                    for(int i = r+1;i < xr;i++)
                        if(Map[i][c] != 0){
                            flag = false;
                            break;
                        }
                    for(int i = xr+1;i < r;i++)
                        if(Map[i][c] != 0){
                            flag = false;
                            break;
                        }
                }else if(xr == r){
                    for(int i = c+1;i < xc;i++)
                        if(Map[r][i] != 0){
                            flag = false;
                            break;
                        }
                     for(int i = xc+1;i < c;i++)
                        if(Map[r][i] != 0){
                            flag = false;
                            break;
                        }
                }else flag = false;
                if(flag) return false;
            }
            else if(t == 'C'){
                if(xc == c){
                    int cnt = 0;
                    for(int i = r+1;i < xr;i++)
                        if(Map[i][c] != 0){
                            cnt++;
                        }
                    for(int i = xr+1;i < r;i++)
                        if(Map[i][c] != 0){
                            cnt++;
                        }
                    if(cnt == 1) return false;
                }else if(xr == r){
                    int cnt = 0;
                    for(int i = c+1;i < xc;i++)
                        if(Map[r][i] != 0){
                            cnt++;
                        }
                    for(int i = xc+1;i < c;i++)
                        if(Map[r][i] != 0){
                            cnt++;
                        }
                    if(cnt == 1) return false;
                }
            }else if(t == 'H'){
                if(xr - 2 > 0){
                    if(Map[xr-1][xc] == 0){
                        if(xr - 2 == r && xc - 1 == c)  return false;
                        if(xr - 2 == r && xc + 1 == c)  return false;
                    }
                }
                if(xr + 2 < 11){
                    if(Map[xr+1][xc] == 0){
                        if(xr + 2 == r && xc - 1 == c)  return false;
                        if(xr + 2 == r && xc + 1 == c)  return false;
                    }
                }
                if(xc - 2 > 0){
                    if(Map[xr][xc-1] == 0){
                        if(xr + 1 == r && xc - 2 == c)  return false;
                        if(xr - 1 == r && xc - 2 == c)  return false;
                    }
                }
                if(xc + 2 < 10){
                    if(Map[xr][xc+1] == 0){
                        if(xr + 1 == r && xc + 2 == c)  return false;
                        if(xr - 1 == r && xc + 2 == c)  return false;
                    }
                }
            }
        }
        return true;
    }
    void solve(){
    
        int dr[] = {1,0,-1,0};
        int dc[] = {0,1,0,-1};
        for(int i = 0;i < 4;i++){
            int tr = rr+dr[i];
            int tc = cc+dc[i];
            if(tr > 0 && tr < 4 && tc > 3 && tc < 7){
                if(Map[tr][tc]){
                   qizi[Map[tr][tc]].flag = false;
                }
                if(survival(tr,tc)){
                    puts("NO");
                    return;
                }
                if(Map[tr][tc]){
                   qizi[Map[tr][tc]].flag = true;
                }
            }
        }
        puts("YES");
        return;
    }
    int main()
    {
        while(Read()){
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    js-js系列-数据类型-概念
    js-基础总结3
    js-基础总结2
    js-基础总结1
    js-面试题
    webpack-模块化
    js-对象常用方法
    js-事件冒泡-事件捕获-事件委托
    js-call aplly bind2
    aioxs实现token无感刷新
  • 原文地址:https://www.cnblogs.com/long98/p/10352149.html
Copyright © 2020-2023  润新知