c_cpp 1162.地图分析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 1162.地图分析相关的知识,希望对你有一定的参考价值。

class Solution {
public:
	int maxDistance(vector<vector<int>>& grid) {
		int n = grid.size();
		int dp[n + 2][n + 2];
		int count = 0;
		for (int i = 0; i < n + 2; i++) {
			for (int j = 0; j < n + 2; j++) {
				dp[i][j] = 1e6;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == 1) { 
                    dp[i + 1][j + 1] = 0; 
                    count++; 
                }
			}
		}
		if (count == 0 || count == n * n) {
            return -1;
        }
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));
			}
		}
		for (int i = n; i >= 1; i--) {
			for (int j = n; j >= 1; j--) {
				dp[i][j] = min(dp[i][j], min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
			}
		}
		int ans = dp[1][1];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				// cout << dp[i][j] << " ";
				ans = max(ans, dp[i][j]);
			}
			//  cout<<endl;
		}
		return ans;
	}
};

以上是关于c_cpp 1162.地图分析的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 1162. 地图分析

Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)

题解 P1162 填涂颜色

c_cpp 地图::计数()

c_cpp 地图::找到();

c_cpp 地图练习