zoukankan      html  css  js  c++  java
  • hdu4121 poj4001 Xiangqi(模拟)

    模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了。高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明。

    方法选择的好坏会影响编程复杂度,这题老将最多只能往四个位置走,就枚举这四个位置,每个位置再枚举每个红子看是不是有子能吃了它。枚举时注意有可能老将这一步吃了一个红子。

    有一种情况是一开始双方老将就见面,这其实不叫红棋在将军,实际中红棋不能这么将。但是毕竟是一道编程题,出题人可能也不是太懂棋。。。所以要考虑这种情况。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cctype>
    #include<sstream>
    using namespace std;
    #define pii pair<int,int>
    #define LL long long int
    const int eps=1e-8;
    const int INF=1000000000;
    const int maxn=7+2;
    int n,X,Y;
    int maps[20][20];
    int dx[4]= {1,-1,0,0};
    int dy[4]= {0,0,1,-1};
    struct Red
    {
        char type;
        int x,y;
    } red[10];
    bool can_eat(int r,int a,int b)
    {
        int x=red[r].x;
        int y=red[r].y;
        if(x==a&&y==b) return false;//老将吃掉了这个子
        else if(red[r].type=='G')
        {
            if(y!=b) return false;//老将不碰面
            for(int i=min(a,x)+1; i<max(a,x); i++)
            {
                if(maps[i][b]) return false;//中间隔了子,老将不能碰面
            }
            return true;
        }
        else if(red[r].type=='R')
        {
            if((x!=a)&&(y!=b)) return false;
            else if(x==a)
            {
                for(int i=min(y,b)+1; i<max(y,b); i++)
                {
                    if(maps[x][i]) return false;
                }
                return true;
            }
            else if(y==b)
            {
                for(int i=min(x,a)+1; i<max(x,a); i++)
                {
                    if(maps[i][y]) return false;
                }
                return true;
            }
        }
        else if(red[r].type=='C')
        {
            if((x!=a)&&(y!=b)) return false;
            else if(x==a)
            {
                int num=0;
                for(int i=min(y,b)+1; i<max(y,b); i++)
                {
                    if(maps[x][i]) num++;
                }
                if(num==1) return true;
                return false;
            }
            else if(y==b)
            {
                int num=0;
                for(int i=min(x,a)+1; i<max(x,a); i++)
                {
                    if(maps[i][y]) num++;
                }
                if(num==1) return true;
                return false;
            }
        }
        else //red[r].type=='H'
        {
            if(x+2==a&&y+1==b&&maps[x+1][y]=='') return true;
            if(x+1==a&&y+2==b&&maps[x][y+1]=='') return true;
            if(x-2==a&&y+1==b&&maps[x-1][y]=='') return true;
            if(x-1==a&&y+2==b&&maps[x][y+1]=='') return true;
            if(x-2==a&&y-1==b&&maps[x-1][y]=='') return true;
            if(x-1==a&&y-2==b&&maps[x][y-1]=='') return true;
            if(x+2==a&&y-1==b&&maps[x+1][y]=='') return true;
            if(x+1==a&&y-2==b&&maps[x][y-1]=='') return true;
            return false;
        }
    }
    bool pan();
    int main()
    {
        //freopen("in8.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(cin>>n>>X>>Y)
        {
            if(n==0&&X==0&&Y==0) break;
            int num,tx,ty;
            memset(maps,0,sizeof(maps));
            bool ans=true;
            for(num=0; num<n; num++)
            {
                cin>>red[num].type>>red[num].x>>red[num].y;
                maps[red[num].x][red[num].y]=1;
            }
            if(pan())
            {
                puts("NO");
                continue;
            }
            for(int i=0; i<4; i++)
            {
                tx=X+dx[i];
                ty=Y+dy[i];
                if(tx>=1&&tx<=3&&ty>=4&&ty<=6)//该步移动合法
                {
                    bool capture=false;
                    for(int j=0; j<n; j++) //检查现在有没有红子能吃黑将
                    {
                        if(can_eat(j,tx,ty))
                        {
                            capture=true;
                            break;
                        }
                    }
                    if(capture==true) continue;
                    else
                    {
                        ans=false;
                        break;
                    }
                }
                else continue;
            }
            if(ans==false) printf("NO
    ");
            else printf("YES
    ");
        }
        //fclose(stdin);
        //fclose(stdout);
        return 0;
    }
    bool pan()
    {
        for(int i=0; i<n; i++)
        {
            if(red[i].type=='G')
            {
                if(Y==red[i].y)
                {
                    for(int j=min(red[i].x,X)+1; j<max(red[i].x,X); j++)
                    {
                        if(maps[j][Y])
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        return true;
    }
  • 相关阅读:
    pid系统控制算法
    pandas+numpy 对df数据中的某列进行等分分桶操作
    pandas学习
    angular6 数组遍历项进行双向绑定 丢失焦点 问题解决
    TypeScript 之构造器 constructor 方法 methods
    公开便民信息网站收集
    C++11_线程池
    百度网盘信息
    【赵强老师】MongoDB中的索引(下)
    TDSQL 4001 备份故障处理案例
  • 原文地址:https://www.cnblogs.com/zywscq/p/4104584.html
Copyright © 2011-2022 走看看