Pagini recente » Cod sursa (job #1080493) | Cod sursa (job #769436) | Cod sursa (job #1052894) | Cod sursa (job #625143) | Cod sursa (job #2666003)
import queue
ox = [-1, 0, 1, -1, 1, -1, 0, 1]
oy = [-1, -1, -1, 0, 0, 1, 1, 1]
def isValid(pos, matPos, matLen):
global n, m
i = pos[0]
j = pos[1]
if i < 0 or j < 0 or i >= n or j >= m:
return False
if matPos[i][j] == 'X' or matLen[i][j] != -1:
return False
return True
def lee(matPos, matLen, root):
global ox, oy
q = queue.Queue()
matLen[root[0]][root[1]] = 0
q.put(root)
while q.qsize() > 0:
curr_pos = q.get()
for i in bound_moves:
next_pos = (curr_pos[0] + ox[i], curr_pos[1] + oy[i])
if isValid(next_pos, matPos, matLen):
matLen[next_pos[0]][next_pos[1]
] = matLen[curr_pos[0]][curr_pos[1]] + 1
q.put(next_pos)
bound = range(100)
bound_moves = range(8)
with open("rj.in", 'r') as fin:
tmp = fin.readline().split()
n = int(tmp[0])
m = int(tmp[1])
bound_n = range(n)
bound_m = range(m)
mat = [[0] * m for _ in bound]
mat1 = [[-1] * m for _ in bound]
mat2 = [[-1] * m for _ in bound]
mat = fin.readlines()
for i in bound_n:
for j in bound_m:
if mat[i][j] == 'R':
root1 = (i, j)
elif mat[i][j] == 'J':
root2 = (i, j)
lee(mat, mat1, root1)
lee(mat, mat2, root2)
rez_len = 10e5
for i in bound_n:
for j in bound_m:
if mat1[i][j] == mat2[i][j] and mat1[i][j] != -1:
if mat1[i][j] < rez_len:
rez_len = mat1[i][j]
rez_pos = (i+1, j+1)
with open("rj.out", 'w') as fout:
fout.write(" ".join(str(x) for x in [rez_len+1, rez_pos[0], rez_pos[1]]))