from collections import deque
n=int(input())
mg=[]
for i in range(n):
mg.append(list(input()))
vis=[[0]*n for i in range(n)]
def bfs(x,y):
global flag
q=deque([(x,y)])
vis[x][y]=1
while q:
x,y=q.popleft()
if mg[x][y + 1] == '#' and mg[x][y - 1] == '#' and mg[x + 1][y] == '#' and mg[x - 1][y] == '#':
flag = 1
for dx, dy in [(1, 0), (0, 1), (0, -1), (-1, 0)]:
nx = dx + x
ny = dy + y
if vis[nx][ny]!=1 and mg[nx][ny] == '#':
q.append((nx,ny))
vis[nx][ny]=1
cnt=0
for i in range(n):
for j in range(n):
if mg[i][j]=='#' and vis[i][j]!=1:
flag=0
bfs(i,j)
if flag==0:
cnt+=1
print(cnt)
from collections import deque
import sys
s=input()
ed=input()
l=[1,-1,2,-2,3,-3]
q=deque([(list(s),0)])
all_st=set()
all_st.add(s)
while q:
s1,step=q.popleft()
for dx in l:
ns1 = s1.copy()
x = ns1.index('*')
nx=dx+x
if 0<=nx<len(s):
ns1[x],ns1[nx]=ns1[nx],ns1[x]
str_ns1=''.join(ns1)
now_step=step+1
if str_ns1 == ed:
print(now_step)
sys.exit(0)
if str_ns1 not in all_st:
all_st.add(str_ns1)
q.append((ns1,now_step))
该题与青蛙跳杯子思路一样,但该题是二维的,故在存储和判重方面有些许不同
注意:该题如果用二维列表,就不能用copy函数,因为copy函数对于一维列表与深拷贝效果一样,但是对于二维列表则为浅拷贝
还有一点要注意的就是空格可以不放在原位置,笔者就是掉进了这两个坑里,调了半天。
列表储存状态法
import sys
from collections import deque
import copy
l=[]
s1=input()
s2=input()
s=s1+s2
l.append(list(s1))
l.append(list(s2))
for i in range(2):
for j in range(3):
if l[i][j] == ' ':
_x, _y = i, j
if l[i][j] == 'A':
ax, ay = i, j
if l[i][j] == 'B':
bx, by = i, j
q=deque([])
q.append([l,0,_x,_y])
vis=set()
vis.add(s)
for i in range(2):
for j in range(3):
if l[i][j] == 'A':
ax, ay = i, j
if l[i][j] == 'B':
bx, by = i, j
el=copy.deepcopy(l)
el[ax][ay],el[bx][by]=el[bx][by],el[ax][ay]
es1 = ''.join(el[0])
es2 = ''.join(el[1])
es=es1+es2
# print(s)
# print(es)
while q:
lis,d,x,y=q.popleft()
# for i in range(2):
# print(lis[i])
# print()
for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
lis_copy = copy.deepcopy(lis)
nx=dx+x
ny=dy+y
nd = d + 1
if 0<=nx<2 and 0<=ny<3:
lis_copy[x][y],lis_copy[nx][ny]=lis_copy[nx][ny],lis_copy[x][y]
s1=''.join(lis_copy[0])
s2=''.join(lis_copy[1])
s=s1+s2
# if s==es:#错误
if lis_copy[ax][ay]=='B' and lis_copy[bx][by]=='A':
print(nd)
sys.exit(0)
if s not in vis:
vis.add(s)
q.append([lis_copy,nd,nx,ny])
class储存状态法
import sys
from collections import deque
import copy
l=[]
s1=input()
s2=input()
s=s1+s2
l.append(list(s1))
l.append(list(s2))
for i in range(2):
for j in range(3):
if l[i][j] == ' ':
_x, _y = i, j
if l[i][j] == 'A':
ax, ay = i, j
if l[i][j] == 'B':
bx, by = i, j
q=deque([])
class Point:
def __init__(self,x,y,state,step):
self.x=x ; self.y=y
self.state=state
self.step=step
q.append(Point(_x,_y,l,0))
vis=set()
vis.add(s)
for i in range(2):
for j in range(3):
if l[i][j] == 'A':
ax, ay = i, j
if l[i][j] == 'B':
bx, by = i, j
el=copy.deepcopy(l)
el[ax][ay],el[bx][by]=el[bx][by],el[ax][ay]
es1 = ''.join(el[0])
es2 = ''.join(el[1])
es=es1+es2
# print(s)
# print(es)
while q:
cu=q.popleft()
# for i in range(2):
# print(lis[i])
# print()
x = cu.x;y = cu.y
lis=cu.state
d=cu.step
for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
lis_copy = copy.deepcopy(lis)
nx=dx+x
ny=dy+y
nd = d + 1
if 0<=nx<2 and 0<=ny<3:
lis_copy[x][y],lis_copy[nx][ny]=lis_copy[nx][ny],lis_copy[x][y]
s1=''.join(lis_copy[0])
s2=''.join(lis_copy[1])
s=s1+s2
# if s==es:
if lis_copy[ax][ay]=='B' and lis_copy[bx][by]=='A':
print(nd)
sys.exit(0)
if s not in vis:
vis.add(s)
q.append(Point(nx,ny,lis_copy,nd))