Problem: We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).  The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.  You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.  You now have to take Q practical tests of your ability as a magical girl.  The i-th test will be conducted as follows:    * Initially, a piece is placed on the square where the integer L_i is written.    * Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.    * Here, it is guaranteed that R_i-L_i is a multiple of D.  For each test, find the sum of magic points consumed during that test.

Submission attempts (in order):
H, W, D=[int(x) for x in str(input()).split()]
A=[[0 for x in range(W+1)]]
for i in range(1,H+1):
  b=[0]
  b+=[int(x) for x in str(input()).split()]
  A.append(b)
Q=int(input())
LR=[[0 for x in range(3)]]
for i in range(1,Q+1):
  b=[0]
  b+=[int(x) for x in str(input()).split()]
  LR.append(b)












B=[0 for x in range(H*W+1)]
for i in range(1,W+1):
  for j in range(1,H+1):
    B[A[j][i]]=[j,i]

print(B)

XY=[0]
for i in range(1,D+1):

  C=[]
  x=i
  s=0
  while x<(H*W-D+1):

    s+=abs(B[x][0]-B[x+D][0])+abs(B[x][1]-B[x+D][1])
    C.append(s)
    x+=D

  C.append(s+abs(B[i][0]-B[x][0])+abs(B[i][1]-B[x][1]))
  XY.append(C)
print(XY)


def mo(x,y):
  if x%y==0:
    return y
  else:
    return x%y

def br(x):
  if x<=0:
    return 0
  else:
    return int(x)

ans=[]
for i in range (1,Q+1):
  if LR[i][1]<LR[i][2] and LR[i][1]==mo(LR[i][1],D):
    g=XY[mo(LR[i][1],D)][br((LR[i][1]-0.1)//D-1)]
    h=XY[mo(LR[i][1],D)][br((LR[i][2]-0.1)//D-1)]

    ans.append(h)
  elif LR[i][1]<LR[i][2]:
    g=XY[mo(LR[i][1],D)][br((LR[i][1]-0.1)//D-1)]
    h=XY[mo(LR[i][1],D)][br((LR[i][2]-0.1)//D-1)]

    ans.append(h-g)
  elif LR[i][1]>LR[i][2]:
    g=XY[mo(LR[i][2],D)][br((LR[i][2]+0.1)//D)-2]
    h=XY[mo(LR[i][2],D)][-1]-XY[mo(LR[i][1],D)][br((LR[i][2]+0.1)//D-2)]
    ans.append(h+g)
  else:
    ans.append(0)

for i in ans:
  print(i)



H, W, D=[int(x) for x in str(input()).split()]
A=[[0 for x in range(W+1)]]
for i in range(1,H+1):
  b=[0]
  b+=[int(x) for x in str(input()).split()]
  A.append(b)
Q=int(input())
LR=[[0 for x in range(3)]]
for i in range(1,Q+1):
  b=[0]
  b+=[int(x) for x in str(input()).split()]
  LR.append(b)












B=[0 for x in range(H*W+1)]
for i in range(1,W+1):
  for j in range(1,H+1):
    B[A[j][i]]=[j,i]

XY=[0]
for i in range(1,D+1):

  C=[]
  x=i
  s=0
  while x<(H*W-D+1):

    s+=abs(B[x][0]-B[x+D][0])+abs(B[x][1]-B[x+D][1])
    C.append(s)
    x+=D

  C.append(s+abs(B[i][0]-B[x][0])+abs(B[i][1]-B[x][1]))
  XY.append(C)


def mo(x,y):
  if x%y==0:
    return y
  else:
    return x%y

def br(x):
  if x<=0:
    return 0
  else:
    return int(x)

ans=[]
for i in range (1,Q+1):
  if LR[i][1]<LR[i][2] and LR[i][1]==mo(LR[i][1],D):
    g=XY[mo(LR[i][1],D)][br((LR[i][1]-0.1)//D-1)]
    h=XY[mo(LR[i][1],D)][br((LR[i][2]-0.1)//D-1)]

    ans.append(h)
  elif LR[i][1]<LR[i][2]:
    g=XY[mo(LR[i][1],D)][br((LR[i][1]-0.1)//D-1)]
    h=XY[mo(LR[i][1],D)][br((LR[i][2]-0.1)//D-1)]

    ans.append(h-g)
  elif LR[i][1]>LR[i][2]:
    g=XY[mo(LR[i][2],D)][br((LR[i][2]+0.1)//D)-2]
    h=XY[mo(LR[i][2],D)][-1]-XY[mo(LR[i][1],D)][br((LR[i][2]+0.1)//D-2)]
    ans.append(h+g)
  else:
    ans.append(0)

for i in ans:
  print(i)



Verdicts: Wrong Answer

Accepted