โจทย์
The grid holds 1 for land and 0 for water. Each land cell is a 1×1 square. Compute the total perimeter of all land: every side of a land cell that faces water or the grid edge counts 1.
Implement island_perimeter(grid).
Input
grid: A rectangular grid of 0s and 1s.
Output
Return the total perimeter.
เงื่อนไข
0 ≤ rows, columns ≤ 200
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
grid = [
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[1, 1, 0, 0]
]
ผลลัพธ์ที่คาดหวัง:
expected = 16
อินพุต:
std::vector<std::vector<long long>> grid = {
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0},
{1, 1, 0, 0}
};
ผลลัพธ์ที่คาดหวัง:
long long expected = 16;
อินพุต:
long[][] grid = new long[][] {
new long[] {0, 1, 0, 0},
new long[] {1, 1, 1, 0},
new long[] {0, 1, 0, 0},
new long[] {1, 1, 0, 0}
};
ผลลัพธ์ที่คาดหวัง:
long expected = 16;
คำอธิบาย
7 land cells have 28 sides; 6 shared edges remove 12: perimeter 16.
ตัวอย่างที่ 2
อินพุต:
grid = [
[1]
]
ผลลัพธ์ที่คาดหวัง:
expected = 4
อินพุต:
std::vector<std::vector<long long>> grid = {
{1}
};
ผลลัพธ์ที่คาดหวัง:
long long expected = 4;
อินพุต:
long[][] grid = new long[][] {
new long[] {1}
};
ผลลัพธ์ที่คาดหวัง:
long expected = 4;
คำอธิบาย
A single land cell has 4 sides.
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน