码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4121 poj4001 Xiangqi(模拟)

时间:2014-11-18 00:06:23      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   for   div   

模拟题考验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]==\0) return true;
        if(x+1==a&&y+2==b&&maps[x][y+1]==\0) return true;
        if(x-2==a&&y+1==b&&maps[x-1][y]==\0) return true;
        if(x-1==a&&y+2==b&&maps[x][y+1]==\0) return true;
        if(x-2==a&&y-1==b&&maps[x-1][y]==\0) return true;
        if(x-1==a&&y-2==b&&maps[x][y-1]==\0) return true;
        if(x+2==a&&y-1==b&&maps[x+1][y]==\0) return true;
        if(x+1==a&&y-2==b&&maps[x][y-1]==\0) 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\n");
        else printf("YES\n");
    }
    //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;
}

 

hdu4121 poj4001 Xiangqi(模拟)

标签:style   blog   io   color   ar   os   sp   for   div   

原文地址:http://www.cnblogs.com/zywscq/p/4104584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!