9.11 京东笔试 1 0

第一题

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int row, col, movee, turn, click;   // 行数 列数 移动

map<char, pair<int, int>> m_map;   // 方便查找

int abs(int a, int b)
{
    return a > b ? a - b : b - a;
}

// 给定初始位置,结束位置,计算耗时  返回耗时  引用记录位置
int timeSpend(int& armRow, int& armCol, int charRow, int charCol)
{
    int moveNum = abs(armRow, charRow) + abs(armCol, charCol);
    int turnNum;
    if (armRow != charRow && armCol != charCol)
        turnNum = 1;
    else turnNum = 0;
    int clickNum = 1;
    armRow = charRow;
    armCol = charCol;

    return moveNum * movee + turnNum * turn + clickNum * click;
}

int main()
{
    cin >> row;
    cin >> col;
    cin >> movee;
    cin >> turn;
    cin >> click;

    for (int i = 0; i != row; ++i)
    {
        for (int j = 0; j != col; ++j)
        {
            char temp;
            cin >> temp;
            m_map[temp] = pair<int, int>(i, j);
        }
    }

    string lizi;
    cin >> lizi;

    // 初始化机械臂位置
    int armRow = 0;
    int armCol = 0;

    long long SumTime = 0;
    for (int i = 0; i != lizi.size(); ++i)
    {
        auto iter = m_map.find(lizi[i]);
        SumTime += timeSpend(armRow, armCol, iter->second.first, iter->second.second);
    }

    cout << SumTime << endl;

    return 0;
}

第二题

#include <bits/stdc++.h>
using namespace std;

typedef struct Nodee {
    bool working = false;
    vector<int> need;
    vector<int> beNeeded;
} Node;

int main()
{
    int n, q;
    cin >> n >> q;
    vector<Node> node(n);

    for (int i = 0; i < n; ++i)
    {
        int c;
        cin >> c;
        for (int j = 0; j != c; ++j)
        {
            int cur;
            cin >> cur;
            node[i].need.push_back(cur - 1);
            node[cur - 1].beNeeded.push_back(i);
        }
    }

    for (int i = 0; i < q; ++i)
    {
        int x, y;
        cin >> x >> y;
        if (x == 0)
        {
            node[y - 1].working = false;
            int levelSize = 1;
            queue<int> que;
            que.push(y - 1);   // 停机的服务器序号
            while (levelSize)
            {
                for (int k = 0; k < levelSize; ++k)
                {
                    for (auto& item : node[que.front()].beNeeded)
                        if (node[item].working)
                            que.push(item);
                    node[que.front()].working = false;
                    que.pop();
                }
                levelSize = que.size();
            }
        }
        else if (x == 1)
        {
            node[y - 1].working = true;
            int levelSize = 1;
            queue<int> que;
            que.push(y - 1);
            while (levelSize)
            {
                for (int k = 0; k < levelSize; ++k)
                {
                    for (auto& item : node[que.front()].need)
                        if (!node[item].working)
                            que.push(item);
                    node[que.front()].working = true;
                    que.pop();
                }
                levelSize = que.size();
            }
        }

        int ret = 0;
        for (const auto& c : node)
            if (c.working) ++ret;

        cout << ret << endl;
    }

    system("pause");
    return 0;
}
全部评论

相关推荐

牛客279957775号:铁暗恋
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务