荣耀笔试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;
	}
};

#荣耀笔试#
全部评论
投的北京地区吗
点赞 回复 分享
发布于 2022-10-02 00:06 北京
没有验证,有无大佬帮看一下这个回溯逻辑对吗
点赞 回复 分享
发布于 2022-09-29 21:07 浙江

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务