Luogu5380 [THUPC2019]鸭棋

https://www.luogu.com.cn/problem/P5380

模拟

纯粹的大模拟,一点贪心啥的都没有

代码蛮好调的(虽然我太菜\(WA\)了好多发)

\(Code:\)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#define pr pair<int,bool>
#define mp make_pair
#define red 0
#define blue 1
#define blank 0
#define captain 1
#define guard 2
#define elephant 3
#define horse 4
#define car 5
#define duck 6
#define soldier 7
#define space mp(blank,0)
#define wz(x,y) (m[x][y].first)
#define col(x,y) (m[x][y].second)
#define check(x,y) (0<=x && x<=9 && 0<=y && y<=8)
using namespace std;
int T,sx,sy,tx,ty;
int now=red,nx,ny,qx,qy,rx,ry,g;
bool gameover=false,ge,flag,dangerous;
string id[2]={"red","blue"},name[soldier+1];
int Captain[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int Guard[4][2]={{1,1},{-1,-1},{1,-1},{-1,1}};
int Elephant[4][2][2]=
{
    {{1,1},{2,2}},
    {{-1,-1},{-2,-2}},
    {{1,-1},{2,-2}},
    {{-1,1},{-2,2}}
};
int Horse[8][2][2]=
{
    {{1,0},{2,1}},
    {{1,0},{2,-1}},
    {{-1,0},{-2,1}},
    {{-1,0},{-2,-1}},
    {{0,1},{1,2}},
    {{0,1},{-1,2}},
    {{0,-1},{1,-2}},
    {{0,-1},{-1,-2}}
};
int Duck[8][3][2]=
{
    {{2,1},{1,0},{3,2}},
    {{2,-1},{1,0},{3,-2}},
    {{-2,1},{-1,0},{-3,2}},
    {{-2,-1},{-1,0},{-3,-2}},
    {{1,2},{0,1},{2,3}},
    {{1,-2},{0,-1},{2,-3}},
    {{-1,2},{0,1},{-2,3}},
    {{-1,-2},{0,-1},{-2,-3}}
};
int Soldier[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
pr m[10][9];
void _captain()
{
    int nx,ny,lx=0,ly=0;
    bool find=false;
    for (int i=0;i<10 && !find;i++)
        for (int j=0;j<9 && !find;j++)
            if (col(i,j)==now && wz(i,j)==captain)
            {
                lx=i,ly=j;
                find=true;
                break;
            }
    for (int sx=0;sx<10;sx++)
        for (int sy=0;sy<9;sy++)
            if (col(sx,sy)!=now)
            {
                int p=wz(sx,sy);
                if (p==captain)
                {
                    for (int i=0;i<4;i++)
                    {
                        nx=sx+Captain[i][0];
                        ny=sy+Captain[i][1];
                        if (nx==lx && ny==ly)
                        {
                            dangerous=true;
                            return;
                        }
                    }
                }
                if (p==guard)
                {
                    for (int i=0;i<4;i++)
                    {
                        nx=sx+Guard[i][0];
                        ny=sy+Guard[i][1];
                        if (nx==lx && ny==ly)
                        {
                            dangerous=true;
                            return;
                        }
                    }
                }
                if (p==elephant)
                {
                    for (int i=0;i<4;i++)
                    {
                        qx=sx+Elephant[i][0][0];
                        qy=sy+Elephant[i][0][1];
                        nx=sx+Elephant[i][1][0];
                        ny=sy+Elephant[i][1][1];
                        if (check(qx,qy) && wz(qx,qy)!=blank)
                            continue;
                        if (nx==lx && ny==ly)
                        {
                            dangerous=true;
                            return;
                        }
                    }
                }
                if (p==horse)
                {
                    for (int i=0;i<8;i++)
                    {
                        qx=sx+Horse[i][0][0];
                        qy=sy+Horse[i][0][1];
                        nx=sx+Horse[i][1][0];
                        ny=sy+Horse[i][1][1];
                        if (check(qx,qy) && wz(qx,qy)!=blank)
                            continue;
                        if (nx==lx && ny==ly)
                        {
                            dangerous=true;
                            return;
                        }
                    }
                }
                if (p==car)
                {
                    rx=sx-1,ry=sy;
                    flag=false;
                    while (rx>=0 && !flag)
                    {
                        if (rx==lx && ry==ly)
                        {
                            flag=true;
                            break;
                        }
                        if (wz(rx,ry)!=blank)
                            break;
                        rx--;
                    }
                    rx=sx+1,ry=sy;
                    while (rx<10 && !flag)
                    {
                        if (rx==lx && ry==ly)
                        {
                            flag=true;
                            break;
                        }
                        if (wz(rx,ry)!=blank)
                            break;
                        rx++;
                    }
                    rx=sx,ry=sy-1;
                    while (ry>=0 && !flag)
                    {
                        if (rx==lx && ry==ly)
                        {
                            flag=true;
                            break;
                        }
                        if (wz(rx,ry)!=blank)
                            break;
                        ry--;
                    }
                    rx=sx,ry=sy+1;
                    while (ry<9 && !flag)
                    {
                        if (rx==lx && ry==ly)
                        {
                            flag=true;
                            break;
                        }
                        if (wz(rx,ry)!=blank)
                            break;
                        ry++;
                    }
                    if (flag)
                    {
                        dangerous=true;
                        return;
                    }
                }
                if (p==duck)
                {
                    for (int i=0;i<8;i++)
                    {
                        nx=sx+Duck[i][2][0];
                        ny=sy+Duck[i][2][1];
                        if (nx!=lx || ny!=ly)
                            continue;
                        qx=sx+Duck[i][0][0];
                        qy=sy+Duck[i][0][1];
                        if (check(qx,qy) && wz(qx,qy)!=blank)
                            continue;
                        qx=sx+Duck[i][1][0];
                        qy=sy+Duck[i][1][1];
                        if (check(qx,qy) && wz(qx,qy)!=blank)
                            continue;
                        dangerous=true;
                        return;
                    }
                }
                if (p==soldier)
                {
                    for (int i=0;i<8;i++)
                    {
                        nx=sx+Soldier[i][0];
                        ny=sy+Soldier[i][1];
                        if (nx==lx && ny==ly)
                        {
                            dangerous=true;
                            return;
                        }
                    }
                }
            }
}
void answer()
{
    if (!ge)
    {
        puts("Invalid command");
        return;
    }
    cout << id[now] << " " << name[g] << ";";
    if (wz(tx,ty)==blank)
        cout << "NA;"; else
        cout << id[now^1] << " " << name[wz(tx,ty)] << ";";
    if (wz(tx,ty)==captain)
    {
        gameover=true;
        cout << "no;yes" << endl;
        return;
    }
    m[tx][ty]=m[sx][sy];
    m[sx][sy]=space;
    dangerous=false;
    _captain();
    now^=1;
    if (!dangerous)
        _captain();
    if (dangerous)
        cout << "yes;"; else
        cout << "no;";
    cout << "no" << endl;
}
int main()
{
    name[blank]="blank";
    name[captain]="captain";
    name[guard]="guard";
    name[elephant]="elephant";
    name[horse]="horse";
    name[car]="car";
    name[duck]="duck";
    name[soldier]="soldier";
    for (int i=0;i<=9;i++)
        for (int j=0;j<=8;j++)
            m[i][j]=space;
    m[0][0]=mp(car,red);
    m[2][0]=mp(duck,red);
    m[3][0]=mp(soldier,red);
    m[0][1]=mp(horse,red);
    m[0][2]=mp(elephant,red);
    m[3][2]=mp(soldier,red);
    m[0][3]=mp(guard,red);
    m[0][4]=mp(captain,red);
    m[3][4]=mp(soldier,red);
    for (int i=5;i<=9;i++)
        for (int j=0;j<=4;j++)
            m[i][j]=m[9-i][j],col(i,j)=blue;
    for (int i=0;i<=9;i++)
        for (int j=5;j<=8;j++)
            m[i][j]=m[i][8-j];
    scanf("%d",&T);
    while (T --> 0)
    {
        scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
        if (wz(sx,sy)==blank || wz(sx,sy)!=blank && col(sx,sy)!=now || gameover)
        {
            puts("Invalid command");
            continue;
        }
        if (wz(tx,ty)!=blank && col(tx,ty)==now)
        {
            puts("Invalid command");
            continue;
        }
        ge=false;
        g=wz(sx,sy);
        if (g==captain)
        {
            for (int i=0;i<4;i++)
            {
                nx=sx+Captain[i][0];
                ny=sy+Captain[i][1];
                if (nx!=tx || ny!=ty)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
        if (g==guard)
        {
            for (int i=0;i<4;i++)
            {
                nx=sx+Guard[i][0];
                ny=sy+Guard[i][1];
                if (nx!=tx || ny!=ty)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
        if (g==elephant)
        {
            for (int i=0;i<4;i++)
            {
                qx=sx+Elephant[i][0][0];
                qy=sy+Elephant[i][0][1];
                nx=sx+Elephant[i][1][0];
                ny=sy+Elephant[i][1][1];
                if (check(qx,qy) && wz(qx,qy)!=blank || nx!=tx || ny!=ty)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
        if (g==horse)
        {
            for (int i=0;i<8;i++)
            {
                qx=sx+Horse[i][0][0];
                qy=sy+Horse[i][0][1];
                nx=sx+Horse[i][1][0];
                ny=sy+Horse[i][1][1];
                if (check(qx,qy) && wz(qx,qy)!=blank || nx!=tx || ny!=ty)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
        if (g==car)
        {
            rx=sx-1,ry=sy;
            while (rx>=0 && !ge)
            {
                if (rx==tx && ry==ty)
                {
                    ge=true;
                    break;
                }
                if (wz(rx,ry)!=blank)
                    break;
                rx--;
            }
            rx=sx+1,ry=sy;
            while (rx<10 && !ge)
            {
                if (rx==tx && ry==ty)
                {
                    ge=true;
                    break;
                }
                if (wz(rx,ry)!=blank)
                    break;
                rx++;
            }
            rx=sx,ry=sy-1;
            while (ry>=0 && !ge)
            {
                if (rx==tx && ry==ty)
                {
                    ge=true;
                    break;
                }
                if (wz(rx,ry)!=blank)
                    break;
                ry--;
            }
            rx=sx,ry=sy+1;
            while (ry<9 && !ge)
            {
                if (rx==tx && ry==ty)
                {
                    ge=true;
                    break;
                }
                if (wz(rx,ry)!=blank)
                    break;
                ry++;
            }
            answer();
        }
        if (g==duck)
        {
            for (int i=0;i<8;i++)
            {
                nx=sx+Duck[i][2][0];
                ny=sy+Duck[i][2][1];
                if (nx!=tx || ny!=ty)
                    continue;
                qx=sx+Duck[i][0][0];
                qy=sy+Duck[i][0][1];
                if (check(qx,qy) && wz(qx,qy)!=blank)
                    continue;
                qx=sx+Duck[i][1][0];
                qy=sy+Duck[i][1][1];
                if (check(qx,qy) && wz(qx,qy)!=blank)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
        if (g==soldier)
        {
            for (int i=0;i<8;i++)
            {
                nx=sx+Soldier[i][0];
                ny=sy+Soldier[i][1];
                if (nx!=tx || ny!=ty)
                    continue;
                ge=true;
                break;
            }
            answer();
        }
    }
    return 0;
}
posted @ 2020-08-07 20:05  GK0328  阅读(72)  评论(0编辑  收藏  举报