8月31 华为笔试 100 100 16
#华为笔试#8月31
![](https://uploadfiles.nowcoder.com/images/20220815/318889480_1660553763490/62AF11E48344D159DA608796DA7D39E5)
![](https://uploadfiles.nowcoder.com/images/20220815/318889480_1660553763490/62AF11E48344D159DA608796DA7D39E5)
![](https://uploadfiles.nowcoder.com/images/20220815/318889480_1660553763490/62AF11E48344D159DA608796DA7D39E5)
#华为笔试#
第一题 用的双指针移位 ,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%