秋招日记(二十) 大疆 C++ 笔试
笔试题型
- 单选题*5
- 多选题*5
- 填空题*3
- 简答题*3
- 编程题*1
编程题
- 题目:给定一个图像矩阵,以及一个点的坐标和一个阈值,求与点相连的颜色值小于阈值的点的数量
- 难度:简单BFS
- 完成度:90%,有可能是输入输出的问题,题目提示了图像数据是用uint8_t保存的,我用的int
其他题目范围
- 数据结构与算法
- C++基础语法
丑陋代码
#include <iostream> #include <vector> #include <queue> using namespace std; vector<vector<int>> img = {{37,37,39,41,13,205}, {37,41,41,203,39,243}, {37,41,40,131,40,41}, {91,41,39,198,41,9}, {189,41,39,40,40,38}, {37,124,38,167,41,41}}; int calu(int a, int b) { if (a > b) { return a - b; } else { return b - a; } } int main() { int n = 6, m = 6, x = 2, y = 3, t = 3; if (t == 0) { cout << 1 << endl; return 0; } if (t == 255) { cout << n * m << endl; return 0; } vector<vector<int>> isVisit(n, vector<int>(m, 0)); int array[4][2] = { {1,0},{-1,0},{0,1},{0,-1} }; int em = img[y][x]; queue<pair<int, int>> q; q.push({ y,x }); isVisit[y][x] = 1; int res = 0; while (!q.empty()) { pair<int, int> temp = q.front(); q.pop(); res++; for (int i = 0; i < 4; i++) { pair<int, int> point{ temp.first + array[i][0], temp.second + array[i][1] }; if (point.first < 0 || point.first >= n || point.second < 0 || point.second >= m || isVisit[point.first][point.second]) { continue; } if (calu(img[point.first][point.second], em) < t) { q.push(point); isVisit[point.first][point.second] = 1; std::cout << point.first << point.second << endl; } } } std::cout << res << endl; }