1765. 최고봉 지도
난이도:중
주제: 배열, 너비 우선 검색, 행렬
land와 water 셀
의 지도를 나타내는 m x n 크기의 정수 행렬 isWater가 제공됩니다.
- isWater[i][j] == 0이면 셀 (i, j)는 육상 셀입니다.
- isWater[i][j] == 1이면 셀 (i, j)는 물 셀입니다.
다음 규칙에 따라 각 셀에 높이를 할당해야 합니다.
- 각 셀의 높이는 음수가 아니어야 합니다.
- 셀이 물 셀인 경우 높이는 0이어야 합니다.
- 인접한 두 셀의 절대 높이 차이는 최대여야 합니다. 1. 전자가 후자의 바로 북쪽, 동쪽, 남쪽 또는 서쪽에 있는 경우 해당 셀은 다른 셀과 인접해 있습니다(예: 옆구리가 닿아있습니다).
행렬의 최대 높이가 최대화되도록 높이 할당을 찾습니다.
높이[i][j]가 셀(i, j)의 높이인 m x n 크기의 정수 행렬 높이를 반환합니다. 솔루션이 여러 개 있는 경우 아무개를 반환하세요.
예 1:
-
입력: isWater = [[0,1],[0,0]]
-
출력: [[1,0],[2,1]]
-
설명: 이미지는 각 셀에 할당된 높이를 보여줍니다.
- 파란색 셀이 물 셀, 녹색 셀이 육지 셀입니다.
예 2:
-
입력: isWater = [[0,0,1],[1,0,0],[0,0,0]]
-
출력: [[1,1,0],[0,1,1],[1,2,2]]
-
설명: 높이는 2로 모든 과제의 가능한 최대 높이입니다.
- 규칙을 준수하면서 최대 높이가 2인 높이 할당도 허용됩니다.
예 3:
-
입력: isWater = [[1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,1,0, 0,0,0,0,0,0,1,0,0,1,0,1,1,0,0,1,0 ,1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0 ,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0, 0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,1,1,1,0,0,1, 0,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,0 ,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0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,1,0,1,2, 1,1,0,1,1,1,1 ,0,1,1,2,1,0,1,1,2,1,2,2,1,1,0,1,0,1,0,1,1,2,1,0,1 ,2,1,...]]
제약조건:
- m == isWater.length
- n == isWater[i].length
- 1
-
isWater[i][j]는 0 또는 1입니다.
- 물탱크가 하나개 이상 있습니다.
힌트:
- 각 물 셀을 0으로 설정합니다. 각 셀의 높이는 가장 가까운 물 셀에 의해 제한됩니다.
- 모든 물 셀을 소스로 하여 다중 소스 BFS를 수행합니다.
참고: 이 질문은 542.01 Matrix
와 동일합니다.
해결책:
BFS(폭 우선 검색) 접근 방식을 사용할 수 있습니다. 단계별로 접근하는 방법은 다음과 같습니다.
문제 분석:
-
물 셀: 1이 붙은 셀은 물 셀을 나타내며 높이는 항상 0입니다.
-
랜드 셀: 0이 붙은 셀은 랜드 셀을 나타내며, 인접한 랜드 셀의 높이 차이가 최대 1이 되도록 높이를 할당해야 합니다.
접근하다:
-
BFS 초기화:
- 모든 물 셀(값이 1인 셀)을 BFS의 시작점으로 표시하고 높이를 0으로 지정하는 것으로 시작합니다.
- 그런 다음 이웃한 육지 셀(값이 0인 셀)을 처리하여 높이를 할당합니다.
-
BFS 순회:
- 각 물 셀에서 바깥쪽으로 확장하여 인접한 각 육지 셀의 높이를 1씩 늘려 인접한 셀 간의 높이 차이가 1을 초과하지 않도록 합니다.
- 모든 셀을 방문할 때까지 이 과정을 계속합니다.
결과: 결과는 높이 값이 최대화되는 주어진 규칙을 준수하는 높이 행렬이 됩니다.
PHP에서 이 솔루션을 구현해 보겠습니다: 1765. 최고봉 지도
<?php /**
* @param Integer[][] $isWater
* @return Integer[][]
*/
function highestPeak($isWater) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
$$isWater1 = [[0,1],[0,0]];
$$isWater2 = [[0,0,1],[1,0,0],[0,0,0]];
$$isWater3 = [[1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,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,1,0,0,0,1,0,1,2,2,1,1,0,1,0,1,1,0,1,1,1,1,0,1,0,0,1,1,1,0,0,0,1,2,1,0,0,1,1,0,1,0,1,2,1,1,0,1,2,1,1,1,1,1,1,2,1,2,3,3,2,1,0,1,0,0,1,0,1,0,0,1,0,1,2,1,2,3,2,1,1,0,1,1,0,1,0,1,2,1,2,3],[1,1,0,0,1,1,0,1,1,2,1,0,1,1,1,0,1,0,1,0,1,1,0,1,2,1,1,0,1,1,1,1,0,1,1,2,1,0,1,1,2,1,2,2,1,1,0,1,0,1,0,1,1,2,1,0,1,2,1,...]]
?>
설명:
-
초기화:
- 모든 셀에 대해 높이 행렬을 -1로 초기화합니다. 물 셀은 즉시 0으로 설정됩니다.
- 물 셀이 BFS 대기열에 추가됩니다.
-
BFS:
- 우리는 각 셀을 대기열에서 제거하여 대기열을 처리하고 각 인접 셀에 대해 경계 내에 있는지, 방문하지 않았는지 확인합니다.
- 유효한 랜드 셀(방문하지 않음)인 경우 현재 셀 높이보다 1만큼 높은 높이를 할당하고 추가 처리를 위해 대기열에 추가합니다.
-
결과:
- BFS가 완료된 후 높이 매트릭스에는 주어진 제약 조건을 고려하여 각 셀에 대해 가능한 가장 높은 높이가 포함됩니다.
시간 복잡도:
-
O(m * n) 여기서 m은 행 수이고 n은 열 수입니다. 이는 BFS 순회 중에 각 셀이 최대 한 번 처리되기 때문입니다.
이 솔루션은 행렬이 올바른 높이로 채워지도록 보장하며, BFS는 인접한 셀 간의 높이 차이 제약 조건을 유지하면서 각 셀의 최대 높이를 보장합니다.
연락처 링크
이 시리즈가 도움이 되었다면 GitHub에서 저장소에 별표를 표시하거나 즐겨찾는 소셜 네트워크에서 게시물을 공유해 보세요. 여러분의 지원은 저에게 큰 의미가 될 것입니다!
이렇게 더 유용한 콘텐츠를 원하시면 저를 팔로우해주세요.
위 내용은 최고봉 지도의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!