leetcode542

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.

Example 1: 
Input:

0 0 0
0 1 0
0 0 0
Output:
0 0 0
0 1 0
0 0 0

Example 2: 
Input:

0 0 0
0 1 0
1 1 1
Output:
0 0 0
0 1 0
1 2 1

Note:

  1. The number of elements of the given matrix will not exceed 10,000.
  2. There are at least one 0 in the given matrix.
  3. The cells are adjacent in only four directions: up, down, left and right. 
这道题我先针对0把0周边的数设为1,后再将改为1的数加入队列中操作
class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {   //找最近的位置和0的距离
    
        vector<vector<int>> empty;
	if (matrix.empty())
		return empty;
	int r = matrix.size();
	int c = matrix[0].size();
	vector<vector<int>> result(r, vector<int>(c));
	queue<int> rq;
	queue<int> cq;
	for (int i = 0; i < r; ++i) {
		for (int j = 0; j < c; ++j) {
			if (matrix[i][j] == 1) {
				result[i][j] = INT_MAX;
			}
		}
	}
	for (int i = 0; i < r; ++i) {
		for (int j = 0; j < c; ++j) {
			if (matrix[i][j] == 0) {
				set(result, i, j, rq, cq);
			}
		}
	}
	while (!rq.empty() && !cq.empty()) {
		setcore(result, rq, cq);
	}
	return result;
    }
    void set(vector<vector<int>>& result, int i, int j, queue<int>& rq, queue<int>& cq) {
	if (i > 0) {
		if (result[i - 1][j] == INT_MAX) {
			result[i - 1][j] = 1;
			rq.push(i - 1);
			cq.push(j);
		}
	}
	if (i < result.size() - 1) {
		if (result[i + 1][j] == INT_MAX) {
			result[i + 1][j] = 1;
			rq.push(i + 1);
			cq.push(j);
		}
	}
	if (j > 0) {
		if (result[i][j - 1] == INT_MAX) {
			result[i][j - 1] = 1;
			rq.push(i);
			cq.push(j - 1);
		}
	}
	if (j < result[0].size() - 1) {
		if (result[i][j + 1] == INT_MAX) {
			result[i][j + 1] = 1;
			rq.push(i);
			cq.push(j + 1);
		}
	}
}

void setcore(vector<vector<int>>& result, queue<int>& rq, queue<int>& cq) {
	int i = rq.front();
	rq.pop();
	int j = cq.front();
	cq.pop();
	if (i > 0) {
		if (result[i - 1][j] == INT_MAX) {
			rq.push(i - 1);
			cq.push(j);
		}
		if (result[i - 1][j] == INT_MAX) {
			result[i - 1][j] = result[i][j]+1;
		}
		else if (result[i - 1][j] != 0 && result[i - 1][j] != 1) {
			result[i - 1][j] = min(result[i][j] + 1, result[i - 1][j]);
		}
		
	}
	if (i < result.size() - 1) {
		if ( result[i + 1][j] == INT_MAX) {
			rq.push(i + 1);
			cq.push(j);
		}
		if (result[i + 1][j] == INT_MAX) {
			result[i + 1][j] = result[i][j]+1;
		}
		else if (result[i + 1][j] != 0 && result[i + 1][j] != 1) {
			result[i + 1][j] = min(result[i][j] + 1, result[i + 1][j]);
		}
		
	}
	if (j > 0) {
		if ( result[i][j - 1]== INT_MAX) {
			rq.push(i);
			cq.push(j - 1);
		}
		if (result[i][j - 1] == INT_MAX) {
			result[i][j - 1] = result[i][j]+1;
		}
		else if (result[i ][j-1] != 0 && result[i ][j-1] != 1) {
			result[i][j - 1] = min(result[i][j] + 1, result[i][j - 1]);
		}
		
	}
	if (j < result[0].size() - 1) {
		if (result[i][j + 1] == INT_MAX) {
			rq.push(i);
			cq.push(j + 1);
		}
		if (result[i][j + 1] == INT_MAX) {
			result[i][j + 1] = result[i][j]+1;

		}
		else if (result[i ][j+1] != 0 && result[i ][j+1] != 1) {
			result[i ][j+1] = min(result[i][j] + 1, result[i][j + 1]);
		}
		
	}
}
};

代码较混乱,后来想了化简的方法,可以不用分两种进行set,先留下坑,下次填

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值