跳到正文

个人记录

做题笔记(luogu)

akakal约 18 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 18 分钟

版本

  1. v1当前

2024

2.21

P1071

开学刷刷一下水题。

直接开个面向 char 的 int 的桶数组就行。

  • 题目要求得到所有的加密信息(A∼S\texttt A\sim \texttt S),并非只得到需要解密的;
  • 一个桶写的解法只能确保一个密文不代表多个原文,反之不行。
C++
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
using namespace std;
const int N = 100+5;
const int W = 255+5;

string es; // encrypt string
string os; // original string
string ts; // todo string

int code[W];
int decode[W];
string ans;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> es >> os >> ts;

	int n = es.size();
	for(int i = 0; i < n; i++){
		if(decode[es[i]] && decode[es[i]] != os[i]
		|| code[os[i]] && code[os[i]] != es[i]){
			cout << "Failed" << endl;
			return 0;
		}
		decode[es[i]] = os[i];
		code[os[i]] = es[i];
	}

	for(int i = 65; i <= 90; i++){
		if(!decode[i]){
			cout << "Failed" << endl;
			return 0;
		}
	}

	int m = ts.size();
	for(int i = 0; i < m; i++)
		ans += decode[ts[i]];

	cout << ans << endl;
	return 0;
}

7.15

P5380

核心部分有两个,一个是能不能走,一个是能不能将军。

将军暴搜就行了。

能不能走分两部分判断(见代码)。

为了缩短代码,直接获得起止方向和差距,根据归纳判断。

C++
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
using namespace std;
const int N = 1e1+5;
const string name[N] = {"","car","horse","elephant","guard","captain","duck","soldier"};

struct node{
    int x,y;
};

int a[N][N] = {
    {1,0,6,7,0,0,-7,-6,0,-1},
    {2,0,0,0,0,0,0,0,0,-2}, // 0 空 1 车
    {3,0,0,7,0,0,-7,0,0,-3}, // 2 马 3 象
    {4,0,0,0,0,0,0,0,0,-4}, // 4 士 5 王
    {5,0,0,7,0,0,-7,0,0,-5}, // 6 鸭 7 兵
    {4,0,0,0,0,0,0,0,0,-4}, // 以上指 abs
    {3,0,0,7,0,0,-7,0,0,-3}, // 正为红,负为蓝
    {2,0,0,0,0,0,0,0,0,-2},
    {1,0,6,7,0,0,-7,-6,0,-1}};
bool act = true; // 行动方 1 红 0 蓝
bool isEnd = false; // 结束否

inline bool checkLegality(int sx,int sy,int ex,int ey){
    int s = a[sx][sy],t = a[ex][ey];
    int absS = abs(s),absT = abs(t);
    if(!a[sx][sy]) return false;
    if(act && s < 0 || !act && s > 0) return false;
    if(act && t > 0 || !act && t < 0) return false;

    int directionX = 0,directionY = 0; // 方向 direction
    int distanceX = ex-sx,distanceY = ey-sy; // 距离 distance
    int absDistanceX = abs(distanceX),absDistanceY = abs(distanceY);
    if(sx < ex) directionX = 1; else if(sx > ex) directionX = -1;
    if(sy < ey) directionY = 1; else if(sy > ey) directionY = -1;

    // (无阻挡情况下)能否到达
    if(absS == 1 && (sx != ex && sy != ey)) return false;
    if(absS == 2 && !(absDistanceX == 2 && absDistanceY == 1 || absDistanceX == 1 && absDistanceY == 2)) return false;
    if(absS == 3 && !(absDistanceX == 2 && absDistanceY == 2)) return false;
    if(absS == 4 && !(absDistanceX == 1 && absDistanceY == 1)) return false;
    if(absS == 5 && !(absDistanceX == 1 && absDistanceY == 0 || absDistanceX == 0 && absDistanceY == 1)) return false;
    if(absS == 6 && !(absDistanceX == 2 && absDistanceY == 3 || absDistanceX == 3 && absDistanceY == 2)) return false;
    if(absS == 7 && !(absDistanceX <= 1 && absDistanceY <= 1)) return false;

    // 是否有阻挡
    if(absS == 1){
        int x = sx,y = sy;
        while(x != ex || y != ey){
            x += directionX;
            y += directionY;

            if(a[x][y] && !(x == ex && y == ey))
                return false;
        }
    }
    if(absS == 2 && (absDistanceX == 2 ? a[sx+directionX][sy] : a[sx][sy+directionY])) return false;
    if(absS == 3 && a[sx+directionX][sy+directionY]) return false;
    if(absS == 6 && (absDistanceX == 3 ? a[sx+directionX][sy]||a[sx+directionX*2][sy+directionY] : a[sx][sy+directionY]||a[sx+directionX][sy+directionY*2])) return false;

    return true;
}

// 找王 1 红 0 蓝
inline node findCaptain(bool b){
    int aim = (b ? 1 : -1) * 5;
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 10; j++){
            if(a[i][j] == aim)
                return (node){i,j};
        }
    }
    return (node){-1,-1};
}

inline bool checkMateRed(node blueCaptain){
    bool b = act;
    act = true;

    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 10; j++){
            if(checkLegality(i,j,blueCaptain.x,blueCaptain.y)){
                act = b;
                return true;
            }
        }
    }
    act = b;
    return false;
}

inline bool checkMateBlue(node redCaptain){
    bool b = act;
    act = false;

    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 10; j++){
            if(checkLegality(i,j,redCaptain.x,redCaptain.y)){
                act = b;
                return true;
            }
        }
    }
    act = b;
    return false;
}

inline bool checkMate(){
    node redCaptain = findCaptain(1);
    node blueCaptain = findCaptain(0);

    if(redCaptain.x != -1 && checkMateRed(blueCaptain)) return true;
    if(blueCaptain.x != -1 && checkMateBlue(redCaptain)) return true;
    return false;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

    int Q;
    cin >> Q;
    while(Q--){
        int sx,sy,ex,ey;

        cin >> sx >> sy;
        cin >> ex >> ey;

        swap(sx,sy);
        swap(ex,ey);

        if(isEnd){
            cout << "Invalid command" << endl;
            continue;
        }

        if(!checkLegality(sx,sy,ex,ey)){
            cout << "Invalid command" << endl;
            continue;
        }

        int s = a[sx][sy],t = a[ex][ey];
        cout << (s > 0 ? "red" : "blue") << " ";
        cout << name[abs(s)] << ";";

        if(t == 0) cout << "NA;";
        else{
            cout << (t > 0 ? "red" : "blue") << " ";
            cout << name[abs(t)] << ";";
        }

        a[ex][ey] = s;
        a[sx][sy] = 0;

        cout << (checkMate() ? "yes" : "no") << ";";

        if(abs(t) == 5)
            isEnd = true;
        cout << (isEnd ? "yes" : "no") << endl;

        act = !act;
    }
	return 0;
}

P2482

先考虑写基础部分,即输入和初步处理数据、判断结束与胜利否、输出结果。

然后写摸牌。

咕咕咕。晚上再写。

懒得写,咕个几天几个月吧。

C++
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
using namespace std;
const int N = 1e1+5;
const int M = 2e3+5;

struct pig{
    int number; // 编号
    int health; // 体力
    int kind; // 类别 0 主 1 忠 2 反
    bool isSurvive; // 是否存活
    bool isEquipment; // 是否装备
    int tot; // 手牌数量
    char card[M]; // 手牌
    vector<char> cardOrder; // 手牌顺序
}a[N];

bool isWin = true;
bool isEnd = false;
int behavior[N] = {0,1}; // 1 忠 2 反
bool likeAnti[N] = {0}; // 类反

int n,m;
int cardPosition;
int card[M];

/*
1 桃(P) 5 决斗(F)
2 杀(K) 6 南猪(N)
3 闪(D) 7 万箭(W)
4 弩(Z) 8 无懈(J)
*/

inline void checkEnd(){
    bool isDefeat = true;
    for(int i = 1; i <= n; i++){
        if(a[i].kind == 0 && !a[i].isSurvive){
            isWin = false;
            isEnd = true;
        }
        if(a[i].kind == 2 && a[i].isSurvive){
            isDefeat = false;
        }
    }

    if(isDefeat) isEnd = true;
}

inline int find(int x,char c){
    for(int i = 1; i <= a[x].tot; i++){
        if(a[x].card[i] == c)
            return i;
    }
    return false;
}

inline void useP(int c){
    if()
}

inline void useN(int c){
    for(int i = c+1; i <= n && !isEnd; i++){
        int res = find(i,'K');
        if(res) a[i].card[res] = '-';
        else{
            a[i].health--;

            checkEnd();
        }
    }
    for(int i = 1; i < c && !isEnd; i++){

    }
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

    cin >> n >> m;

    string kind;
    char cardKind;
    for(int i = 1; i <= n; i++){
        a[i].number = i;
        a[i].isSurvive = true;
        a[i].isEquipment = false;
        a[i].tot = 4;

        cin >> kind;
        if(kind == "MP") a[i].kind = 0;
        else if(kind == "ZP") a[i].kind = 1;
        else if(kind == "FP") a[i].kind = 2;

        for(int j = 1; j <= 4; j++){
            cin >> cardKind;
            a[i].card[j] = cardKind;
        }
    }

    for(int i = 1; i <= m; i++)
        cin >> card[i];
    card[0] = card[m];

    int p = -1;
    while(!isEnd){
        p = (p + 1) % n;
        int c = p + 1; // current

        if(!a[c].isSurvive)
            continue;

        a[c].card[++a[c].tot] = card[++cardPosition];
        if(cardPosition == m) cardPosition--;
        a[c].card[++a[c].tot] = card[++cardPosition];
        if(cardPosition == m) cardPosition--;

        /*for(int i = 1; i <= a[i].tot; i++){
            if(a[c].card[i] == 'Z'){
                a[c].card[i] = '-';
                a[c].cardOrder.push_back('Z');
                a[c].isEquipment = true;
            }
        }*/

        if(a[c].kind == 0){
             for(int i = 1; i <= a[c].tot; i++){
                 if(a[c].card[i] == 'N'){
                     useN(c);
                 }
             }
        }else if(a[c].kind == 1){

        }else if(a[c].kind == 2){

        }

        checkEnd();
    }

    cout << (isWin ? "MP" : "FP") << endl;

    for(int i = 1; i <= n; i++){
        if(!a[i].isSurvive){
            cout << "DEAD" << endl;
            continue;
        }

        int size = a[i].cardOrder.size();
        for(int j = 0; j < size; j++)
            cout << a[i].cardOrder[j] << " ";
        cout << endl;
    }
	return 0;
}
原文