You are given row x col
grid
representing a map where grid[i][j] = 1
represents land and grid[i][j] = 0
represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid
is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes ", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]] Output: 4
Example 3:
Input: grid = [[1,0]] Output: 4
Constraints:
row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j]
is 0
or 1
.grid
.program main
implicit none
integer, parameter :: row = 4, col = 4
integer :: grid(row, col)
! Example 1
grid(:, :) = reshape([0, 1, 0, 0, &
1, 1, 1, 0, &
0, 1, 0, 0, &
1, 1, 0, 0], shape(grid))
print '(I0)', solve(grid)
! Example 2
grid(:, :) = reshape([1], shape(grid))
print '(I0)', solve(grid)
! Example 3
grid(:, :) = reshape([1, 0], shape(grid))
print '(I0)', solve(grid)
contains
function solve(grid) result(perimeter)
implicit none
integer, intent(in) :: grid(:, :)
integer :: perimeter
integer :: i, j
perimeter = 0
do i = 1, size(grid, 1)
do j = 1, size(grid, 2)
if (grid(i, j) == 1) then
perimeter = perimeter + 4
if (i > 1 .and. grid(i-1, j) == 1) then
perimeter = perimeter - 2
end if
if (j > 1 .and. grid(i, j-1) == 1) then
perimeter = perimeter - 2
end if
end if
end do
end do
end function solve
end program main
temp.f95:15:25: 15 | grid(:, :) = reshape([1], shape(grid)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape temp.f95:19:25: 19 | grid(:, :) = reshape([1, 0], shape(grid)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
! Declare variables
integer :: row, col, i, j, perimeter
integer, dimension(:,:), allocatable :: grid
! Read input
read(*,*) row
read(*,*) col
allocate(grid(row,col))
do i = 1, row
read(*,*) grid(i,:)
end do
! Calculate perimeter
perimeter = 0
do i = 2, row - 1
do j = 2, col - 1
if (grid(i,j) == 1) then
if (grid(i-1,j) == 0 .or. grid(i+1,j) == 0 .or. grid(i,j-1) == 0 .or. grid(i,j+1) == 0) then
perimeter = perimeter + 4
end if
end if
end do
end do
! Print output
write(*,*) perimeter
end program
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f76fc51a960 in ??? #1 0x7f76fc51b4d9 in ??? #2 0x7f76fc76f17b in ??? #3 0x7f76fc768684 in ??? #4 0x7f76fc7692aa in ??? #5 0x59a57866d28c in MAIN__ #6 0x59a57866d84f in main
def islandPerimeter(grid):
perimeter = 0
for row in range(len(grid)):
for col in range(len(grid[row])):
if grid[row][col] == 1:
perimeter += 4
if row > 0 and grid[row - 1][col] == 1:
perimeter -= 2
if col > 0 and grid[row][col - 1] == 1:
perimeter -= 2
return perimeter
The algorithm iterates through each cell in the grid. If a cell contains land, increment the perimeter count by 4 (each side of the square). If there is land in the adjacent cell to the left or above, decrement the perimeter count by 2 for each adjacent land cell as they share a common side. This way, we can calculate the total perimeter of the island without any extra data structures or recursion.
int islandPerimeter(vector<vector<int>>& grid) {
int perimeter = 0;
for (int row = 0; row < grid.size(); row++) {
for (int col = 0; col < grid[row].size(); col++) {
if (grid[row][col] == 1) {
perimeter += 4;
if (row > 0 && grid[row - 1][col] == 1) perimeter -= 2;
if (col > 0 && grid[row][col - 1] == 1) perimeter -= 2;
}
}
}
return perimeter;
}
The algorithm iterates through each cell in the grid. If a cell contains land, increment the perimeter count by 4 (each side of the square). If there is land in the adjacent cell to the left or above, decrement the perimeter count by 2 for each adjacent land cell as they share a common side. This way, we can calculate the total perimeter of the island without any extra data structures or recursion.