跳到正文

个人记录

111

Super_Diu约 17 分钟
原文
文章信息与版本

文章信息

收录
2026年2月23日
更新
2026年5月9日
篇幅
约 17 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=1e5+10;
int q;
int n=9,m=10;
int fx1[]={0,0,1,0,-1};
int fy1[]={0,1,0,-1,0};
int fx2[]={0,1,1,-1,-1};
int fy2[]={0,1,-1,-1,1};
int fx3[]={0,-2,-1,1,2,2,1,-1,-2};
int fy3[]={0,1,2,2,1,-1,-2,-2,-1};
int fx4[]={0,0,1,1,1,0,-1,-1,-1};
int fy4[]={0,1,1,0,-1,-1,-1,0,1};
int fx5[]={0,-1,0,0,1,1,0,0,-1};
int fy5[]={0,0,1,-1,0,0,-1,1,0};
bool f;
bool end_;
char now_who='r',last_who='r';
string ate="NA";
char cl[10][11]={
{},
    {'0','r','0','r','r','0','0','b','b','0','b'},
    {'0','r','0','0','0','0','0','0','0','0','b'},
    {'0','r','0','0','r','0','0','b','0','0','b'},
    {'0','r','0','0','0','0','0','0','0','0','b'},
    {'0','r','0','0','r','0','0','b','0','0','b'},
    {'0','r','0','0','0','0','0','0','0','0','b'},
    {'0','r','0','0','r','0','0','b','0','0','b'},
    {'0','r','0','0','0','0','0','0','0','0','b'},
    {'0','r','0','r','r','0','0','b','b','0','b'}
};
char mp[10][11]={
{},
    {'0','c','0','d','b','0','0','b','d','0','c'},
    {'0','m','0','0','0','0','0','0','0','0','m'},
    {'0','x','0','0','b','0','0','b','0','0','x'},
    {'0','s','0','0','0','0','0','0','0','0','s'},
    {'0','w','0','0','b','0','0','b','0','0','w'},
    {'0','s','0','0','0','0','0','0','0','0','s'},
    {'0','x','0','0','b','0','0','b','0','0','x'},
    {'0','m','0','0','0','0','0','0','0','0','m'},
    {'0','c','0','d','b','0','0','b','d','0','c'}
};
inline char change_c(char c){
    if(c=='r') return 'b';
    return 'r';
}
inline string what(int tx,int ty){
    if(mp[tx][ty]=='0') return "none";
    else if(mp[tx][ty]=='w') return "captain";
    else if(mp[tx][ty]=='s') return "guard";
    else if(mp[tx][ty]=='x') return "elephant";
    else if(mp[tx][ty]=='m') return "horse";
    else if(mp[tx][ty]=='c') return "car";
    else if(mp[tx][ty]=='d') return "duck";
    else return "soldier";
}
inline bool same(int tx,int ty){
    if(cl[tx][ty]==now_who) return 1;
    return 0;
}
bool is_check(){
    if(end_) return false;
    int kx=fd('w').first,ky=fd('w').second;
    if(kx==78) return false;

}
pair<int,int> fd(char s){
    char c=change_c(now_who);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(mp[i][j]==s&&cl[i][j]==c)
                return {i,j};
    return {78,91};
}
bool can_move(int sx,int sy,int ex,int ey){
    if((!same(sx,sy))||end_||(sx==ex&&sy==ey)) return 0;
    if(cl[ex][ey]==now_who&&mp[ex][ey]!='0') return 0;
    bool flag=false;
    string wt=what(sx,sy);
    if(wt=="captain"){
        for(int i=1;i<=4;++i){
            int tx=sx+fx1[i];
            int ty=sy+fy1[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                if(mp[tx][ty]!='0'&&same(tx,ty)) continue;
                if(tx==ex&&ty==ey) return 1;
            }
        }
        return 0;
    }
    if(wt=="guard"){
        for(int i=1;i<=4;++i){
            int tx=sx+fx2[i];
            int ty=sy+fy2[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                if(mp[tx][ty]!='0'&&same(tx,ty)) continue;
                if(tx==ex&&ty==ey) return 1;
            }
        }
        return 0;
    }
    if(wt=="elephant"){
        for(int i=1;i<=4;++i){
            int tx=sx+fx2[i]*2;
            int ty=sy+fy2[i]*2;
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                int ttx=sx+fx2[i];
                int tty=sy+fy2[i];
                if(mp[ttx][tty]!='0') continue;
                if(tx==ex&&ty==ey) return 1;
            }
        }
        return 0;
    }
    if(wt=="horse"){
        for(int i=1;i<=8;++i){
            int tx=sx+fx3[i];
            int ty=sy+fy3[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                if(tx!=ex||ty!=ey) continue;
                int ttx=sx,tty=sy;
                if(abs(fx3[i])==2) ttx+=fx3[i]/2;
                else tty+=fy3[i]/2;
                if(mp[ttx][tty]=='0') return 1;
            }
        }
        return 0;
    }
    if(wt=="car"){
        if(sx==ex){
            int min_y=min(sy, ey);
            int max_y=max(sy, ey);
            bool blocked=false;
            for(int y=min_y+1;y<max_y;++y){
                if(mp[sx][y]!='0'){
                    blocked=true;
                    break;
                }
            }
            if(!blocked) return 1;
        }
        if(sy==ey){
            int min_x=min(sx, ex);
            int max_x=max(sx, ex);
            bool blocked=false;
            for(int x=min_x+1;x<max_x;++x){
                if(mp[x][sy]!='0'){
                    blocked=true;
                    break;
                }
            }
            if(!blocked) return 1;
        }
        return 0;
    }
    if(wt=="duck"){
         int dir1[8][6]={
            {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,2,1,1,0},
            {3,-2,2,-1,1,0},
            {-3,2,-2,1,-1,0},
            {-3,-2,-2,-1,-1,0}
        };
        for(int i=0;i<8;++i){
            int tx=sx+dir1[i][0];
            int ty=sy+dir1[i][1];
            if(tx==ex&&ty==ey&&tx>=1&&tx<=n&&ty>=1&&ty<=m){
                int mx1=sx+dir1[i][2],my1=sy+dir1[i][3];
                int mx2=sx+dir1[i][4],my2=sy+dir1[i][5];
                if(mp[mx1][my1]=='0'&&mp[mx2][my2]=='0'){
                    return 1;
                }
            }
        }
        int dir2[8][6]={
            {2,3,1,2,0,1},
            {2,-3,1,-2,0,-1},
            {-2,3,-1,2,0,1},
            {-2,-3,-1,-2,0,-1},
            {2,3,1,2,0,1},
            {2,-3,1,-2,0,-1},
            {-2,3,-1,2,0,1},
            {-2,-3,-1,-2,0,-1}
        };
        for(int i=0;i<4;++i){
            int tx=sx+dir2[i][0];
            int ty=sy+dir2[i][1];
            if(tx==ex&&ty==ey&&tx>=1&&tx<=n&&ty>=1&&ty<=m){
                int mx1=sx+dir2[i][2],my1=sy+dir2[i][3];
                int mx2=sx+dir2[i][4],my2=sy+dir2[i][5];
                if(mp[mx1][my1]=='0'&&mp[mx2][my2]=='0'){
                    return 1;
                }
            }
        }
        return 0;
    }
    if(wt=="soldier"){
        for(int i=1;i<=8;++i){
            int tx=sx+fx4[i];
            int ty=sy+fy4[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m){
                if(mp[tx][ty]!='0'&&same(tx,ty)) continue;
                if(tx==ex&&ty==ey) return 1;
            }
        }
        return 0;
    }
    return 0;
}
inline void muve(int sx,int sy,int ex,int ey){
    ate="NA";
    bool hc=(mp[ex][ey]!='0'&&cl[ex][ey]==change_c(now_who));
    if(hc){
        string ct=what(ex, ey);
        string cc=(cl[ex][ey]=='r')?"red ":"blue ";
        ate=cc+ct;
        if(ct=="captain"){
            end_= 1;
        }
    }
    mp[ex][ey]=mp[sx][sy];
    mp[sx][sy]='0';
    cl[ex][ey]=cl[sx][sy];
    cl[sx][sy]='0';
    return;
}
inline string color(int tx,int ty){
    string ans="";
    if(cl[tx][ty]=='r') ans="red ";
    else ans="blue ";
    ans+=what(tx,ty);
    return ans;
}
string jj() {
    if(end_) return "no";
    int king_x=fd('w').first,king_y=fd('w').second;
    if(king_x==-1||king_y==-1) return "no";
    char temp_cl[10][11],temp_mp[10][11];
    memcpy(temp_cl,cl,sizeof cl);
    memcpy(temp_mp,mp,sizeof mp);
    bool temp_end=end_;
    char temp_who=now_who;
    bool is_check=false;
    now_who=change_c(now_who);
    char enemy=change_c(now_who);
    for(int i=1;i<=n&&!is_check;++i){
        for(int j=1;j<=m&&!is_check;++j){
            if(cl[i][j]==enemy&&mp[i][j]!='0'){
                now_who=enemy;
                if(can_move(i,j,king_x,king_y)){
                    is_check=true;
                }
                now_who=temp_who;
            }
        }
    }
    memcpy(cl,temp_cl,sizeof cl);
    memcpy(mp,temp_mp,sizeof mp);
    end_=temp_end;
    return (is_check?"yes":"no");
}
inline string _end(){
    if(end_) return "yes";
    return "no";
}
int main(){
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    cin>>q;
    while(q--){
        int sx,sy,ex,ey;
        cin>>sx>>sy>>ex>>ey;
        ++sx;++sy;++ex;++ey;
        swap(sx,sy);swap(ex,ey);
        if((!can_move(sx,sy,ex,ey))||end_){
            cout<<"Invalid command\n";
            continue;
        }
        muve(sx,sy,ex,ey);
        cout<<color(ex,ey)<<';';
        cout<<ate<<';';
        cout<<jj()<<';';
        cout<<_end()<<'\n';
        last_who=now_who;
        now_who=change_c(now_who);
        f=1;
    }
    return 0;
}
原文