Python:每日一题 389
今天的题目:给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地,0 表示水域。
网格中的格子水平和垂直方向相连(但对角线方向不相连)。
整个网格被水完全包围,但其中恰好有一个岛屿(或者说一个或多个表示陆地的格子相连组成的岛屿)。
格子是边长为 1 的正方形,网格为长方形。计算这个岛屿的周长。
说明:1. 岛屿中没有 “湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。
2. 网格的长度和高度均不超过 100 。
示例:
输入:
[
,
,
,
]
输出:16
解释:它的周长是下面图片中的 16 个黄色的边:
{:10_298:}欢迎大家一起答题!{:10_298:} 本帖最后由 TJBEST 于 2020-5-2 21:19 编辑
def fun389(grid):
if len(grid) == 1:
return 2*sum(grid)+2
result = 0#结果
has = 0#标志 表示已经进入岛屿
temp = 0#每行周长
M = len(grid)
N = len(grid)
#Line 0
if grid == 1:
temp += 4 - (grid + grid)
for j in range(1,N-1):
if grid == 1:
temp += 4 - (grid + grid + grid)
if grid == 1:
temp += 4 - (grid + grid)
if temp > 0:
has = 1
result += temp
#Line 1 ~ M-2
for i in range(1,M-1):
temp = 0
if grid == 1:
temp += 4 - (grid + grid + grid)
for j in range(1,N-1):
if grid == 1:
temp += 4 - (grid + grid + grid + grid)
if grid == 1:
temp += 4 - (grid + grid + grid)
if temp > 0:
has = 1
result += temp
else:
if has > 0:
break
else:
pass
#Line M-1 前面直接跳出无所谓 就是多算一行而行
temp = 0
if grid == 1:
temp += 4 - (grid + grid)
for j in range(1,N-1):
if grid == 1:
temp += 4 - (grid + grid + grid)
if grid == 1:
temp += 4 - (grid + grid)
result += temp
return result 占楼 本帖最后由 Twilight6 于 2020-5-2 14:55 编辑
占楼~
。。。放弃,这题我不会{:9_220:} 占 本帖最后由 xiangjianshinan 于 2020-5-2 20:09 编辑
我愚钝,再仔细研究下题目!!!
pandas 还不会用,否则就是几句代码的问题吧??
import pandas as pd
def timu389(l):
la = pd.DataFrame(l)
jishu,sa = 0,sum(la.sum(1))*4
for i in range(la.shape):
for j in range(la.shape):
if (i > 0 and la and la):
jishu += 1
if (j > 0 and la and la):
jishu += 1
return sa-jishu*2
la = [
,
,
,
]
print(timu389(la))
另:这里pandas几乎或者根本没有用到。 本帖最后由 kinkon 于 2020-5-2 23:23 编辑
def f389(grid):
if not grid or not grid:
return 0
m, n = len(grid), len(grid)
ans = [(-1,0),(0,-1),(1,0),(0,1)]
cout = 0
for i in range(m):
for j in range(n):
if grid == 1:
cout += 4
for k, v in ans:
if 0 <= i+k < m and 0 <= j+v < n:
if grid == 1:
cout -= 1
return cout 本帖最后由 悠悠2264 于 2020-5-2 18:32 编辑
运用深度优先搜索,由于需要用到递归,所以封装了两个函数。
def dfs(x,y):
global num
global book
book = 1
num = num + 4
if x+1 < len(l) and l == 1:
num = num - 1
if book == 0:
dfs(x+1,y)
if y+1 < len(l) and l == 1:
num = num - 1
if book == 0:
dfs(x,y+1)
if x-1 >= 0 and l == 1:
num = num - 1
if book == 0:
dfs(x-1,y)
if y-1 >= 0 and l == 1:
num = num - 1
if book == 0:
dfs(x,y-1)
return
def day389(list1):
global num
num = 0
global book
book = []
global l
l = list1
for i in range(len(l)):
t = []
for j in range(len(l)):
t.append(0)
book.append(t)
for i in range(len(l)):
for j in range(len(l)):
if l == 1:
dfs(i,j)
return num 本帖最后由 liuzhengyuan 于 2020-5-12 09:39 编辑
我在力扣上测了一下(通过了)
class Solution:
def islandPerimeter(self, grid: List]) -> int:
# 思路:横向纵向暴力……
# 在各个边的的结尾部分插入 0,以便之后使用暴力……
for i in range(len(grid)):
grid.append(0)
templist = []
for i in range(len(grid)):
templist.append(0)
grid.append(templist)
# 暴力
sides = 0
for i in grid:
panb = False
for j in i:
if panb != j:
sides += 1
panb = j
for i in range(len(grid)):
panb = False
for j in range(len(grid)):
if panb != grid:
sides += 1
panb = grid
return sides
def d389(list1):
# 思路:横向纵向暴力……
# 在各个边的的结尾部分插入 0,以便之后使用暴力……
for i in range(len(list1)):
list1.append(0)
templist = []
for i in range(len(list1)):
templist.append(0)
list1.append(templist)
# 暴力
sides = 0
for i in list1:
panb = False
for j in i:
if panb != j:
sides += 1
panb = j
for i in range(len(list1)):
panb = False
for j in range(len(list1)):
if panb != list1:
sides += 1
panb = list1
return sides def daily389(grid: list) -> int:
# 解题思路
# 岛的解题方法一般会用DFS(但这里好像没必要)
# 说明中特别指出没有湖(这不就是告诉我们要偷懒吗) -> 只需要求北边和西边的长度
# 南边长度等于北边,东边长度等于西边
row = len(grid)
column = len(grid)
c = 0
for i in range(row):
for j in range(column):
if grid == 1:
if i == 0 or grid == 0:
c += 1
if j == 0 or grid == 0:
c += 1
return c * 2 xiangjianshinan 发表于 2020-5-2 16:14
我愚钝,再仔细研究下题目!!!
pandas 还不会用,否则就是几句代码的问题吧??
哈哈哈,和我的思路好像 March2615 发表于 2020-5-2 20:01
哈哈哈,和我的思路好像
很遗憾哦,我感觉看别人的代码很累。看不懂思路,也不想看!!!我知道这样不对的,不利于我技术水平地提高。。。
求问,有秘诀吗?
def f(list1):
for n in range(len(list1)):
if 1 in list1:
break
m = n
length = (list1).count(1)
while 1 in list1 and n+1<len(list1):
n += 1
for each in range(len(list1)):
if list1 == 1 and list1 ==0:
length += 1
return (length+n-m+1)*2 def fun389(lst):
result=0
for i in range(len(lst)):
lst.insert(0,0)
lst.append(0)
lst.insert(0,*len(lst))
lst.append(*len(lst))
for i in range(1,len(lst)-1):
for j in range(1,len(lst)-1):
if lst:
result+=4-lst-lst-lst-lst
return result
不知道是不是这个意思,但我这个程序运行还是比较慢的 相当于把矩阵周围加一圈0,原矩阵中每个元素分别向上向左模2加
import numpy as np
def func389(matrix):
matrix = np.array(matrix)
row, col = np.shape(matrix)
circle = 0
for i in range(row):
for j in range(col):
if i == 0 and j == 0:
circle += matrix
elif i == 0 and j != 0:
circle += (matrix + matrix) % 2 + matrix
elif i != 0 and j == 0:
circle += (matrix + matrix) % 2 + matrix
else:
circle += (matrix + matrix) % 2 + (matrix + matrix) % 2
circle = circle + np.sum(matrix) + np.sum(matrix[:, col-1])
return circle def func389(grid):
count = 0
subnumber = 0
m = len(grid)
n = len(grid)
for i in range(m):
for j in range(n):
if grid == 1:
count +=1
if j+1!=n:
if grid == 1:
subnumber += 1
if i+1!=m:
if grid == 1:
subnumber += 1
return count*4 - subnumber*2 def zcd(x,y,list):
zc = 0 #初始化周长
#判断周边是否与陆地接触
#左边
if y == 0:
zc += 1
else:
if list == 0:
zc += 1
#上边
if x == 0:
zc += 1
else:
if list == 0:
zc += 1
#右边
if y == 3:
zc += 1
else:
if list == 0:
zc += 1
#下边
if x == 3:
zc += 1
else:
if list == 0:
zc += 1
return zc
list = [
,
,
,
]
s = 0#初始化总周长
#计算周长
for i in range(4):
for j in range(4):
if list == 1:
s += zcd(i,j,list)
print(s)
def zcd(x,y,list):
zc = 0 #初始化周长
#判断周边是否与陆地接触
#左边
if y == 0:
zc += 1
else:
if list == 0:
zc += 1
#上边
if x == 0:
zc += 1
else:
if list == 0:
zc += 1
#右边
if y == 3:
zc += 1
else:
if list == 0:
zc += 1
#下边
if x == 3:
zc += 1
else:
if list == 0:
zc += 1
return zc
list = [
,
,
,
]
s = 0#初始化总周长
#计算周长
for i in range(4):
for j in range(4):
if list == 1:
s += zcd(i,j,list)
print(s) def P389(lit):
n = 0
row, column = len(lit), len(lit)
for r in range(row):
for c in range(column):
i = 0
if lit == 0:
continue
else:
if r != 0 and lit == 1:
i += 1
## print('1:',i,end=' ')
if c != 0 and lit == 1:
i += 1
## print('2:',i,end=' ')
if r != row - 1 and lit == 1:
i += 1
## print('3:',i,end=' ')
if c != column - 1 and lit == 1:
i += 1
## print('4:',i)
n += 4-i
## print('n:',n)
return n
lit = [
,
,
,
]
print(P389(lit))
萌新求大佬赐教 def fun389(lst):
result = []
for row in range(len(lst)):
for col in range(len(lst)):
count =[]
if lst == 1:
count.extend() + 1, col - len(lst) + 1])
if row > 0:
count.append(lst)
if row + 1 < len(lst) - 1:
count.append(lst)
if col > 0:
count.append(lst)
if col + 1 < len(lst) - 1:
count.append(lst)
result.append(count.count(0))
return sum(result)
页:
[1]
2