xiaodu1997 2016-09-27 14:05 采纳率: 0%
浏览 991
已结题

uva-1589象棋--------AC不了

试了一些数据 都没问题
但提交的时候却是WA,不清楚是哪种情况没有考虑到
代码如下

 #include <iostream>

using namespace std;

const int G1 = 1,G = 2,R = 3,H = 4,C = 5;
int ploy[11][10] = {{0}};
int n = 0;
struct A
{
    int x;
    int y;
};
int isKO(int & x,int & y,A * b);
A a;
int main()
{
    int x = 0,y = 0,win = 0;
    cin >> n >> x >> y;
    if(n!=0&&x!=0&&y!=0)
    {
        ploy[x][y] = G1;
        a.x = x;
        a.y = y;
        A b[n];
        for(int i = 0; i < n; i++)
        {
            char type;
            cin >> type >> x >> y;
            switch (type)
            {
            case 'G':
                ploy[x][y] = G;
                b[i].x = x;
                b[i].y = y;
                break;
            case 'R':
                ploy[x][y] = R;
                b[i].x = x;
                b[i].y = y;
                break;
            case 'H':
                ploy[x][y] = H;
                b[i].x = x;
                b[i].y = y;
                break;
            case 'C':
                ploy[x][y] = C;
                b[i].x = x;
                b[i].y = y;
                break;
            }
        }
        int m[4];
        int n[4];
        for(int i = 0; i<2; i++)
        {
            m[i] = a.x;
            if(i == 0)
                n[i] = a.y-1>=4?a.y-1:0;
            if(i == 1)
                n[i] = a.y+1<=6?a.y+1:0;
        }
        for(int i = 2; i<4; i++)
        {
            n[i] = a.y;
            if(i == 2)
                m[i] = a.x-1>=1?a.x-1:0;
            if(i == 3)
                m[i] = a.x+1<=3?a.x+1:0;
        }
        if(isKO(a.x,a.y,b)&&isKO(m[0],n[0],b)&&isKO(m[1],n[1],b)&&isKO(m[2],n[2],b)&&isKO(m[3],n[3],b))
            cout << "YES\n";
        else
            cout<<"NO\n";
    }
    else
    {
        cout << endl;
    }
    return 0;
}

int isKO(int & x,int & y,A * b)
{
    ploy[a.x][a.y] = 0;
    if(x==0||y==0)
        return 1;
    int win = 0;
    for(int i = 0; i < n; i++)
    {
        if(ploy[(b[i].x)][(b[i].y)] == G && b[i].y == y)
        {
            int sum = 0;
            for(int j = x+1; j < b[i].x; j++)
            {
                if(ploy[j][y] != 0)
                {
                    sum = 1;
                    break;
                }
            }
            if(!sum)
            {
                break;
            }
        }

        if(ploy[(b[i].x)][(b[i].y)] == H)
        {
            if(b[i].x > x)
            {
                if((b[i].x-1) == a.x && (((b[i].y-2) == y || (b[i].y+2) == y)))
                {
                    if(ploy[b[i].x][(b[i].y+y)/2] != 0)
                    {}
                    else
                    {
                        win = 1;
                        break;
                    }
                }
                if((b[i].x-2) == x && (((b[i].y-1) == y) || ((b[i].y+1) == y)))
                {
                    if( ploy[(b[i].x+x)/2][b[i].y] != 0) {}
                    else
                    {
                        win = 1;
                        break;
                    }
                }
            }
            if(b[i].x < x)
            {
                if((b[i].x+1) == x && (((b[i].y-2) == y || (b[i].y+2) == y)))
                {
                    if(ploy[b[i].x][(b[i].y+y)/2] != 0)
                    {}
                    else
                    {
                        win = 1;
                        break;
                    }
                }
                if((b[i].x+2) == x && (((b[i].y-1) == y) || ((b[i].y+1) == y)))
                {
                    if( ploy[(b[i].x+x)/2][b[i].y] != 0) {}
                    else
                    {
                        win = 1;
                        break;
                    }
                }
            }
        }
        if(ploy[(b[i].x)][(b[i].y)] == R)
        {
            int total = 0;
            if(b[i].x == x&&b[i].y == y) {}
            else
            {
                if(b[i].x == x)
                {
                    int y2 = b[i].y < y? b[i].y : y;
                    int y3 = b[i].y < y? y : b[i].y;
                    for(int j = y2+1; j < y3; j++)
                    {
                        if(ploy[b[i].x][j]!=0)
                            total++;
                    }
                    if(!total)
                    {
                        win = 1;
                        break;
                    }
                }
                if(b[i].y == y)
                {
                    int x2 = b[i].x < x? b[i].x : x;
                    int x3 = b[i].x < x? x : b[i].x;
                    for(int j = x2+1; j < x3; j++)
                    {
                        if(ploy[j][b[i].y]!=0)
                            total++;
                    }
                    if(!total)
                    {
                        win = 1;
                        break;
                    }
                }
            }
        }
        if(ploy[(b[i].x)][(b[i].y)] == C)
        {
            int total = 0;
            if(b[i].x == x)
            {
                int y2 = b[i].y < y? b[i].y : y;
                int y3 = b[i].y < y? y : b[i].y;
                for(int j = y2+1; j < y3; j++)
                {
                    if(ploy[b[i].x][j]!=0)
                        total++;
                }
                if(total == 1)
                {
                    win = 1;
                    break;
                }
            }
            if(b[i].y == y)
            {
                int x2 = b[i].x < x? b[i].x : x;
                int x3 = b[i].x < x? x : b[i].x;
                for(int j = x2+1; j < x3; j++)
                {
                    if(ploy[j][b[i].y]!=0)
                        total++;
                }
                if(total == 1)
                {
                    win = 1;
                    break;
                }
            }
        }
        if(win == 1)
            break;
    }
    if(win)
        return 1;
    else
        return 0;
}

还没有去优化代码 这就是我完整的思维过程。。麻烦哪位大神帮忙看看

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型
  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。
  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决