荣耀笔试9.29第三题
class solution {
public:
int flag = 1;
int res = 0;
int max1 = 0;
void backtracing(vector<vector<int>>&nums, int m, int n) {//清除完毕来到这个地方
int size = nums.size();
if (flag == 1) {
if (m == size - 1 && n == size - 1) {
flag = 2;
backtracing(nums, size - 1, size - 1);
flag == 1;
}
if (m + 1 < size && nums[m + 1][n] != -1) {
if (nums[m + 1][n] == 0) {
backtracing(nums, m + 1, n);
}
else {
res++;
nums[m + 1][n] = 0;
backtracing(nums, m + 1, n);
res--;
nums[m + 1][n] = 1;
}
}
if (n + 1 < size && nums[m][n + 1] != -1) {
if (nums[m][n + 1] == 0) {
backtracing(nums, m, n + 1);
}
else {
res++;
nums[m][n + 1] = 0;
backtracing(nums, m, n + 1);
res--;
nums[m][n + 1] = 1;
}
}
}
else {
if (m == 0 && n == 0) {
max1 = max(res, max1);
return;
}
if (m - 1 >= 0 && nums[m - 1][n] != -1) {
if (nums[m - 1][n] == 0) {
backtracing(nums, m - 1, n);
}
else {
res++;
nums[m - 1][n] = 0;
backtracing(nums, m - 1, n);
res--;
nums[m - 1][n] = 1;
}
}
if (n - 1 >= 0 && nums[m][n - 1] != -1) {
if (nums[m][n - 1] == 0) {
backtracing(nums, m, n - 1);
}
else {
res++;
nums[m][n - 1] = 0;
backtracing(nums, m, n - 1);
res--;
nums[m][n - 1] = 1;
}
}
}
}
int max_guaishou(vector<vector<int>>&nums) {
int n = nums.size();
if (nums[0][0] == 1) {
res++;
}
backtracing(nums, 0, 0);
return max1 == 0 ? -1 : max1;
}
}; #荣耀笔试#

