8月31 华为笔试 100 100 16

#华为笔试#8月31
第一题 用的双指针移位 ,100%通过
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include<bits/stdc++.h>
using namespace std;
void string_to_lower (string &s) {
    int length = s.length();
    for (int i = 0; i < length; ++i) {
//        if (s[i] >= 'A') {
//            s[i] = 'A' + s[i]  -  'A';
//        }
        s[i] = tolower(s[i]);
    }
}
string string_transto_lower (const string &s) {
    int length = s.length();
    string res = s;
    for (int i = 0; i < length; ++i) {
//        if (s[i] >= 'A') {
//            res[i] = 'A' + s[i]  -  'A';
//        }
        res[i] = tolower(s[i]);
    }
    return res;
}
bool isalpha (char c) {
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        return true;
    }
    return false;
}
int main()
{
    string str;
    getline(cin, str);
    unordered_map<string, int>mp;
    string word;
    int index = 0;
    while (cin>>word) {
        string_to_lower(word);
        mp[word] = index;
        cout<<word<<endl;
        ++index;
    }
    // 现在是如何分割出单词,并且保留原有的 符号和空格
    // 双指针来遍历寻找!!
    int length = str.length();
    int left = 0, right = 0;
    string res = "";
    while (right < length) {
        while (str[left] == ',' || str[left] == ' ' || str[left] == '.' ) {
            res += str[left];
            ++left;
        }
        if (str[left] == '"') {
            int tmp_left = str.find('"', left + 1) + 1;
            res += str.substr(left, tmp_left - left);
            left = tmp_left;
            continue; // 跳过两个引号之间的内容
        }
        right = left;
        while (right < length && isalpha(str[right])) {
            right++;
        }
        string tmp = str.substr(left, right - left);
        string tmp_lower = string_transto_lower(tmp);
        cout<<tmp_lower<<endl;
        if (mp.find(tmp_lower) != mp.end()) {
            res +=  to_string(mp[tmp_lower]);
        } else {
            res += tmp;
        }
        left = right;
    }
    cout<<res<<endl;
    // please define the C++ input here. For example: int a,b; cin>>a>>b;;
    // please finish the function body here.
    // please define the C++ output here. For example:cout<<____<<endl;

    return 0;
}
第二题 我用的dfs减枝 这个好像叫A* 搜索? 100%通过

// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include<bits/stdc++.h>
using namespace std;
// 直接dfs 然后 加减枝
// 走到陷进上 是 额外花费 三个吗? 还是就是三个?? 先直接按三个来算
int minCost = ( 25 + 25 ) * 3;
int f[] = {0, 1, 0, -1, 0};
int n, m;
int finalx, finaly;

void dfs (vector<vector<int>> &arr, int x, int y, int cost, vector<vector<bool>>&vis) {
    //cout<<x<<" "<<y<<" "<<arr[x][y]<<endl;
    if (arr[x][y] == 3) {
        if (cost < minCost) {
            minCost = cost;
        }
        return;
    }
    if (cost >= minCost) {
        return;
    }
    int tmpMinCost = cost + abs(finalx - x) + abs(finaly - y);
    if (tmpMinCost >= minCost) {
        return;
    }
    for (int i = 0; i < 4; ++i) {
        int nx = x + f[i];
        int ny = y + f[i + 1];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m || vis[nx][ny] || arr[nx][ny] == 1 || arr[nx][ny] == 2) {
            continue;
        }
        if (arr[nx][ny] == 0 || arr[nx][ny] == 3) {
            cost++;
            vis[nx][ny] = true;
            dfs(arr, nx, ny, cost, vis);
            cost--;
            vis[nx][ny] = false;
        } else if (arr[nx][ny] == 1) {
            continue;
        } else if (arr[nx][ny] == 4) {
            cost += 3;
            vis[nx][ny] = true;
            dfs(arr, nx, ny, cost, vis);
            cost -=3;
            vis[nx][ny] = false;
        } else if (arr[nx][ny] == 6) { // 炸弹!
           // cout<<" ***"<<endl;
            cost++;
            vis[nx][ny] = true;
            // 炸毁城墙
            vector<vector<bool>>mask(n, vector<bool>(m, false));
            for (int k = 0; k < 4; ++k) {
                int nnx = nx + f[k];
                int nny = ny + f[k + 1];
                if (nnx < 0 || nny < 0 || nnx >= n || nny >= m || vis[nnx][nny] || arr[nnx][nny] != 1) {
                    continue;
                }
                arr[nnx][nny] = 0; // 炸掉强;
                mask[nnx][nny] = true;
            }
            dfs(arr, nx, ny, cost, vis);
            // 恢复城墙
            for (int t1 = 0; t1 < n; ++t1) {
                for (int t2 = 0; t2 < m; ++t2) {
                    if (mask[t1][t2]) {
                        arr[t1][t2] = 1; // 恢复城墙;
                    }
                }
            }
            cost--;
            vis[nx][ny] = false;
        }
    }
}
int main()
{

    cin>>n>>m;
    vector<vector<int>> arr(n, vector<int>(m, 0));
    vector<vector<bool>> vis(n, vector<bool>(m, false));
//    vector<bool>&vis
    int startx = 0, starty = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin>>arr[i][j];
            if (arr[i][j] == 2) {
                startx = i;
                starty = j;
            }
            if (arr[i][j] == 3) {
                finalx = i;
                finaly = j;
            }
        }
    }
   // cout<<startx<<" "<<starty<<endl;
    dfs(arr, startx, starty, 0, vis);
    cout<<minCost<<endl;
    return 0;
}
由于前面做的比较慢,第三题没时间写了,随便骗了16%

#华为笔试#
全部评论
我约的14号的机考,突然压力好大,有人给我看过这一期的题,1 3 我还能做,手写图论是什么怪物
1 回复 分享
发布于 2022-09-10 06:42 山东
第二题只过了95%,看了你的代码才发现我没有把炸了的墙恢复。
点赞 回复 分享
发布于 2022-09-01 11:29 上海
怎么能骗到16啊
点赞 回复 分享
发布于 2022-09-04 21:22 浙江
感谢楼主分享
点赞 回复 分享
发布于 2022-09-06 16:46 陕西
开始面试了吗
点赞 回复 分享
发布于 2022-09-13 13:17 北京
请教一下大佬,为啥一开始定义minCost = ( 25 + 25 ) * 3;是这个值呀?
点赞 回复 分享
发布于 2022-09-15 10:31 湖南

相关推荐

2024-12-23 10:55
已编辑
大连理工大学 Java
牛客930504082号:华子综测不好好填会挂的,而且填的时候要偏向牛马选项
点赞 评论 收藏
分享
评论
30
86
分享

创作者周榜

更多
牛客网
牛客企业服务