Python代码
from collections import deque
g=[]
for i in range(4):
g.append(list(input()))
ans=deque()
tmp=deque()
def turn_one(x,y):
g[x][y]='+' if g[x][y]=='-' else '-'
def turn_all(x,y):
turn_one(x,y)
for i in range(4):
turn_one(x,i)
turn_one(i,y)
def dfs(x,y):
global ans,tmp
if x==3 and y==4:
success=True
for i in range(4):
for j in range(4):
if g[i][j]=='+':
success=False
break
if success :
if len(ans)<len(tmp):
ans=tmp.copy()
return
if y==4:
x+=1
y=0
#动开关
tmp.append((x,y))
turn_all(x,y)
dfs(x,y+1)
#恢复现场
tmp.pop()
turn_all(x,y)
#不动开关
dfs(x,y+1)
dfs(0,0)
print(len(ans)) # 输出最优解的长度
for i in range(len(ans)):
step = ans[i]
step = (step[0] + 1, step[1] + 1) # 每个数字加 1
print(step[0], step[1]) # 逐行输出最优解的步骤