首页 日常,🐶算法

试题 B: 扩散

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

const int N = 10010;
int arr[N][N];

struct n
{
    int x, y, t;
};

bool jud(int x, int y, int t)
{
    if (x >= 0 && y >= 0 && arr[x][y] == 0 && t <= 2020) return true;
    return false;
}

int nx[4] = {0, 0, 1, -1};
int ny[4] = {1, -1, 0, 0};

queue<n> q;

int main()
{
    arr[0 + 2100][0 + 2100] = arr[2020 + 2100][11 + 2100] = arr[11 + 2100][14 + 2100] = arr[2000 + 2100][2000 + 2100] = 1;
    n a;
    a.x = 0 + 2100;
    a.y = 0 + 2100;
    a.t = 0;
    q.push(a);
    a.x = 2020 + 2100;
    a.y = 11 + 2100;
    a.t = 0;
    q.push(a);
    a.x = 11 + 2100;
    a.y = 14 + 2100;
    a.t = 0;
    q.push(a);
    a.x = 2000 + 2100;
    a.y = 2000 + 2100;
    a.t = 0;
    q.push(a);
    long long res = 4;
    while (!q.empty()) {
        a = q.front();
        q.pop();
        int fx, fy, ft = a.t + 1;
        for (int j = 0; j < 4; j++) {
            fx = a.x + nx[j];
            fy = a.y + ny[j];
            if (jud(fx, fy, ft) == true) {
                n b;
                b.x = fx;
                b.y = fy;
                b.t = ft;
                q.push(b);
                arr[fx][fy] = 1;
                res++;
            }
        }
    }
    cout<<endl;
    cout<<res<<endl;
    return 0;
}

试题 G: 游园安排

#include <iostream>
#include <vector>
#include <string>
#include <cstring>

using namespace std;
const int N = 1e5 + 5;

string arr[N];
int dp[N];
int path[N];

void bl(int x)
{
    if (x == 0) return;
    bl(path[x]);
    cout<<arr[x];
}

int main()
{
    string str;
    int id = -1;
    cin>>str;
    arr[0] = "";
    for (int i = 0; i < str.size(); i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            id++;
            arr[id] = "";

        }
        arr[id] += str[i];
    }
    int nmax = -1, pos = 0;
    for (int i = 0; i <= id; i++) {
        for (int j = 0; j <i; j++) {
            if (arr[i] > arr[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
                path[i] = j;
                if (nmax < dp[i]) {
                    nmax = dp[i];
                    pos = i;
                }
            }
        }
    }
    bl(pos);
    return 0;
}



文章评论

    阿里巴巴速卖通宝典 访客FireFoxWindows
    2021-07-16 14:46   回复

    内容好多,看起来很高级的样子