京东笔试第一题,36%
#include <iostream> #include <vector> using namespace std; //判断联通个数 int liantong(vector<vector<int>> nums, int i, int j, int val){ if (i >= 0 && i < 5 && j >= 0 && j < 5){ if (nums[i][j] == val){ nums[i][j] = 0; return 1 + liantong(nums, i - 1, j, val) + liantong(nums, i + 1, j, val) + liantong(nums, i, j + 1, val) + liantong(nums, i, j - 1, val); } else return 0; } else return 0; } //删除联通元素 void del_nums(vector<vector<int>> &nums, int i, int j, int val){ if (i >= 0 && i < 5 && j >= 0 && j < 5){ if (nums[i][j] == val){ nums[i][j] = 0; del_nums(nums, i - 1, j, val); del_nums(nums, i + 1, j, val); del_nums(nums, i, j + 1, val); del_nums(nums, i, j - 1, val); } else return ; } else return ; } int main(){ vector<vector<int>> board(5, vector<int>(5, 0)); for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) cin >> board[i][j]; int m = 5, n = 5; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (board[i][j] == 0) continue; if (liantong(board, i, j, board[i][j]) >= 3){ del_nums(board, i, j, board[i][j]); for (int j = 0; j < n; ++j) { int t = m - 1; for (int i = m - 1; i >= 0; --i) { if (board[i][j]) swap(board[t--][j], board[i][j]); } } } } } int result = 0; for (int i = 0; i < 5; ++i){ for (int j = 0; j < 5; ++j){ if (board[i][j] != 0) result++; } } cout << result << endl; return 0; }也不知道错哪里了
#京东##笔试题目#