RE求助!
查看原帖
RE求助!
923403
FallingFYC_楼主2023/11/4 10:06
#include <bits/stdc++.h>
//#include <windows.h>
using namespace std;
//王(captain):1   士(guard):2   象(elephant):3   马(horse):4   车(car):5   鸭(duck):6    兵(soldier):7
const int sx[] = {1 , 0 , -1 , 0 , 1 , 1 , -1 , -1};
const int sy[] = {0 , 1 , 0 , -1 , 1 , -1 , 1 , -1};
map<int , string> name;
struct Node
{
    int type , cm; //0:red 1:blue
}mp[10][10];
int q , xs , ys , xt , yt;
bool isEnd;

void init()
{
    for (int i = 0 ; i < 10 ; i++)
        for (int j = 0 ; j < 9 ; j++) mp[i][j] = {-1 , 0};
    mp[0][0] = {1 , 5}; mp[0][1] = {1 , 4}; mp[0][2] = {1 , 3}; mp[0][3] = {1 , 2}; mp[0][4] = {1 , 1}; mp[0][5] = {1 , 2}; mp[0][6] = {1 , 3}; mp[0][7] = {1 , 4}; mp[0][8] = {1 , 5};
    mp[2][0] = mp[2][8] = {1 , 6};
    mp[3][0] = mp[3][2] = mp[3][4] = mp[3][6] = mp[3][8] = {1 , 7};
    mp[6][0] = mp[6][2] = mp[6][4] = mp[6][6] = mp[6][8] = {0 , 7};
    mp[7][0] = mp[7][8] = {0 , 6};
    mp[9][0] = {0 , 5}; mp[9][1] = {0 , 4}; mp[9][2] = {0 , 3}; mp[9][3] = {0 , 2}; mp[9][4] = {0 , 1}; mp[9][5] = {0 , 2}; mp[9][6] = {0 , 3}; mp[9][7] = {0 , 4}; mp[9][8] = {0 , 5};
    name[0] = '.'; name[1] = "captain"; name[2] = "guard"; name[3] = "elephant"; name[4] = "horse"; name[5] = "car"; name[6] = "duck"; name[7] = "soldier";
	return;
}

bool check(int x , int y) {return x >= 0 && x < 10 && y >= 0 && y < 9;}

vector<pair<int , int>> move1(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 0 ; i < 4 ; i++)
    {
        int tx = x + sx[i] , ty = y + sy[i];
        if (check(tx , ty) && mp[x][y].type != mp[tx][ty].type) to.push_back(make_pair(tx , ty));
    }
    return to;
}

vector<pair<int , int>> move2(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 4 ; i < 8 ; i++)
    {
        int tx = x + sx[i] , ty = y + sy[i];
        if (check(tx , ty) && mp[x][y].type != mp[tx][ty].type) to.push_back(make_pair(tx , ty));
    }
    return to;
}

vector<pair<int , int>> move3(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 4 ; i < 8 ; i++)
    {
        int cx = x + sx[i] , cy = y + sy[i] , tx = x + 2 * sx[i] , ty = y + 2 * sy[i];
        if (check(cx , cy) && check(tx , ty) && !mp[cx][cy].cm && mp[x][y].type != mp[tx][ty].type)
            to.push_back(make_pair(tx , ty));
    }
    return to;
}

vector<pair<int , int>> move4(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 4 ; i < 8 ; i++)
    {
        int cx1 = x + sx[i] , cy1 = y , tx1 = x + 2 * sx[i] , ty1 = y + sy[i];
        if (check(cx1 , cy1) && check(tx1 , ty1) && !mp[cx1][cy1].cm && mp[x][y].type != mp[tx1][ty1].type)
            to.push_back(make_pair(tx1 , ty1));
        int cx2 = x , cy2 = y + sy[i] , tx2 = x + sx[i] , ty2 = y + 2 * sy[i];
        if (check(cx2 , cy2) && check(tx2 , ty2) && !mp[cx2][cy2].cm && mp[x][y].type != mp[tx2][ty2].type)
            to.push_back(make_pair(tx2 , ty2));
    }
    return to;
}

vector<pair<int , int>> move5(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 0 ; i < 4 ; i++)
    {
        int tx = x + sx[i] , ty = y + sy[i];
        while (!mp[tx][ty].cm) {to.push_back(make_pair(tx , ty)); tx += sx[i]; ty += sy[i];}
    }
    return to;
}

vector<pair<int , int>> move6(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 4 ; i < 8 ; i++)
    {
        int cx1 = x + 2 * sx[i] , cy1 = y + sy[i] , cx2 = x + sx[i] , cy2 = y , tx1 = x + 3 * sx[i] , ty1 = y + 2 * sy[i];
        if (check(cx1 , cy1) && check(cx2 , cy2) && check(tx1 , ty1) && !mp[cx1][cy1].cm && !mp[cx2][cy2].cm && mp[x][y].type != mp[tx1][ty1].type)
            to.push_back(make_pair(tx1 , ty1));
        int cx3 = x + sx[i] , cy3 = y + 2 * sy[i] , cx4 = x , cy4 = y + sy[i] , tx2 = x + 2 * sx[i] , ty2 = y + 3 * sy[i];
        if (check(cx3 , cy3) && check(cx4 , cy4) && check(tx2 , ty2) && !mp[cx3][cy3].cm && !mp[cx4][cy4].cm && mp[x][y].type != mp[tx2][ty2].type)
            to.push_back(make_pair(tx2 , ty2));
    }
    return to;
}

vector<pair<int , int>> move7(int x , int y)
{
    vector<pair<int , int>> to;
    for (int i = 0 ; i < 8 ; i++)
    {
        int tx = x + sx[i] , ty = y + sy[i];
        if (check(tx , ty) && mp[x][y].type != mp[tx][ty].type) to.push_back(make_pair(tx , ty));
    }
    return to;
}

vector<pair<int , int>> move(int x , int y)
{
    switch (mp[x][y].cm)
    {
        case 1: return move1(x , y);
        case 2: return move2(x , y);
        case 3: return move3(x , y);
        case 4: return move4(x , y);
        case 5: return move5(x , y);
        case 6: return move6(x , y);
        case 7: return move7(x , y);
        default: return {};
    }
}

bool checkCheck()
{
    for (int i = 0 ; i < 10 ; i++)
        for (int j = 0 ; j < 9 ; j++)
        {
            vector<pair<int , int>> np = move(i , j);
            for (auto k : np)
				if (mp[k.first][k.second].cm == 1 && mp[i][j].type != mp[k.first][k.second].type) return true;
        }
    return false;
}

int main()
{
    init();

    cin >> q;
    int now = 0;
    for (int i = 1 ; i <= q ; i++)
    {
        //cout << now << ' ' << i<<'\n' << "input: ";
        cin >> xs >> ys >> xt >> yt;
        xs = 9 - xs; xt = 9 - xt;
        //cout << xs << ' ' << ys << " | " << xt << ' ' << yt << "   ";
        //cout << name[mp[xs][ys].cm] << ' ' << name[mp[xt][yt].cm] << '\n';
        if (mp[xs][ys].type != now || mp[xt][yt].type == now) {cout << "Invalid command\n"; continue;}
        else if (isEnd) {cout << "Invalid command\n"; continue;}
        else
        {
            bool flag = false;
            vector<pair<int , int>> np = move(xs , ys);
            for (auto i : np)
                if (i == make_pair(xt , yt)) {flag = true; break;}
            if (!flag) {cout << "Invalid command\n"; continue;}
        }
        Node mo = mp[xt][yt];
        cout << (now == 0 ? "red" : "blue") << ' ' << name[mp[xs][ys].cm] << ';';
        if (!mo.cm) cout << "NA";
        else cout << (mo.type == 0 ? "red" : "blue") << ' ' << name[mo.cm];
        putchar(';');
        mp[xt][yt] = mp[xs][ys]; mp[xs][ys] = {-1 , 0};
        cout << (checkCheck() ? "yes" : "no") << ';';
        if (mo.cm == 1) {cout << "yes"; isEnd = true;}
        else cout << "no";
        putchar(';');
        putchar('\n');
        now = !now;
        
        /*
        for (int i = 0 ; i < 10 ; i++)
        {
        	for (int j = 0 ; j < 9 ; j++)
        	{
        		if (mp[i][j].type == 0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0x04);
        		else if (mp[i][j].type == 1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0x01);
        		else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0x07);
				cout << (name[mp[i][j].cm] == "captain" ? 'w': name[mp[i][j].cm][0]);
			}
			puts("");
		}
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0x07);
		*/
    }
    return 0;
}
2023/11/4 10:06
加载中...