题目描述
blablabla
算法1
(暴力dfs) $O(n^2)$
input_ = lambda : map(int,input().strip().split(" "))
R,S = input_()
drc = [(1,0),(0,1),(-1,0),(0,-1)]
l = [input().strip() for i in range(R)]
def f(x,y,s = set(),ans=0):
global drc,l
for xi,yi in drc:
xr,yr = xi+x,yi+y
if 0<=xr<R and 0<=yr<S and l[xr][yr] not in s:
for t in f(xr,yr,s|set(l[xr][yr]),ans+1):
yield t
yield ans
print(max(f(0,0,set(l[0][0]),1)))
时间复杂度