去哪儿2017校园招聘 开发工程师(第二批次)- 题解

时间:2021-09-27 03:46:49

题目链接:点这里.

这套题很有问题,数据有问题,第一题只有 83% 的数据是对的,第三题只有 14% 的数据是对的,只能说出题人有点随意,第三题居然能出现 n=1 的这种情况,其实第三题应该保证所有单词的长度小于等于 n ,不然这题是没法做的.

还是写下解题报告,我们忽略数据的问题,给出正确的解法.

第一题:AutoPilot02

题目:

给你一个矩阵,让你打印 (0,0) (n1,n1) 的最短路径,最普通的 bfs ,第二题就是在第一题的基础上改了下,输出最短路径的长度就好了,没什么技巧,我都是用 STL 写的,所以代码比较简洁.

代码:

#include <bits/stdc++.h>

using namespace std;

const int dirx[] = {0, 1, 0, -1};
const int diry[] = {1, 0, -1, 0};

struct Node {
    int id;
    int pre;
    Node() {}
    Node(int id, int pre = -1) : id(id), pre(pre) {}
};

bool judge(int x, int y, int n)
{
    return x >= 0 && x < n && y >= 0 && y < n;
}

int main()
{
    vector<int> mp;
    int x, n;
    while (cin >> x) {
        mp.push_back(x);
    }
    n = (int)sqrt(1.0 * mp.size());
    deque<Node> que;
    vector<bool> used(mp.size(), false);
    que.emplace_back(0);
    used[0] = true;
    decltype(que.size()) head = 0;

    int src = -2;
    while (!que.empty()) {
        if (que.cbegin() + head == que.cend())
            break;
        Node now = que[head++];
        if (now.id / n == n - 1 && now.id % n == n - 1) {
            src = head - 1;
            break;
        }
        for (int i = 0; i < 4; i++)
            if (judge(now.id / n + dirx[i], now.id % n + diry[i], n) &&
                mp[(now.id / n + dirx[i]) * n + now.id % n + diry[i]] == 0 &&
                !used[(now.id / n + dirx[i]) * n + now.id % n + diry[i]])
                que.emplace_back((now.id / n + dirx[i]) * n + now.id % n + diry[i], head - 1),
                used[(now.id / n + dirx[i]) * n + now.id % n + diry[i]] = true;
    }
    if (src == -2) {
        cout << "nopath" << endl;
        return 0;
    }
    stack<int> ans;
    while (que[src].pre != -1)
        ans.push(src), src = que[src].pre;
    ans.push(0);
    while (!ans.empty())
        cout << que[ans.top()].id / n << "," << que[ans.top()].id % n << endl, ans.pop();
    return 0;
}

第二题:AutoPilot01

题目:

看第一题…..

解析:

看第一题…..

代码:

#include <bits/stdc++.h>

using namespace std;

const int dirx[] = {0, 1, 0, -1};
const int diry[] = {1, 0, -1, 0};

struct Node {
    int id;
    int step;
    Node() {}
    Node(int id, int step = 0) : id(id), step(step) {}
};

bool judge(int x, int y, int n)
{
    return x >= 0 && x < n && y >= 0 && y < n;
}

int main()
{
    vector<int> mp;
    int x, n;
    while (cin >> x) {
        mp.push_back(x);
    }
    n = (int)sqrt(1.0 * mp.size());
    deque<Node> que;
    vector<bool> used(mp.size(), false);
    que.emplace_back(0);
    used[0] = true;
    decltype(que.size()) head = 0;

    int ans = -1;
    while (!que.empty()) {
        if (que.cbegin() + head == que.cend())
            break;
        Node now = que[head++];
        if (now.id / n == n - 1 && now.id % n == n - 1) {
            ans = now.step;
            break;
        }
        for (int i = 0; i < 4; i++)
            if (judge(now.id / n + dirx[i], now.id % n + diry[i], n) &&
                mp[(now.id / n + dirx[i]) * n + now.id % n + diry[i]] == 0 &&
                !used[(now.id / n + dirx[i]) * n + now.id % n + diry[i]])
                que.emplace_back((now.id / n + dirx[i]) * n + now.id % n + diry[i], now.step + 1),
                used[(now.id / n + dirx[i]) * n + now.id % n + diry[i]] = true;
    }
    cout << ans << endl;
    return 0;
}

第三题:Text Justification

题目:

模拟两端对齐,每行最多 L 个字符,除了单词,剩下的就是空格,空格要均匀,多余的空格往左边加.

解析:

直接模拟,巨水.

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    vector<string> words;
    string str;
    int maxSize = 0;
    while (cin >> str) {
        words.push_back(str);
        maxSize = max(maxSize, (int)str.size());
    }
    int n;
    stringstream sin;
    sin << words[words.size() - 1];
    sin >> n;
    words.pop_back();
    if (maxSize > n) {
        cout << words[0];
        for (decltype(words.size()) i = 1; i < words.size(); i++)
            cout << " " << words[i];
        cout << endl;
        return 0;
    }

    int sumNum = 0, sumLen = 0;
    vector<string> ans;
    for (decltype(words.size()) i = 0; i < words.size(); i++) {
        if (sumLen + words[i].size() + sumNum <= n)
            sumLen += words[i].size(), sumNum++, ans.push_back(words[i]);
        else {
            if (sumNum == 1) {
                cout << ans[0];
                for (int i = 0; i < n - ans[0].size(); i++)
                    cout << " ";
                cout << endl;
                ans.clear();
                sumNum = sumLen = 0;
                --i;
                continue;
            }
            int space = (n - sumLen) / (sumNum - 1);
            int rest = (n - sumLen) % (sumNum - 1);
            cout << ans[0];
            for (decltype(ans.size()) j = 1; j < ans.size(); j++) {
                int top = space + (rest-- > 0 ? 1 : 0);
                for (int i = 0; i < top; i++)
                    cout << " ";
                cout << ans[j];
            }
            cout << endl;

            ans.clear();
            sumNum = sumLen = 0;
            --i;
        }
    }
    if (ans.size() != 0) {
        if (sumNum == 1) {
            cout << ans[0];
            for (int i = 0; i < n - ans[0].size(); i++)
                cout << " ";
            cout << endl;
            return 0;
        }
        int space = (n - sumLen) / (sumNum - 1);
        int rest = (n - sumLen) % (sumNum - 1);
        cout << ans[0];
        for (decltype(ans.size()) j = 1; j < ans.size(); j++) {
            int top = space + (rest-- > 0 ? 1 : 0);
            for (int i = 0; i < top; i++)
                cout << " ";
            cout << ans[j];
        }
        cout << endl;
    }
    return 0;
}