s_id stringlengths 10 10 | p_id stringlengths 6 6 | u_id stringlengths 10 10 | date stringlengths 10 10 | language stringclasses 1
value | original_language stringclasses 11
values | filename_ext stringclasses 1
value | status stringclasses 1
value | cpu_time stringlengths 1 5 | memory stringlengths 1 7 | code_size stringlengths 1 6 | code stringlengths 1 539k |
|---|---|---|---|---|---|---|---|---|---|---|---|
s704836804 | p00481 | u940190657 | 1433661599 | Python | Python3 | py | Runtime Error | 19930 | 18644 | 1396 | import queue
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# initialization visited maps
def init_visited_maps(h, w):
return [[True for i in range(w)] for j in range(h)]
# bfs algorithm
def bfs(h, w, cheese, maps, sx, sy, moves):
que = queue.Queue()
que.put((moves, sx, sy))
visited = init_visited_maps(h, w)
visited[sy][sx] = False
while que.qsize() > 0:
c_moves, c_x, c_y = que.get()
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x] and maps[n_y][n_x] != 'X'):
que.put((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# simulate mouse
def simulate(h, w, n, maps, sx, sy):
moves = 0
x, y = sx, sy
for i in range(1, n+1, 1):
moves, x, y = bfs(h, w, str(i), maps, x, y, moves)
return str(moves)
# main function
def main():
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
maps = []
sx, sy = 0, 0
for i in range(H):
maps.append(input())
for j in range(W):
if maps[i][j] == 'S':
sx, sy = j, i
print(simulate(H, W, N, maps, sx, sy))
if __name__ == '__main__':
main() |
s207319350 | p00481 | u940190657 | 1433662020 | Python | Python3 | py | Runtime Error | 19920 | 18648 | 1404 | import queue
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# bfs algorithm
def bfs(h, w, cheese, maps, sx, sy, moves, visited):
que = queue.Queue()
que.put((moves, sx, sy))
for i in range(h):
for j in range(w):
visited[i][j] = True
visited[sy][sx] = False
while que.qsize() > 0:
c_moves, c_x, c_y = que.get()
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x] and maps[n_y][n_x] != 'X'):
que.put((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# simulate mouse
def simulate(h, w, n, maps, sx, sy):
moves = 0
x, y = sx, sy
visited = [[True for i in range(w)] for j in range(h)]
for i in range(1, n+1, 1):
moves, x, y = bfs(h, w, str(i), maps, x, y, moves, visited)
return str(moves)
# main function
def main():
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
maps = []
sx, sy = 0, 0
for i in range(H):
maps.append(input())
for j in range(W):
if maps[i][j] == 'S':
sx, sy = j, i
print(simulate(H, W, N, maps, sx, sy))
if __name__ == '__main__':
main() |
s226872146 | p00481 | u940190657 | 1433663434 | Python | Python3 | py | Runtime Error | 19920 | 19492 | 1438 | import queue
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
visited = [[True for i in range(1000)] for j in range(1000)]
maps = []
# bfs algorithm
def bfs(h, w, cheese, sx, sy, moves):
que = queue.Queue()
que.put((moves, sx, sy))
for i in range(h):
for j in range(w):
visited[i][j] = (maps[i][j] != 'X')
visited[sy][sx] = False
while que.qsize() > 0:
c_moves, c_x, c_y = que.get()
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x]):
que.put((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# simulate mouse
def simulate(h, w, n, sx, sy):
moves = 0
x, y = sx, sy
#visited = [[True for i in range(w)] for j in range(h)]
for i in range(1, n+1, 1):
moves, x, y = bfs(h, w, str(i), x, y, moves)
return str(moves)
# main function
def main():
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
del str
#maps = []
sx, sy = 0, 0
for i in range(H):
maps.append(input())
for j in range(W):
if maps[i][j] == 'S':
sx, sy = j, i
print(simulate(H, W, N, sx, sy))
if __name__ == '__main__':
main() |
s279781729 | p00481 | u940190657 | 1433664782 | Python | Python3 | py | Runtime Error | 19920 | 18612 | 1280 | import queue
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# map info
visited = []
maps = []
# bfs algorithm
def bfs(h, w, cheese, sx, sy, moves):
que = queue.Queue()
que.put((moves, sx, sy))
for i in range(h):
for j in range(w):
visited[i][j] = (maps[i][j] != 'X')
visited[sy][sx] = False
while que.qsize() > 0:
c_moves, c_x, c_y = que.get()
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x]):
que.put((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# main function
if __name__ == '__main__':
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
del str
visited = [[True for i in range(W)] for j in range(H)]
maps = ["" for i in range(H)]
x, y = 0, 0
for i in range(H):
maps[i] = input()
for j in range(W):
if maps[i][j] == 'S':
x, y = j, i
moves = 0
for i in range(1, N+1, 1):
moves, x, y = bfs(H, W, str(i), x, y, moves)
print(str(moves)) |
s945219944 | p00481 | u940190657 | 1434379919 | Python | Python3 | py | Runtime Error | 0 | 0 | 1228 | # directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# map info
visited = []
maps = []
# bfs algorithm
def bfs(h, w, cheese, sx, sy, moves):
que = [(moves, sx, sy)]
for i in range(h):
for j in range(w):
visited[i][j] = (maps[i][j] != 'X')
visited[sy][sx] = False
while len(que) > 0:
c_moves, c_x, c_y = que.pop(0)
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x]):
que.append((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# main function
if __name__ == '__main__':
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
visited = [[True for i in range(W)] for j in range(H)]
maps = ["" for i in range(H)]
x, y = 0, 0
for i in range(H):
maps[i] = input()
for j in range(W):
if maps[i][j] == 'S':
x, y = j, i
moves = 0
for i in range(1, N+1, 1):
moves, x, y = bfs(H, W, str(i), x, y, moves)
print(str(moves)) |
s069891944 | p00481 | u421925564 | 1461728425 | Python | Python3 | py | Runtime Error | 40000 | 47972 | 1422 | import copy
h = w = 0
mymap =[]
const_mymap = []
qlist = []
ans = 0
def bfs(n):
while True:
if len(qlist) != 0:
temp = qlist.pop(0)
x = temp[0]
y = temp[1]
deep = temp[2]
#print(temp)
if mymap[y][x] == str(n):
return temp
mymap[y] = mymap[y][:x] + '*' + mymap[y][x + 1:]
if x + 1 < w:
if mymap[y][x + 1] not in '*X':
qlist.append([x + 1, y, deep + 1])
if x - 1 >= 0:
if mymap[y][x - 1] not in '*X':
qlist.append([x - 1, y, deep + 1])
if y + 1 < h:
if mymap[y + 1][x] not in '*X':
qlist.append([x, y + 1, deep + 1])
if y - 1 >= 0:
if mymap[y - 1][x] not in '*X':
qlist.append([x, y - 1, deep + 1])
else:
break
h, w, n = list(map(lambda x: int(x), input().split(" ")))
for i in range(0, h):
const_mymap.append(input())
mymap = copy.deepcopy(const_mymap)
for i in range(0, h):
for j in range(0, w):
if mymap[i][j] == 'S':
nstart = [j, i, 0]
qlist.append(nstart)
for i in range (n):
mymap = copy.deepcopy(const_mymap)
temp = bfs(i+1)
#print(temp)
#print(const_mymap)
ans = temp[2]
qlist = []
qlist.append(temp)
#print(ans)
print(ans) |
s529528600 | p00481 | u884495090 | 1461826976 | Python | Python | py | Runtime Error | 40000 | 38828 | 849 | #!/usr/bin/env python
vector = [[1,0],[-1,0],[0,1],[0,-1]]
matrix = []
visited = []
q = []
h,w,n = map(int,raw_input().split())
s_x = 0
s_y = 0
for i in xrange(h):
matrix.append( list(raw_input()))
visited.append([0]*w)
if 'S' in matrix[i]:
s_y = i
s_x = matrix[i].index('S')
ans = 0
for t in xrange(n):
t+=1
q = []
#init visited
visited = []
for i in xrange(h):
visited.append([0]*w)
#search start
q.append((s_x,s_y,0))
while 1:
now = q.pop(0)
visited[now[1]][now[0]] = 1
if matrix[now[1]][now[0]] == str(t):
s_x = now[0]
s_y = now[1]
ans += now[2]
break
for dv in vector:
nx = now[0]+dv[0]
ny = now[1]+dv[1]
ns = now[2]+1
if nx in xrange(w) and ny in xrange(h) and matrix[ny][nx]!='X' and visited[ny][nx] == 0:
q.append((nx,ny,ns))
print ans |
s856809203 | p00481 | u577311000 | 1493719860 | Python | Python3 | py | Runtime Error | 40000 | 7800 | 1342 | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**7)
def search(field,distance,ch,cv,eh,ev,step):
if not (0<=ch<len(field)): return
if not (0<=cv<len(field[ch])): return
if field[ch][cv]=='X': return
if distance[ch][cv]<step: return
distance[ch][cv] = step
search(field,distance,ch+1,cv,eh,ev,step+1)
search(field,distance,ch-1,cv,eh,ev,step+1)
search(field,distance,ch,cv+1,eh,ev,step+1)
search(field,distance,ch,cv-1,eh,ev,step+1)
def calculate_distance(field,locations,current_HP):
ch,cv=locations[current_HP-1]
eh,ev=locations[current_HP]
distance=[[10**6 for _ in range(len(field[0]))] for __ in range(len(field))]
search(field,distance,ch,cv,eh,ev,0)
# print('='*50)
# for v in distance:
# print(v)
# print('-'*50)
return distance[eh][ev]
# return 0
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += calculate_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main() |
s208116348 | p00481 | u577311000 | 1493723004 | Python | Python3 | py | Runtime Error | 0 | 0 | 1355 | # -*- coding: utf-8 -*-
def get_shortest_distance(field,locations,current_HP):
is_visit = [[False for _ in range(len(field[0]))] for __ in range(len(field))]
frontier = set()
frontier.add(locations[current_HP-1])
for step in xrange(10**10):
for cx,cy in frontier:
if (cx,cy)==locations[current_HP]: return step
is_visit[cx][cy]=True
next_frontier = set()
for cx,cy in list(frontier):
for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
nx = cx+dx
ny = cy+dy
if not (0<=nx<len(field)): continue
if not (0<=ny<len(field[nx])): continue
if field[nx][ny]=='X': continue
if is_visit[nx][ny]: continue
next_frontier.add((nx,ny))
# assert len(next_frontier)!=0,(current_HP,is_visit,frontier)
frontier = next_frontier
# assert True,('the goal is not reachable')
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += get_shortest_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main() |
s336608824 | p00481 | u940389926 | 1493742517 | Python | Python3 | py | Runtime Error | 0 | 0 | 3212 | import sys
import numpy
sys.setrecursionlimit(1000000)
class MouseClass:
def __init__(self):
self.X = 0
self.Y = 0
self.LIFE = 1
self.factoriesAble = []
self.factoriesGone = []
self.factoriesWill = []
self.distance = 0
def get_field(self, field:list):
self.field = field
def gen_factoriesInfo(self, factoriesInfo:list):
self.factoriesAble = [i for i in factoriesInfo if i[2]<=self.LIFE]
self.factoriesWill = [factory for factory in self.factoriesAble if factory not in self.factoriesGone]
def findNearestFactory(self, factories:list):
path = [abs(self.X-factory[0]) + abs(self.Y-factory[1]) for factory in factories]
return factories[numpy.argmin(path)]
def goFactory(self, factories:list):
factory = self.findNearestFactory(factories)
# print(abs(self.X-factory[0])+abs(self.Y-factory[1]))
self.distance += self.calcShortestPath(self.X, self.Y, self.field, factory[0:2])
self.X, self.Y = factory[0], factory[1]
self.LIFE += 1
self.factoriesGone.append(factory)
def calcShortestPath(self, x:int, y:int, field:list, goal:list):
print(x, y, goal)
INF = 1000000
d = []
queue = []
H = len(self.field)
W = len(self.field[0])
for _ in range(H):
tmp = []
for _ in range(W):
tmp.append(INF)
d.append(tmp)
queue.append([x, y])
d[x][y] = 0
while len(queue)!=0:
p = queue[0]
queue.pop(0)
if p==goal: break
for dx in [-1, 0, 1]:
if dx == -1: width = [0]
if dx == 0: width = [-1, 1]
if dx == 1: width = [0]
for dy in width:
nx = p[0]+dx
ny = p[1]+dy
if 0 <= nx < H and 0 <= ny < W:
isObstacle = field[nx][ny] == "X"
isNotVisited = d[nx][ny] == INF
if (not isObstacle) and isNotVisited:
queue.append([nx, ny])
d[nx][ny] = d[p[0]][p[1]]+1
return d[goal[0]][goal[1]]
class FieldClass:
def __init__(self):
self.field = []
self.Mouse = MouseClass()
self.factoriesInfo = []
def loadField(self):
field = []
i = 0
H, W = 0, 0
LINE_NUM = 0
field = []
while True:
line = sys.stdin.readline().strip()
LINE_NUM += 1
if LINE_NUM == 1:
line = line.split(" ")
H, W = int(line[0]), int(line[1])
continue
self.field.append(list(line))
if LINE_NUM == H+1: break
self.Mouse.get_field(self.field)
def initMousePos(self):
H = len(self.field)
W = len(self.field[0])
for i in range(H):
for j in range(W):
if self.field[i][j] == "S":
self.Mouse.X = i
self.Mouse.Y = j
def gen_factoriesInfo(self):
NUM_LIST = [str(i) for i in range(10)]
H = len(self.field)
W = len(self.field[0])
factories = []
for i in range(H):
for j in range(W):
if self.field[i][j] in NUM_LIST:
factories.append([i, j, int(self.field[i][j])])
self.factoriesInfo = factories
Field = FieldClass()
Field.loadField()
Field.initMousePos()
Field.gen_factoriesInfo()
Field.Mouse.gen_factoriesInfo(Field.factoriesInfo)
while True:
Field.Mouse.goFactory(Field.Mouse.factoriesWill)
Field.Mouse.gen_factoriesInfo(Field.factoriesInfo)
# print(Field.Mouse.factoriesAble)
# print(Field.Mouse.factoriesGone)
# print(Field.Mouse.factoriesWill)
if len(Field.Mouse.factoriesGone) == 9: break
print(Field.Mouse.distance) |
s007533167 | p00481 | u945345165 | 1494905257 | Python | Python3 | py | Runtime Error | 40000 | 69400 | 1001 | def bfs(count, x, y, cheese):
queue = [(count,x,y)]
while len(queue)>0:
tempc,tempx,tempy = queue.pop(0)
if geo[tempx][tempy] == cheese:
return tempc,tempx,tempy
for i in range(4):
neox = tempx+dx[i]
neoy = tempy+dy[i]
if canGoTo(neox,neoy):
queue.append((tempc+1,neox,neoy))
return (-1,-1,-1)
def canGoTo(x,y):
if 0<=x<H and 0<=y<W:
return not visited[x][y]
return False
dx = [0,1,0,-1]
dy = [-1,0,1,0]
data = input().split(' ')
H,W,N = map(int, data)
#maps
geo = [[None for i in range(W)] for j in range(H)]
visited = [[False for i in range(W)] for j in range(H)]
x,y=-1,-1
for i in range(H):
line = input()
for j in range(W):
p = line[j:j+1]
geo[i][j] = p
if p == 'X':
visited[i][j] = True
elif p == 'S':
x,y, = i,j
count = 0
for n in range(1,N+1):
count,x,y = bfs(count,x,y,str(n))
print(count) |
s450836795 | p00481 | u945345165 | 1494927972 | Python | Python3 | py | Runtime Error | 40000 | 7896 | 1058 | def bfs(count, x, y, cheese):
queue = [(count,x,y)]
while len(queue)>0:
tempc,tempx,tempy = queue.pop(0)
if geo[tempx][tempy] == cheese:
return tempc,tempx,tempy
for i in range(4):
neox = tempx+dx[i]
neoy = tempy+dy[i]
if canGoTo(neox,neoy):
if (tempc+1,neox,neoy) not in queue:
queue.append((tempc+1,neox,neoy))
return (-1,-1,-1)
def canGoTo(x,y):
if 0<=x<H and 0<=y<W:
return not visited[x][y]
return False
dx = [0,1,0,-1]
dy = [-1,0,1,0]
data = input().split(' ')
H,W,N = map(int, data)
#maps
geo = [[None for i in range(W)] for j in range(H)]
visited = [[False for i in range(W)] for j in range(H)]
x,y=-1,-1
for i in range(H):
line = input()
for j in range(W):
p = line[j:j+1]
geo[i][j] = p
if p == 'X':
visited[i][j] = True
elif p == 'S':
x,y, = i,j
count = 0
for n in range(1,N+1):
count,x,y = bfs(count,x,y,str(n))
print(count) |
s041011298 | p00481 | u028939600 | 1495362775 | Python | Python3 | py | Runtime Error | 0 | 0 | 2389 | import sys
sys.setrecursionlimit(100000000)
def getStart(field): # before this, cut the first [H,W,N]
search_field = field[1:]
for i in range(len(search_field)):
for j in range(len(search_field[0])):
if search_field[i][j] == "S":
print("start point ({},{})".format(i+1,j))
return i+1,j
def bfs(field,start_x,start_y,goal_N):
x_direction = [-1,0,1,0]
y_direction = [0,-1,0,1]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
while len(que) != 0:
current = que.pop(0)
for xd,yd in zip(x_direction,y_direction):
nx = current[0] + xd
ny = current[1] + yd
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X":
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print("GOAL",current[3]+1)
return
else:
que.append([nx,ny,current[2]+1,current[3]+1])
else:
que.append([nx,ny,current[2],current[3]+1])
def getField():
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
break
return matrix
def main():
# field = [[4,5,2],
# [".","X",".",".",1],
# [".",".",".",".","X"],
# [".","X","X",".","S"],
# [".",2,".","X","."]]
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
field = getField()
sx,xy = getStart(field)
bfs(field,sx,xy,field[0][2])
main() |
s608561953 | p00481 | u028939600 | 1495377707 | Python | Python3 | py | Runtime Error | 0 | 0 | 2047 | def getField():
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
break
return matrix
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
print("pop",current)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main():
field = getField()
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
main() |
s897980623 | p00481 | u028939600 | 1495378199 | Python | Python3 | py | Runtime Error | 0 | 0 | 1941 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
print("pop",current)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
main()
break |
s639789445 | p00481 | u028939600 | 1495378389 | Python | Python3 | py | Runtime Error | 0 | 0 | 2486 | import sys
sys.setrecursionlimit(100000000)
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
print("pop",current)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
main()
break |
s396944705 | p00481 | u028939600 | 1495379029 | Python | Python3 | py | Runtime Error | 0 | 0 | 2412 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
main()
break |
s155197109 | p00481 | u028939600 | 1495379096 | Python | Python3 | py | Runtime Error | 0 | 0 | 2418 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
main(matrix)
break |
s607279528 | p00481 | u028939600 | 1495379261 | Python | Python3 | py | Runtime Error | 0 | 0 | 2427 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().rstrip().split()
if len(row) == 1:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
else:
matrix.append(row)
if len(matrix) == int(matrix[0][0]) + 1:
main(matrix)
break |
s373074667 | p00481 | u028939600 | 1495379902 | Python | Python3 | py | Runtime Error | 0 | 0 | 1662 | import sys
sys.setrecursionlimit(100000000)
moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def search(dataset):
min_time = 0
mouse = 1
gx, gy = get_start(dataset)
for i in xrange(N):
gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)
mouse += 1
min_time += min_time_tmp
print min_time
def bfs(dataset, start, mouse):
INF = 10000000
min_time = [[INF for j in xrange(W)] for i in xrange(H)]
min_time[start[0]][start[1]] = 0
queue = [start]
while len(queue) != 0:
dx, dy = queue.pop(0)
for i, j in moves:
nx = dx + i
ny = dy + j
if 0 <= nx < H and 0 <= ny < W and dataset[nx][ny] != 'X' and min_time[nx][ny] == INF:
cell = dataset[nx][ny]
else:
continue
if type(cell) is int:
if mouse == int(cell):
min_time[nx][ny] = min_time[dx][dy] + 1
return nx, ny, min_time[nx][ny]
min_time[nx][ny] = min_time[dx][dy] + 1
queue.append((nx, ny))
def get_start(dataset):
for i in xrange(H):
rows = dataset[i]
for j in xrange(W):
cell = rows[j]
if cell == 'S':
return i, j
dataset = []
while True:
line = raw_input().rstrip().split()
if len(line) == 3:
H, W, N = map(int, line)
else:
row = []
for e in line[0]:
if e.isdigit():
row.append(int(e))
else:
row.append(e)
dataset.append(row)
if len(dataset) == H:
search(dataset)
break |
s561358968 | p00481 | u028939600 | 1495385789 | Python | Python3 | py | Runtime Error | 0 | 0 | 1672 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().split()
for i in row:
try:
int(i)
except:
continue
field.append(row)
if len(field) == int(field[0][0]) + 1:
main(field)
break |
s422169442 | p00481 | u028939600 | 1495385902 | Python | Python3 | py | Runtime Error | 0 | 0 | 1689 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().split()
for i in range(len(row)):
try:
int(row[i])
except:
continue
field.append(row)
if len(field) == int(field[0][0]) + 1:
main(field)
break |
s794314891 | p00481 | u028939600 | 1495385952 | Python | Python3 | py | Runtime Error | 0 | 0 | 1698 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().split()
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
continue
field.append(row)
if len(field) == int(field[0][0]) + 1:
main(field)
break |
s848212421 | p00481 | u028939600 | 1495386065 | Python | Python3 | py | Runtime Error | 0 | 0 | 1707 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().rstrip().split()
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
continue
field.append(row)
if len(field) == int(field[0][0]) + 1:
main(field)
break |
s143027214 | p00481 | u028939600 | 1495386130 | Python | Python3 | py | Runtime Error | 0 | 0 | 1694 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().split()
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
pass
field.append(row)
if len(field) == int(field[0][0]) + 1:
main(field)
break |
s655580596 | p00481 | u028939600 | 1495386679 | Python | Python3 | py | Runtime Error | 0 | 0 | 1693 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = input().split()
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
continue
field.append(row)
if len(field) == field[0][0] + 1:
main(field)
break |
s261745845 | p00481 | u028939600 | 1495386969 | Python | Python3 | py | Runtime Error | 0 | 0 | 1695 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]:
if field[ny][nx] == goal_N:
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
field = []
while True:
row = [input().split()]
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
continue
field.append(row)
if len(field) == field[0][0] + 1:
main(field)
break |
s043942970 | p00481 | u028939600 | 1495387025 | Python | Python3 | py | Runtime Error | 0 | 0 | 2791 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y,gotten_cheese,distance])
INF = 1
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
if field[ny][nx] == current[2]: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(current[3]+1)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny,current[2]+1,current[3]+1])
min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
break
else:
que.append([nx,ny,current[2],current[3]+1])
min_path[ny-1][nx] = 0
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
f = []
while True:
row = input().split()
for i in range(len(row)):
try:
row[i] = int(row[i])
except:
continue
field.append(row)
if len(f) == field[0][0] + 1:
main(f)
break
# matrix = []
# while True:
# row = input().rstrip().split()
# if len(row) == 3:
# re_row = []
# for i in row:
# re_row.append(int(i))
# matrix.append(re_row)
# else:
# re_row = []
# for char in row[0]:
# if char.isdigit():
# re_row.append(int(char))
# else:
# re_row.append(char)
# matrix.append(re_row)
# if len(matrix) == int(matrix[0][0]) + 1:
# main(matrix)
# break |
s287158648 | p00481 | u028939600 | 1495426084 | Python | Python3 | py | Runtime Error | 0 | 0 | 2650 |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * field[0][1]] for i in range(field[0][0])] #[[INF for j in range(field[0][1])] for i in range(field[0][0])]
min_path[start_y-1][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
distance += min_path[current[1]-1][current[0]] + 1
if field[ny][nx] == gotten_cheese: # at the same cheese-power number
if field[ny][nx] == goal_N: # goal
print(distance)
return
else: # not goal, but could eat cheese
que = []
que.append([nx,ny])
gotten_cheese += 1
min_path = [[INF] * field[0][1]] for i in range(field[0][0])]
min_path[ny-1][nx] = 0
break
else:
que.append([nx,ny])
def main(field):
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
matrix = []
while True:
row = input().rstrip().split()
if len(row) == 3:
re_row = []
for i in row:
re_row.append(int(i))
matrix.append(re_row)
else:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
if len(matrix) == int(matrix[0][0]) + 1:
print(matrix)
main(matrix)
break |
s734329953 | p00481 | u028939600 | 1495429033 | Python | Python3 | py | Runtime Error | 30 | 8052 | 2598 | from collections import deque
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese = 1
distance = 0
que = deque()
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * field[0][1] for i in range(field[0][0])]
min_path[start_y-1][start_x] = 0
while len(que) != 0:
current = que.popleft()
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:
min_path[ny-1][nx] = min_path[current[1]-1][current[0]] + 1
if field[ny][nx] == gotten_cheese: # at the same cheese-power number
distance += min_path[ny-1][nx]
if field[ny][nx] == goal_N: # goal
print(distance)
return
else: # not goal, but could eat cheese
que = deque([nx,ny])
gotten_cheese += 1
min_path = [[INF] * field[0][1] for i in range(field[0][0])]
min_path[ny-1][nx] = 0
break
else:
que.append([nx,ny])
def main(field):
sx,sy = getStart(field)
bfs(field,sx,sy,field[0][2])
# field = [[10,10,9],
# [".","X",".",".",".","X",".","S",".","X"],
# [6,".",".",5,"X",".",".","X",1,"X"],
# [".",".",".","X","X","X","X",".",".","X"],
# ["X",".",".",9,"X",".",".",".","X","."],
# [8,".","X",2,"X",".",".","X",3,"X"],
# [".",".",".","X","X",".","X",4,".","."],
# ["X","X",".",".",".",".",7,"X",".","."],
# ["X",".",".","X",".",".","X","X",".","."],
# ["X",".",".",".","X",".","X","X",".","."],
# [".",".","X",".",".",".",".",".",".","."]]
#
# main(field)
matrix = []
while True:
row = input().rstrip().split()
if len(row) == 3:
re_row = []
for i in row:
re_row.append(int(i))
matrix.append(re_row)
else:
re_row = []
for char in row[0]:
if char.isdigit():
re_row.append(int(char))
else:
re_row.append(char)
matrix.append(re_row)
if len(matrix) == int(matrix[0][0]) + 1:
main(matrix)
break |
s828713832 | p00481 | u028939600 | 1495436212 | Python | Python3 | py | Runtime Error | 0 | 0 | 1754 |
def bfs(field,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * field[0][1] for i in range(field[0][0])]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < field[0][1] and 0 <= ny < field[0][0] and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N: # at the same cheese-power number
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = input().strip()
H,W,N = map(int,first.split())
for i in range(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,N
def getStart(field):
for y in range(len(field)):
for x in range(len(field[0])):
if search_field[y][x] == "S":
return x,y
def main():
matrix,N = getField()
sx,sy = getStart(matrix)
distance = 0
for k in range(N):
if k != 0:
tmp_x,tmp_y,tmp_dist = dfs(matrix,tmps[0],tmps[1],k+1)
distance += tmp_dist
tmps = []
tmps.append(tmp_x,tmp_y,tmp_dist)
else:
tmp_x,tmp_y,tmp_dist = dfs(matrix,sx,sy,k+1)
distance += tmp_dist
tmps = []
tmps.append(tmp_x,tmp_y)
print(distance)
if __name__=='__main__':
main() |
s620733962 | p00481 | u028939600 | 1495437196 | Python | Python3 | py | Runtime Error | 0 | 0 | 1609 | moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def search(dataset):
min_time = 0
mouse = 1
gx, gy = get_start(dataset)
for i in xrange(N):
gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)
mouse += 1
min_time += min_time_tmp
print(min_time)
def bfs(dataset, start, mouse):
INF = 10000000
min_time = [[INF for j in range(W)] for i in range(H)]
min_time[start[0]][start[1]] = 0
queue = [start]
while len(queue) != 0:
dx, dy = queue.pop(0)
for i, j in moves:
nx = dx + i
ny = dy + j
if 0 <= nx < H and 0 <= ny < W and dataset[nx][ny] != 'X' and min_time[nx][ny] == INF:
cell = dataset[nx][ny]
else:
continue
if type(cell) is int:
if mouse == int(cell):
min_time[nx][ny] = min_time[dx][dy] + 1
return nx, ny, min_time[nx][ny]
min_time[nx][ny] = min_time[dx][dy] + 1
queue.append((nx, ny))
def get_start(dataset):
for i in range(H):
rows = dataset[i]
for j in range(W):
cell = rows[j]
if cell == 'S':
return i, j
dataset = []
while True:
line = input().rstrip().split()
if len(line) == 3:
H, W, N = map(int, line)
else:
row = []
for e in line[0]:
if e.isdigit():
row.append(int(e))
else:
row.append(e)
dataset.append(row)
if len(dataset) == H:
search(dataset)
break |
s687817971 | p00481 | u028939600 | 1495437895 | Python | Python3 | py | Runtime Error | 0 | 0 | 1563 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = input().strip()
H,W,N = map(int,first.split())
for i in range(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,k+1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in range(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print(distance)
if __name__=='__main__':
main() |
s295165093 | p00481 | u198881450 | 1495438126 | Python | Python3 | py | Runtime Error | 0 | 0 | 1563 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = input().strip()
H,W,N = map(int,first.split())
for i in range(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,k+1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in range(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print(distance)
if __name__=='__main__':
main() |
s621955689 | p00481 | u198881450 | 1495438309 | Python | Python | py | Runtime Error | 0 | 0 | 1570 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in xrange(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = raw_input().rstrip()
H,W,N = map(int,first.split())
for i in xrange(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in xrange(len(field)):
for x in xrange(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in xrange(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print distance
if __name__=='__main__':
main() |
s345091932 | p00481 | u198881450 | 1495438449 | Python | Python | py | Runtime Error | 0 | 0 | 1571 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in xrange(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = raw_input().rstrip()
H,W,N = map(int,first.split())
for i in xrange(H):
row = list(input().strip())
for j in xrange(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in xrange(len(field)):
for x in xrange(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in xrange(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print distance
if __name__=='__main__':
main() |
s012309421 | p00481 | u198881450 | 1495438542 | Python | Python | py | Runtime Error | 0 | 0 | 1571 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in xrange(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = raw_input().rstrip()
H,W,N = map(int,first.split())
for i in xrange(H):
row = list(input().strip())
for j in xrange(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in xrange(len(field)):
for x in xrange(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in xrange(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print distance
if __name__=='__main__':
main() |
s080984529 | p00481 | u198881450 | 1495438558 | Python | Python | py | Runtime Error | 0 | 0 | 1542 | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in xrange(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = raw_input().rstrip()
H,W,N = map(int,first.split())
for i in xrange(H):
row = list(input().strip())
for j in xrange(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in xrange(len(field)):
for x in xrange(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in xrange(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print distance
main() |
s105604092 | p00481 | u695154284 | 1505318719 | Python | Python3 | py | Runtime Error | 0 | 0 | 1210 | from collections import deque
d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def bfs(n):
global pos_x, pos_y
while len(que) != 0:
x, y = que.popleft()
for dx, dy in d:
if 0 <= x + dx < W and 0 <= y + dy < H:
if meiz[x + dx][y + dy] == str(n):
pos_x = x + dx
pos_y = y + dy
# print(distance)
return distance[x][y] + 1
elif meiz[x + dx][y + dy] != "X" and distance[x + dx][y + dy] == float("inf"):
distance[x + dx][y + dy] = distance[x][y] + 1
# print(distance)
que.append((x + dx, y + dy))
if __name__ == '__main__':
H, W, N = list(map(int, input().split()))
meiz = [input() for i in range(H)]
pos_x = 0
for tmp in meiz:
if tmp.find("S") != -1:
pos_y = tmp.find("S")
break
pos_x += 1
ans = 0
for i in range(1, N + 1):
distance = [[float("inf") for i in range(W)] for i in range(H)]
distance[pos_x][pos_y] = 0
que = deque()
que.append((pos_x, pos_y))
# print(que)
ans += bfs(i)
print(ans) |
s432873299 | p00481 | u072053884 | 1517476399 | Python | Python3 | py | Runtime Error | 30 | 6008 | 2125 | import sys
file_input = sys.stdin
H, W, N = map(int, file_input.readline().split())
town_map = file_input.read()
start_index = town_map.index('S')
mouse = [start_index, 0, [start_index]]
# position, elapsed time, visited lot
from collections import deque
for goal in range(1, N + 1):
q = deque()
goal = str(goal)
while True:
cur_pos = mouse[0]
ew_pos = cur_pos % (W + 1)
if ew_pos < (W - 1):
next_pos = cur_pos + 1
lot = town_map[next_pos]
if lot != 'X' and next_pos not in mouse[2]:
if lot == goal:
mouse = [next_pos, mouse[1] + 1, [next_pos]]
break
else:
m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
q.append(m)
if ew_pos > 0:
next_pos = cur_pos - 1
lot = town_map[next_pos]
if lot != 'X' and next_pos not in mouse[2]:
if lot == goal:
mouse = [next_pos, mouse[1] + 1, [next_pos]]
break
else:
m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
q.append(m)
ns_pos = cur_pos // (W + 1)
if ns_pos < (H - 1):
next_pos = cur_pos + W + 1
lot = town_map[next_pos]
if lot != 'X' and next_pos not in mouse[2]:
if lot == goal:
mouse = [next_pos, mouse[1] + 1, [next_pos]]
break
else:
m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
q.append(m)
if ns_pos > 0:
next_pos = cur_pos - W - 1
lot = town_map[next_pos]
if lot != 'X' and next_pos not in mouse[2]:
if lot == goal:
mouse = [next_pos, mouse[1] + 1, [next_pos]]
break
else:
m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
q.append(m)
mouse = q.popleft()
print(mouse[1])
|
s609816599 | p00481 | u766926358 | 1525857000 | Python | Python3 | py | Runtime Error | 30 | 6012 | 1009 | from collections import deque
import sys
sys.setrecursionlimit(10000)
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def search(posi):
global flag
global us
global count
global queue
x, y, c = posi
if (field[y][x] == us[-1]):
flag = True
if (us[-1] == str(N)):
print(c)
exit()
us.pop()
queue.clear()
queue.append([x, y, c])
return
q_count = 0
for q in range(4):
nx = x + dx[q]
ny = y + dy[q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
queue.append([nx, ny, c+1])
q_count += 1
for i in range(q_count):
if (flag):
return
search(queue.popleft())
bf = False
for i in range(H):
for j in range(W):
if field[i][j] == 'S':
c = 0
queue.append([j, i, c])
bf = True
break
if (bf):
break
while 1:
flag = False
search(queue.popleft())
|
s788381983 | p00481 | u766926358 | 1525858381 | Python | Python3 | py | Runtime Error | 30 | 6012 | 899 | from collections import deque
import sys
sys.setrecursionlimit(10000)
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def search():
global flag
global us
global count
global queue
x, y, c = queue.popleft()
if (field[y][x] == us[-1]):
flag = True
if (us[-1] == str(N)):
print(c)
exit()
us.pop()
queue.clear()
queue.append([x, y, c])
return
for q in range(4):
nx = x + dx[q]
ny = y + dy[q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
queue.append([nx, ny, c+1])
bf = False
for i in range(H):
for j in range(W):
if field[i][j] == 'S':
c = 0
queue.append([j, i, c])
bf = True
break
if (bf):
break
while 1:
flag = False
search()
|
s678880456 | p00481 | u766926358 | 1525858590 | Python | Python3 | py | Runtime Error | 30 | 6012 | 890 | from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
us = [str(i) for i in range(N, 0, -1)]
queue = deque([])
dx = [1,0,-1,0]
dy = [0,1,0,-1]
def search():
global flag
global us
global count
global queue
while queue:
x, y, c = queue.popleft()
if (field[y][x] == us[-1]):
flag = True
if (us[-1] == str(N)):
print(c)
exit()
us.pop()
queue.clear()
queue.append([x, y, c])
return
for q in range(4):
nx = x + dx[q]
ny = y + dy[q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and ([nx,ny] not in queue) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
queue.append([nx, ny, c+1])
bf = False
for i in range(H):
for j in range(W):
if field[i][j] == 'S':
c = 0
queue.append([j, i, c])
bf = True
break
if (bf):
break
while 1:
flag = False
search()
|
s773936454 | p00481 | u766926358 | 1525864102 | Python | Python3 | py | Runtime Error | 1430 | 18712 | 806 | from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()
def search(start, goal):
Q.append(start)
while Q:
x, y, c = Q.popleft()
searched[y][x] = 1
if ([x, y, 0] == goal):
return c
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X') and (field[ny][nx] != 'S'):
Q.append([nx, ny, c+1])
for i in range(H):
for j in range(W):
if field[i][j] in 'S123456789':
node_posi[field[i][j]] = [j, i, 0]
s = 'S123456789'
cost = 0
for i in range(len(node_posi)-1):
Q.clear()
searched = [[0]*W for i in range(H)]
cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)
|
s645155206 | p00481 | u766926358 | 1525864299 | Python | Python3 | py | Runtime Error | 1570 | 18712 | 779 | from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()
def search(start, goal):
Q.append(start)
while Q:
x, y, c = Q.popleft()
searched[y][x] = 1
if ([x, y, 0] == goal):
return c
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
Q.append([nx, ny, c+1])
for i in range(H):
for j in range(W):
if field[i][j] in 'S123456789':
node_posi[field[i][j]] = [j, i, 0]
s = 'S123456789'
cost = 0
for i in range(len(node_posi)-1):
Q.clear()
searched = [[0]*W for i in range(H)]
cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)
|
s606138553 | p00481 | u352394527 | 1525936944 | Python | Python3 | py | Runtime Error | 60 | 7172 | 1095 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1,s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1,s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] == None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] == None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] == None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] == None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
return bfs(i)
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
mp[x][y] = 0
que = queue.Queue()
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s100498812 | p00481 | u352394527 | 1525937057 | Python | Python3 | py | Runtime Error | 140 | 10844 | 1138 | import sys
import queue
sys.setrecursionlimit(10000000)
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1,s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1,s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] == None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] == None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] == None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] == None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
return bfs(i)
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
mp[x][y] = 0
que = queue.Queue()
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s495741969 | p00481 | u352394527 | 1525937774 | Python | Python3 | py | Runtime Error | 140 | 6952 | 1129 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1,s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1,s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] == None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] == None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] == None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] == None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
mp[x][y] = 0
que = queue.Queue()
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s037771115 | p00481 | u352394527 | 1525941083 | Python | Python3 | py | Runtime Error | 0 | 0 | 1499 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
def bfs_(i):
for count in range(1000000):
sz = que.qsize()
for loop in range(sz):
(x,y) = que.get()
if ss[x][y] == "X" or not mp[x][y] is None:
continue
if (x,y) == factrys[i + 1]:
return count
mp[x][y] = count
que.put((x + 1, y))
que.put((x - 1, y))
que.put((x, y - 1))
que.put((x, y + 1))
ans = 0
for i in range(n):
mp = [[None] * (h + 2) for j in range(w + 2)]
x,y = factrys[i]
que = queue.Queue()]
mp[x][y] = 0
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s042477423 | p00481 | u352394527 | 1525941164 | Python | Python3 | py | Runtime Error | 50 | 6856 | 1498 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
def bfs_(i):
for count in range(1000000):
sz = que.qsize()
for loop in range(sz):
(x,y) = que.get()
if ss[x][y] == "X" or not mp[x][y] is None:
continue
if (x,y) == factrys[i + 1]:
return count
mp[x][y] = count
que.put((x + 1, y))
que.put((x - 1, y))
que.put((x, y - 1))
que.put((x, y + 1))
ans = 0
for i in range(n):
mp = [[None] * (h + 2) for j in range(w + 2)]
x,y = factrys[i]
que = queue.Queue()
mp[x][y] = 0
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s806718734 | p00481 | u352394527 | 1525941354 | Python | Python3 | py | Runtime Error | 140 | 6952 | 1498 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
def bfs_(i):
for count in range(1000000):
sz = que.qsize()
for loop in range(sz):
(x,y) = que.get()
if ss[x][y] == "X" or not mp[x][y] is None:
continue
if (x,y) == factrys[i + 1]:
return count
mp[x][y] = count
que.put((x + 1, y))
que.put((x - 1, y))
que.put((x, y - 1))
que.put((x, y + 1))
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
que = queue.Queue()
mp[x][y] = 0
que.put((x,y))
ans += (bfs(i))
print(ans)
|
s302069088 | p00481 | u352394527 | 1525942887 | Python | Python3 | py | Runtime Error | 0 | 0 | 1189 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
que = queue.Queue()
mp[x][y] = 0
que.put((x,y))
ret = bfs(i)
if ret is None:
ans += 0
else:
ans += (bfs(i))
print(ans)
|
s576183556 | p00481 | u352394527 | 1525943016 | Python | Python3 | py | Runtime Error | 160 | 6948 | 1186 | import queue
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
mp = []
que = queue.Queue()
def bfs(i):
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
ans = 0
for i in range(n):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
que = queue.Queue()
mp[x][y] = 0
que.put((x,y))
ret = bfs(i)
if ret is None:
ans += 0
else:
ans += (ret)
print(ans)
|
s499563284 | p00481 | u352394527 | 1525943502 | Python | Python3 | py | Runtime Error | 130 | 6900 | 1189 | import queue
from collections import deque
h,w,n = map(int,input().split())
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
for i in range(h):
s = "X" + input() + "X"
if "S" in s:
factrys[0] = (i + 1, s.index("S"))
for j in range(1,n + 1):
if str(j) in s:
factrys[j] = (i + 1, s.index(str(j)))
ss.append(s)
ss.append("X" * (w + 2))
def bfs(i):
mp = [[None] * (w + 2) for j in range(h + 2)]
x,y = factrys[i]
mp[x][y] = 0
que = queue.Queue()
que.put((x, y))
while True:
(x,y) = que.get()
# print(i,x,y)
# print(mp[x][y])
if (x,y) == factrys[i + 1]:
return mp[x][y]
count = mp[x][y]
if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
mp[x + 1][y] = count + 1
que.put((x + 1, y))
if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
mp[x - 1][y] = count + 1
que.put((x - 1, y))
if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
mp[x][y + 1] = count + 1
que.put((x, y + 1))
if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
mp[x][y - 1] = count + 1
que.put((x, y - 1))
ans = 0
for i in range(n):
ret = bfs(i)
if ret is None:
ans += 0
else:
ans += (ret)
print(ans)
|
s896918749 | p00481 | u766926358 | 1525968760 | Python | Python3 | py | Runtime Error | 0 | 0 | 1297 | h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
y += [1,-1,0,0][i]
x += [0,0,1,-1][i]
if y >= 0 and y < h and x >= 0 and x < w:
if bfs_map[y][x] == -1 and stage[y][x] != "X":
bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
data_y.append(y)
data_x.append(x)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
y -= move_y[i]
x -= move_x[i]
print(sum)
|
s961259576 | p00481 | u766926358 | 1525968806 | Python | Python3 | py | Runtime Error | 0 | 0 | 1297 | h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
y += [1,-1,0,0][i]
x += [0,0,1,-1][i]
if y >= 0 and y < h and x >= 0 and x < w:
if bfs_map[y][x] == -1 and stage[y][x] != "X":
bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
data_y.append(y)
data_x.append(x)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
y -= move_y[i]
x -= move_x[i]
print(sum)
|
s251392599 | p00481 | u766926358 | 1525969022 | Python | Python3 | py | Runtime Error | 0 | 0 | 1313 | h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
move_x = [0,0,1,-1]
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
y += [1,-1,0,0][i]
x += move_x[i]
if y >= 0 and y < h and x >= 0 and x < w:
if bfs_map[y][x] == -1 and stage[y][x] != "X":
bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
data_y.append(y)
data_x.append(x)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
y -= move_y[i]
x -= move_x[i]
print(sum)
|
s393386398 | p00481 | u766926358 | 1525970567 | Python | Python3 | py | Runtime Error | 1590 | 18712 | 779 | from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = deque()
def search(start, goal):
Q.append(start)
while Q:
x, y, c = Q.popleft()
searched[y][x] = 1
if ([x, y, 0] == goal):
return c
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
Q.append([nx, ny, c+1])
for i in range(H):
for j in range(W):
if field[i][j] in 'S123456789':
node_posi[field[i][j]] = [j, i, 0]
s = 'S123456789'
cost = 0
for i in range(len(node_posi)-1):
Q.clear()
searched = [[0]*W for i in range(H)]
cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)
|
s911736805 | p00481 | u766926358 | 1525970800 | Python | Python3 | py | Runtime Error | 5360 | 18360 | 740 | H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = []
def search(start, goal):
Q.append(start)
while Q:
x, y, c = Q.pop(0)
searched[y][x] = 1
if ([x, y, 0] == goal):
return c
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
Q.append([nx, ny, c+1])
for i in range(H):
for j in range(W):
if field[i][j] in 'S123456789':
node_posi[field[i][j]] = [j, i, 0]
s = 'S123456789'
cost = 0
for i in range(len(node_posi)-1):
Q.clear()
searched = [[0]*W for i in range(H)]
cost += search(node_posi[s[i]], node_posi[s[i+1]])
print(cost)
|
s868628418 | p00481 | u766926358 | 1525971119 | Python | Python3 | py | Runtime Error | 5990 | 18364 | 719 | H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
node_posi = {}
Q = []
for i in range(H):
for j in range(W):
if field[i][j] in 'S123456789':
node_posi[field[i][j]] = [j, i, 0]
s = 'S123456789'
cost = 0
for i in range(len(node_posi)-1):
Q.clear()
searched = [[0]*W for i in range(H)]
start, goal = node_posi[s[i]], node_posi[s[i+1]]
Q.append(start)
while Q:
x, y, c = Q.pop(0)
searched[y][x] = 1
if ([x, y, 0] == goal):
cost += c
break
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if (nx >= 0 and nx <= W-1) and (ny >= 0 and ny <= H-1) and (searched[ny][nx] == 0) and (field[ny][nx] != 'X'):
Q.append([nx, ny, c+1])
print(cost)
|
s212897917 | p00481 | u531592024 | 1529421214 | Python | Python3 | py | Runtime Error | 30 | 6344 | 1300 | import copy
(h, w, n) = list(map(int, input().split()))
dis = [[] for i in range(n + 1)]
def setArr(ph):
global dis
res = [False for i in range(w + 2)]
for i, x in enumerate(list(input())):
if x == "X":
continue
res[i + 1] = True
if x == ".":
continue
if x == "S":
dis[0] = [ph, i + 1]
continue
dis[int(x)] = [ph, i + 1]
return res
arr = [[] for i in range(h + 2)]
arr[0] = [False for i in range(h + 2)]
arr[-1] = [False for i in range(h + 2)]
for i in range(1, h + 1):
arr[i] = setArr(i)
res = 0
move = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def ze(start, goal):
arra = copy.deepcopy(arr)
global res
queue = [start, [-1, -1]]
ap = queue.append
zu = False
for q in queue:
if q[0] < 0:
res += 1
ap([-1, -1])
continue
if not arra[q[0]][q[1]]:
continue
for m in move:
de = [q[0] + m[0], q[1] + m[1]]
if de == goal:
res += 1
zu = True
break
if arra[de[0]][de[1]]:
ap(de)
if zu:
break
arra[q[0]][q[1]] = False
for i in range(n):
ze(dis[i], dis[i + 1])
print(res)
|
s737171396 | p00481 | u011621222 | 1530087212 | Python | Python3 | py | Runtime Error | 190 | 7176 | 1095 | from queue import Queue
from collections import defaultdict
try:
d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
H, W, N = map(int, input().split())
G = []
sx = sy = 0
for i in range(H):
G.append(input())
for j, c in enumerate(G[i]):
if c == 'S':
sx, sy = i, j
Q = Queue()
Q.put((sx, sy, 0))
visited = defaultdict(lambda: defaultdict(lambda: False))
visited[sx][sy] = True
target = 1
while not Q.empty():
x, y, step = Q.get()
if G[x][y] == str(target):
visited.clear()
visited[x][y] = True
while not Q.empty():
Q.get()
target += 1
if target == N + 1:
print(step)
break
for dx, dy in d:
nx, ny = x + dx, y + dy
if 0 <= nx < H and 0 <= ny < W and G[nx][ny] != 'X':
if not visited[nx][ny]:
visited[nx][ny] = True
Q.put((nx, ny, step + 1))
except BaseException as e:
print(e)
|
s727593766 | p00481 | u011621222 | 1530088257 | Python | Python3 | py | Runtime Error | 200 | 7164 | 1212 | from queue import Queue
from collections import defaultdict
try:
d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
H, W, N = map(int, input().split())
G = []
sx = sy = 0
for i in range(H):
G.append(input())
for j, c in enumerate(G[i]):
if c == 'S':
sx, sy = i, j
Q = Queue()
Q.put((sx, sy, 0))
visited = defaultdict(lambda: defaultdict(lambda: 0))
# visited = [[False for _ in range(W)] for _ in range(H)]
visited[sx][sy] = True
target = 1
while not Q.empty():
x, y, step = Q.get()
if G[x][y] == str(target):
# visited = [[False for _ in range(W)] for _ in range(H)]
# visited.clear()
target += 1
visited[x][y] = target
while not Q.empty():
Q.get()
if target == N + 1:
print(step)
break
for dx, dy in d:
nx, ny = x + dx, y + dy
if 0 <= nx < H and 0 <= ny < W and G[nx][ny] != 'X':
if visited[nx][ny] != target:
visited[nx][ny] = target
Q.put((nx, ny, step + 1))
except BaseException as e:
print(e)
|
s393832534 | p00481 | u260980560 | 1382805533 | Python | Python | py | Runtime Error | 0 | 0 | 996 | from queue import Queue
INF = 20000000
q = Queue()
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
smp = [[INF for i in range(1002)] for j in range(1002)]
mp = [['X' for i in range(1002)] for j in range(1002)]
ipt = input().split('\n')
H, W, n = map(int,ipt[0].split())
for i in range(1, H+1):
for j in range(1, W+1):
mp[i][j] = ipt[i][j-1];
x = y = 0
for i in range(1, H+1):
for j in range(1, W+1):
if mp[i][j]=='S':
x = j; y = i;
c = 0; ans = 0;
q.put([x,y]); smp[y][x] = 0;
while c<n:
p = q.get()
x = p[0]; y = p[1];
if mp[y][x]==chr(ord('1')+c):
ans += smp[y][x]; c+=1;
if c==n: break;
while q.empty()==False: q.get()
for i in range(1, H+1):
for j in range(1, W+1):
smp[i][j] = INF
smp[y][x] = 0
for i in range(4):
if mp[y+dy[i]][x+dx[i]]!='X' and smp[y+dy[i]][x+dx[i]]==INF:
q.put([x+dx[i], y+dy[i]]);
smp[y+dy[i]][x+dx[i]] = smp[y][x]+1;
print(ans) |
s568703582 | p00481 | u260980560 | 1382805714 | Python | Python | py | Runtime Error | 0 | 0 | 1000 | from Queue import Queue
INF = 20000000
q = Queue()
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
smp = [[INF for i in range(1002)] for j in range(1002)]
mp = [['X' for i in range(1002)] for j in range(1002)]
ipt = raw_input().split('\n')
H, W, n = map(int,ipt[0].split())
for i in range(1, H+1):
for j in range(1, W+1):
mp[i][j] = ipt[i][j-1];
x = y = 0
for i in range(1, H+1):
for j in range(1, W+1):
if mp[i][j]=='S':
x = j; y = i;
c = 0; ans = 0;
q.put([x,y]); smp[y][x] = 0;
while c<n:
p = q.get()
x = p[0]; y = p[1];
if mp[y][x]==chr(ord('1')+c):
ans += smp[y][x]; c+=1;
if c==n: break;
while q.empty()==False: q.get()
for i in range(1, H+1):
for j in range(1, W+1):
smp[i][j] = INF
smp[y][x] = 0
for i in range(4):
if mp[y+dy[i]][x+dx[i]]!='X' and smp[y+dy[i]][x+dx[i]]==INF:
q.put([x+dx[i], y+dy[i]]);
smp[y+dy[i]][x+dx[i]] = smp[y][x]+1;
print(ans) |
s347495805 | p00482 | u150984829 | 1526567784 | Python | Python3 | py | Runtime Error | 60 | 5600 | 623 | M, N = map(int, input().split())
cloth = sum([[*input()]for _ in[0] * M], [])
ans = 3**cloth.count('?')
def have_JOI(cloth):
for i, p in enumerate(cloth[:-N]):
if p == 'J' and cloth[i + 1] == 'O' and cloth[i + N] == 'I':
return True
return False
def search(cloth):
if have_JOI(cloth):
return
if '?' not in cloth:
global ans
ans -= 1
else:
index = cloth.index('?')
cloth[index] = 'J'
search(cloth[:])
cloth[index] = 'O'
search(cloth[:])
cloth[index] = 'I'
search(cloth[:])
search(cloth)
print(ans)
|
s890935664 | p00482 | u150984829 | 1526568201 | Python | Python3 | py | Runtime Error | 70 | 5596 | 646 | M, N = map(int, input().split())
cloth = sum([[*input()]for _ in[0] * M], [])
ans = 3 ** cloth.count('?')
def have_JOI(cloth):
for i, p in enumerate(cloth[:-N]):
if i % N != N-1 and p == 'J' and cloth[i + 1] == 'O' and cloth[i + N] == 'I':
return True
return False
def search(cloth):
if have_JOI(cloth):
return
if '?' not in cloth:
global ans
ans -= 1
else:
index = cloth.index('?')
cloth[index] = 'J'
search(cloth[:])
cloth[index] = 'O'
search(cloth[:])
cloth[index] = 'I'
search(cloth[:])
search(cloth)
print(ans % 10 ** 5)
|
s146815062 | p00482 | u150984829 | 1526568372 | Python | Python3 | py | Runtime Error | 70 | 5596 | 743 | M, N = map(int, input().split())
cloth = sum([[*input()]for _ in[0] * M], [])
ans = 3 ** cloth.count('?')
def have_JOI(cloth):
for i, p in enumerate(cloth[:-N]):
if i % N != N-1 and p == 'J' and cloth[i + 1] == 'O' and cloth[i + N] == 'I':
return True
return False
def search(cloth):
if '?' not in cloth:
global ans
ans -= 1
else:
index = cloth.index('?')
cloth[index] = 'J'
if not have_JOI(cloth):
search(cloth[:])
cloth[index] = 'O'
if not have_JOI(cloth):
search(cloth[:])
cloth[index] = 'I'
if not have_JOI(cloth):
search(cloth[:])
if not have_JOI(cloth):
search(cloth)
print(ans % 10 ** 5)
|
s322382938 | p00483 | u633068244 | 1416591208 | Python | Python | py | Runtime Error | 19930 | 6168 | 325 | M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
for roop in range(K):
a,b,c,d = map(int,raw_input().split())
j = o = i = 0
for m in range(a-1,c):
j += A[m][b-1:d].count("J")
o += A[m][b-1:d].count("O")
i += A[m][b-1:d].count("I")
print j,o,i |
s892196579 | p00483 | u633068244 | 1416592098 | Python | Python | py | Runtime Error | 19930 | 35572 | 650 | M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
J,O,I = [[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)]
for m in range(M):
j = o = i = 0
for n in range(N):
if A[m][n] == "J": j += 1
elif A[m][n] == "O": o += 1
elif A[m][n] == "I": i += 1
J[m][n+1],O[m][n+1],I[m][n+1] = j,o,i
for roop in range(K):
a,b,c,d = map(int,raw_input().split())
j = sum(J[m][d] - J[m][b-1] for m in range(a-1,c))
o = sum(O[m][d] - O[m][b-1] for m in range(a-1,c))
i = sum(I[m][d] - I[m][b-1] for m in range(a-1,c))
print j,o,i |
s410031603 | p00483 | u633068244 | 1416592263 | Python | Python | py | Runtime Error | 19920 | 35576 | 696 | M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
J,O,I = [[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)]
for m in range(M):
j = o = i = 0
for n in range(N):
if A[m][n] == "J": j += 1
elif A[m][n] == "O": o += 1
elif A[m][n] == "I": i += 1
J[m][n+1],O[m][n+1],I[m][n+1] = j,o,i
for roop in range(K):
a,b,c,d = map(int,raw_input().split())
try:
j = sum(J[m][d] - J[m][b-1] for m in range(a-1,c))
o = sum(O[m][d] - O[m][b-1] for m in range(a-1,c))
i = sum(I[m][d] - I[m][b-1] for m in range(a-1,c))
except:
pass
print j,o,i |
s253638563 | p00483 | u186082958 | 1430035303 | Python | Python3 | py | Runtime Error | 0 | 0 | 541 | w=input()
height=int(w[0])
wide=int(w[2])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1; west=int(y[2])-1
south=int(y[4])-1; east=int(y[6])-1
jungle=0;ocean=0;ice=0;
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k]=="J":
jungle+=1
elif a[j][k]=="O":
ocean+=1
elif a[j][k]=="I":
ice+=1
print(jungle,ocean,ice) |
s725957125 | p00483 | u186082958 | 1430035398 | Python | Python3 | py | Runtime Error | 0 | 0 | 554 | w=input()
height=int(w[0])
wide=int(w[2])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1
west=int(y[2])-1
south=int(y[4])-1
east=int(y[6])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k]=="J":
jungle+=1
elif a[j][k]=="O":
ocean+=1
elif a[j][k]=="I":
ice+=1
print(jungle,ocean,ice) |
s819415994 | p00483 | u186082958 | 1430035513 | Python | Python3 | py | Runtime Error | 0 | 0 | 554 | w=input()
height=int(w[0])
wide=int(w[2])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1
west=int(y[2])-1
south=int(y[4])-1
east=int(y[6])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k]=="J":
jungle+=1
elif a[j][k]=="O":
ocean+=1
elif a[j][k]=="I":
ice+=1
print(jungle,ocean,ice) |
s822497270 | p00483 | u186082958 | 1430036103 | Python | Python3 | py | Runtime Error | 0 | 0 | 642 | w=input()
height=int(w[0])
wide=int(w[2])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1
west=int(y[2])-1
south=int(y[4])-1
east=int(y[6])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k]=="J":
jungle+=1
elif a[j][k]=="O":
ocean+=1
elif a[j][k]=="I":
ice+=1
#print(jungle,ocean,ice)
#print(jungle,ocean,ice)
#print(jungle,ocean,ice)
#print(jungle,ocean,ice) |
s800889078 | p00483 | u186082958 | 1430036213 | Python | Python3 | py | Runtime Error | 0 | 0 | 624 | w=input()
height=int(w[0])
wide=int(w[2])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1
west=int(y[2])-1
south=int(y[4])-1
east=int(y[6])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k] == "J":
#jungle+=1
jungle =jungle+1
elif a[j][k] == "O":
#ocean+=1
ocean=ocean+1
elif a[j][k] == "I":
#ice+=1
ice=ice+1 |
s866969345 | p00483 | u186082958 | 1430037107 | Python | Python3 | py | Runtime Error | 0 | 0 | 554 | line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
y=input()
north=int(y[0])-1
west=int(y[2])-1
south=int(y[4])-1
east=int(y[6])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k] == "J":
jungle+=1
elif a[j][k] == "O":
ocean+=1
elif a[j][k] == "I":
ice+=1 |
s959047668 | p00483 | u186082958 | 1430037227 | Python | Python3 | py | Runtime Error | 0 | 0 | 587 | line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])
pattern_num=int(input())
a=[]
for i in range(pattern_num):
a.append(input())
for i in range(pattern_num):
aline =input()
aw=aline.split(" ")
north=int(aw[0])-1
west=int(aw[1])-1
south=int(aw[2])-1
east=int(aw[3])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k] == "J":
jungle+=1
elif a[j][k] == "O":
ocean+=1
elif a[j][k] == "I":
ice+=1 |
s173196310 | p00483 | u186082958 | 1430037462 | Python | Python3 | py | Runtime Error | 19920 | 6888 | 610 | line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])
pattern_num=int(input())
a=[]
for i in range(height):
a.append(input())
for i in range(pattern_num):
aline =input()
aw=aline.split(" ")
north=int(aw[0])-1
west=int(aw[1])-1
south=int(aw[2])-1
east=int(aw[3])-1
jungle=0
ocean=0
ice=0
for j in range(north,south+1):
for k in range(west,east+1):
if a[j][k] == "J":
jungle+=1
elif a[j][k] == "O":
ocean+=1
elif a[j][k] == "I":
ice+=1
print(jungle,ocean,ice) |
s612748927 | p00483 | u228488524 | 1508219352 | Python | Python3 | py | Runtime Error | 0 | 0 | 939 | # coding: utf-8
M, N = list(map(int, input().split()))
q = int(input())
Map = []
for i in range(M):
Map.append(input)
def get_slide_sum(Map, kw):
l = []
for r in range(M):
tmp = []
for c in range(N):
ll = 0 if c == 0 else tmp[c-1]
ul = 0 if r == 0 or c == 0 else l[r-1][c-1]
ur = 0 if r == 0 else l[r-1][c]
count = ll + ur - ul
if Map[r][c] == kw:
count+=1
tmp.append(count)
l.append(tmp)
return l
J = get_slide_sum(Map, 'J')
O = get_slide_sum(Map, 'O')
I = get_slide_sum(Map, 'I')
def get_sum(inp,L):
a, b, c, d = list(map(int, inp.split()))
ll = 0 if a == 1 else L[a-2][d-1]
ul = 0 if a == 1 or b == 1 else L[a-2][b-2]
ur = 0 if b == 1 else L[c-1][b-2]
return L[c-1][d-1] - ll - ur + ul
for i in range(q):
inp = input()
print(get_sum(inp, J), get_sum(inp, O), get_sum(inp, I)) |
s901582256 | p00483 | u228488524 | 1508221591 | Python | Python3 | py | Runtime Error | 0 | 0 | 958 | # coding: utf-8
# Here your code !
M, N = list(map(int, input().split()))
q = int(input())
Map = []
for i in range(M):
Map.append(input)
def get_slide_sum(Map, kw):
l = []
for r in range(M):
tmp = []
for c in range(N):
ll = 0 if c == 0 else tmp[c-1]
ul = 0 if r == 0 or c == 0 else l[r-1][c-1]
ur = 0 if r == 0 else l[r-1][c]
count = ll + ur - ul
if Map[r][c] == kw:
count+=1
tmp.append(count)
l.append(tmp)
return l
J = get_slide_sum(Map, 'J')
O = get_slide_sum(Map, 'O')
I = get_slide_sum(Map, 'I')
def get_sum(inp,L):
a, b, c, d = list(map(int, inp.split()))
ll = 0 if a == 1 else L[a-2][d-1]
ul = 0 if a == 1 or b == 1 else L[a-2][b-2]
ur = 0 if b == 1 else L[c-1][b-2]
return L[c-1][d-1] - ll - ur + ul
for i in range(q):
inp = input()
print(get_sum(inp, J), get_sum(inp, O), get_sum(inp, I)) |
s972016185 | p00483 | u829501316 | 1518280028 | Python | Python3 | py | Runtime Error | 4700 | 11828 | 1096 | M, N = map(int, input().split())
K = int(input())
P = ['_' * (N + 1) for _ in range(M + 1)]
for mi in range(1, M + 1):
P[mi] = '_' + input() # for p in P:
# print('p', p)
J = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
O = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
I = [[0 for _ in range(N + 1)] for _ in range(M + 1)]
for i in range(1, M + 1):
ji, oi, ii = 0, 0, 0
for j in range(1, N + 1):
ji += P[i][j] == 'J'
oi += P[i][j] == 'O'
ii += P[i][j] == 'I'
J[i][j] = J[i - 1][j] + ji
O[i][j] = O[i - 1][j] + oi
I[i][j] = I[i - 1][j] + ii
# for j in J:
# print('j', j)
# for o in O:
# print('o', o)
# for i in I:
# print('i', i)
def cum(mat, a, b, c, d):
a -= 1
b -= 1
return mat[a][b] + mat[c][d] - mat[a][d] - mat[c][b]
for k in range(K):
a, b, c, d = map(int, input().split())
j = cum(J, a, b, c, d)
o = cum(O, a, b, c, d)
i = cum(I, a, b, c, d)
print(j, o, i)
|
s419402601 | p00483 | u847467233 | 1530335684 | Python | Python3 | py | Runtime Error | 430 | 32144 | 530 | # AOJ 0560: Planetary Exploration
# Python3 2018.6.30 bal4u
tr = { 'J':0, 'O':1, 'I':2 }
s = [[[0 for c in range(1002)] for r in range(1002)] for k in range(3)]
m, n = map(int, input().split())
k = int(input())
for r in range(1, m+1):
buf = input().strip()
for c in range(1, n+1):
for i in range(3): s[i][r][c] = s[i][r-1][c] + (i == tr[buf[c-1]])
for i in range(k):
r1, c1, r2, c2 = map(int, input().split())
ans = [0]*3
for j in range(3):
for c in range(c1, c2+1): ans[j] += s[j][r2][c] - s[j][r1-1][c];
print(*ans)
|
s590258743 | p00483 | u452764412 | 1377132525 | Python | Python | py | Runtime Error | 20000 | 13724 | 381 | M, N = map(int, raw_input().split())
K = input()
d = {}
for i in range(M):
a = raw_input()
for j in range(N):
d[i+1, j+1] = a[j]
for _ in range(K):
b = map(int, raw_input().split())
D = {}
D['J'] = D['O'] = D['I'] = 0
for i in range(b[0], b[2]+1):
for j in range(b[1], b[3]+1):
D[d[i, j]] += 1
print D['J'], D['O'], D['I'] |
s341275728 | p00483 | u872035937 | 1383261874 | Python | Python | py | Runtime Error | 39860 | 7780 | 574 | sn_km, ew_km = map(int, raw_input().split())
seek_area = int(raw_input())
zone = sn_km*[ew_km*[0]]
def cnt_zone(idx, first, last, str_for_count):
return int(str(zone[idx])[first:last].count(str_for_count))
for i in range(sn_km):
zone[i] = raw_input()
for i in range(seek_area):
J = O = I = 0
a, b, c, d = map((lambda x, y: x+y), map(int, raw_input().split()), 4*[-1])
for y in range(c - a + 1):
J += cnt_zone(a+y, b, d+1, 'J')
O += cnt_zone(a+y, b, d+1, 'O')
I += cnt_zone(a+y, b, d+1, 'I')
print "%d %d %d" % (J, O, I) |
s285444654 | p00483 | u872035937 | 1383262016 | Python | Python | py | Runtime Error | 39860 | 7780 | 574 | sn_km, ew_km = map(int, raw_input().split())
seek_area = int(raw_input())
zone = sn_km*[ew_km*[0]]
def cnt_zone(idx, first, last, str_for_count):
return int(str(zone[idx])[first:last].count(str_for_count))
for i in range(sn_km):
zone[i] = raw_input()
for i in range(seek_area):
J = O = I = 0
a, b, c, d = map((lambda x, y: x+y), map(int, raw_input().split()), 4*[-1])
for y in range(c - a + 1):
J += cnt_zone(a+y, b, d+1, 'J')
O += cnt_zone(a+y, b, d+1, 'O')
I += cnt_zone(a+y, b, d+1, 'I')
print "%d %d %d" % (J, O, I) |
s770683576 | p00484 | u797673668 | 1454129542 | Python | Python3 | py | Runtime Error | 40000 | 8072 | 677 | from collections import deque
from itertools import chain, combinations
n, k = map(int, input().split())
books = [[] for _ in range(10)]
while n:
c, g = map(int, input().split())
books[g - 1].append(c)
n -= 1
for q in books:
q.sort(reverse=True)
done = set()
max_cost = 0
for book_combi in combinations(chain.from_iterable([[i] * min(k, len(q)) for i, q in enumerate(books) if q]), k):
if book_combi in done:
continue
done.add(book_combi)
cost = 0
counts = [0] * 10
for i in book_combi:
cost += books[i][counts[i]] + counts[i] * 2
counts[i] += 1
if cost > max_cost:
max_cost = cost
print(max_cost) |
s959809106 | p00484 | u894114233 | 1475047026 | Python | Python | py | Runtime Error | 0 | 0 | 536 | n,k=map(int,raw_input().split())
g=[[] for _ in xrange(10)]
for i in xrange(n):
v,j=map(int,raw_input().split())
g[j-1].append(v)
for i in xrange(10):
g[i].sort()
g[i].reverse()
books=[[0]*(k+1) for _ in xrange(10)]
for i in xrange(10):
for j in xrange(1,len(g[i])+1):
books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)
dp=[[0]*(k+1) for _ in xrange(11)]
for i in xrange(10):
for j in xrange(1,k+1):
for l in xrange(j+1):
dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])
print(dp[10][k]) |
s185922554 | p00484 | u197615397 | 1509891721 | Python | Python3 | py | Runtime Error | 0 | 0 | 965 | def solve():
N, K = map(int, input().split())
a = [[] for _ in [0]*10]
l = []
for c, g in (tuple(map(int, input().split())) for _ in [0]*N):
a[g-1].append(c)
for i, prices in enumerate(a):
ln = len(prices)
if ln == 0:
continue
dp = [[-1]*(K+1) for _ in [0]*(ln+1)]
dp[0][0] = 0
for j, price in enumerate(prices):
for k, prev in enumerate(dp[j][:j+1]):
if prev > dp[j+1][k]:
dp[j+1][k] = prev
if prev+price+k*2 > dp[j+1][k+1]:
dp[j+1][k+1] = prev+price+k*2
l.append(dp[-1][1:ln+1])
dp = [0]*(K+1)
for prices in l:
for i, price in enumerate(prices, start=1):
for j, cur, prev in zip(range(K, -1, -1), dp[::-1], dp[K-i::-1]):
if prev+price > cur:
dp[j] = prev+price
print(max(dp))
if __name__ == "__main__":
solve() |
s513355749 | p00484 | u266872031 | 1521364134 | Python | Python | py | Runtime Error | 0 | 0 | 575 | [N,K]=map(int, raw_input().split())
C=[[] for i in range(11)]
D=[[0 for j in range(K+1)] for i in range(11)]
M=[0 for i in range(11)]
for i in range(N):
c,g=map(int, raw_input().split())
C[g].append(c)
M[g]+=1
for i in range(1,11):
y=sorted(C[i], reverse=True)
for j in range(len(y)):
n = j+1
D[i][n] =y[j] + D[i][n-1] + 2*(n-1)
DP=[[0 for i in range(K+1)] for j in range(11)]
for g in range(1,11):
for k in range(K+1):
DP[g][k] = max( [ DP[g-1][k-x] + D[g][x] for x in range(min( k+1, M[g]+1 ) ) ] + [0] )
print DP[10][K]
|
s977990382 | p00486 | u013637113 | 1526639254 | Python | Python3 | py | Runtime Error | 0 | 0 | 1218 | import numpy
def cal_time(W, H, x, y):
return 2*(abs(W-x) + abs(H-y))
geo = input("Input W H: ").split()
W = int(geo[0])
H = int(geo[1])
if W<1 or W>1000000000:
print("wrong W")
exit()
if H<1 or H>1000000000:
print("wrong H")
exit()
N = int(input("N: "))
if N<1 or N>100000:
print("wrong N")
exit()
total_time = 0
''' Input houses and calculate time'''
houses = []
x_list = []
y_list = []
for i in range(N):
co = input("x y: ").split()
x = int(co[0])
if x<1 or x>W:
print("wrong x")
exit()
x_list.append(x)
y = int(co[1])
if y<1 or y>H:
print("wrong y")
exit()
y_list.append(y)
if (x,y) in houses:
print("Coordinate exists")
exit()
houses.append((x,y))
#total_time += cal_time(W, H, x, y)
avg_x = numpy.mean(x_list)
avg_y = numpy.mean(y_list)
# round
target_x = int(round(avg_x))
target_y = int(round(avg_y))
#print(target_x)
#print(target_y)
all_time = []
for co in houses:
#print(co)
time = cal_time(target_x, target_y, co[0], co[1])
#print(time)
all_time.append(time)
total_time += time
total_time -= int(max(all_time)/2)
print(total_time)
print(target_x, target_y)
|
s036486093 | p00486 | u013637113 | 1526639370 | Python | Python3 | py | Runtime Error | 0 | 0 | 1200 | import numpy
def cal_time(W, H, x, y):
return 2*(abs(W-x) + abs(H-y))
geo = input().split()
W = int(geo[0])
H = int(geo[1])
if W<1 or W>1000000000:
print("wrong W")
exit()
if H<1 or H>1000000000:
print("wrong H")
exit()
N = int(input())
if N<1 or N>100000:
print("wrong N")
exit()
total_time = 0
''' Input houses and calculate time'''
houses = []
x_list = []
y_list = []
for i in range(N):
co = input("x y: ").split()
x = int(co[0])
if x<1 or x>W:
print("wrong x")
exit()
x_list.append(x)
y = int(co[1])
if y<1 or y>H:
print("wrong y")
exit()
y_list.append(y)
if (x,y) in houses:
print("Coordinate exists")
exit()
houses.append((x,y))
#total_time += cal_time(W, H, x, y)
avg_x = numpy.mean(x_list)
avg_y = numpy.mean(y_list)
# round
target_x = int(round(avg_x))
target_y = int(round(avg_y))
#print(target_x)
#print(target_y)
all_time = []
for co in houses:
#print(co)
time = cal_time(target_x, target_y, co[0], co[1])
#print(time)
all_time.append(time)
total_time += time
total_time -= int(max(all_time)/2)
print(total_time)
print(target_x, target_y)
|
s815565455 | p00486 | u013637113 | 1526639444 | Python | Python3 | py | Runtime Error | 0 | 0 | 1200 | import numpy
def cal_time(W, H, x, y):
return 2*(abs(W-x) + abs(H-y))
geo = input().split()
W = int(geo[0])
H = int(geo[1])
if W<1 or W>1000000000:
print("wrong W")
exit()
if H<1 or H>1000000000:
print("wrong H")
exit()
N = int(input())
if N<1 or N>100000:
print("wrong N")
exit()
total_time = 0
''' Input houses and calculate time'''
houses = []
x_list = []
y_list = []
for i in range(N):
co = input("x y: ").split()
x = int(co[0])
if x<1 or x>W:
print("wrong x")
exit()
x_list.append(x)
y = int(co[1])
if y<1 or y>H:
print("wrong y")
exit()
y_list.append(y)
if (x,y) in houses:
print("Coordinate exists")
exit()
houses.append((x,y))
#total_time += cal_time(W, H, x, y)
avg_x = numpy.mean(x_list)
avg_y = numpy.mean(y_list)
# round
target_x = int(round(avg_x))
target_y = int(round(avg_y))
#print(target_x)
#print(target_y)
all_time = []
for co in houses:
#print(co)
time = cal_time(target_x, target_y, co[0], co[1])
#print(time)
all_time.append(time)
total_time += time
total_time -= int(max(all_time)/2)
print(total_time)
print(target_x, target_y)
|
s147410937 | p00486 | u013637113 | 1526639501 | Python | Python3 | py | Runtime Error | 0 | 0 | 1193 | import numpy
def cal_time(W, H, x, y):
return 2*(abs(W-x) + abs(H-y))
geo = input().split()
W = int(geo[0])
H = int(geo[1])
if W<1 or W>1000000000:
print("wrong W")
exit()
if H<1 or H>1000000000:
print("wrong H")
exit()
N = int(input())
if N<1 or N>100000:
print("wrong N")
exit()
total_time = 0
''' Input houses and calculate time'''
houses = []
x_list = []
y_list = []
for i in range(N):
co = input().split()
x = int(co[0])
if x<1 or x>W:
print("wrong x")
exit()
x_list.append(x)
y = int(co[1])
if y<1 or y>H:
print("wrong y")
exit()
y_list.append(y)
if (x,y) in houses:
print("Coordinate exists")
exit()
houses.append((x,y))
#total_time += cal_time(W, H, x, y)
avg_x = numpy.mean(x_list)
avg_y = numpy.mean(y_list)
# round
target_x = int(round(avg_x))
target_y = int(round(avg_y))
#print(target_x)
#print(target_y)
all_time = []
for co in houses:
#print(co)
time = cal_time(target_x, target_y, co[0], co[1])
#print(time)
all_time.append(time)
total_time += time
total_time -= int(max(all_time)/2)
print(total_time)
print(target_x, target_y)
|
s034538312 | p00488 | u633068244 | 1396361829 | Python | Python | py | Runtime Error | 0 | 0 | 70 | print min([input() for i in [1,1,1])+min([input() for i in [1,1]]) -50 |
s720173128 | p00489 | u228488524 | 1506409497 | Python | Python3 | py | Runtime Error | 0 | 0 | 622 | t = int(input())
point_l = [0 for i in range(t)]
for i in range((t*(t-1))/2):
input_line = input()
tmp = [int(x) for x in input_line.rstrip().split(' ')]
if tmp[2] > tmp[3]:
point_l[tmp[0]-1] += 3
elif tmp[3] > tmp[2]:
point_l[tmp[1]-1] += 3
elif tmp[2] == tmp[3]:
point_l[tmp[0]-1] += 1
point_l[tmp[1]-1] += 1
rank_l = [0 for i in range(t)]
rank = 1
for l in sorted(point_l, reverse=True):
dup = point_l.count(l)
for j in range(dup):
rank_l[point_l.index(l)] += rank
point_l[point_l.index(l)] = -1
rank += dup
for l in rank_l:
print(l) |
s026676690 | p00490 | u749355444 | 1480492977 | Python | Python | py | Runtime Error | 0 | 0 | 213 | A,B = map(int, raw_input().split())
C = input()
D = [input() for _ in range(N)]
D = sorted(D)[::-1]
price = A
cal = C
ans = cal/price
for d in D:
cal += d
price += B
ans = max(ans, cal/price)
print ans |
s407584926 | p00490 | u749355444 | 1480493017 | Python | Python | py | Runtime Error | 0 | 0 | 213 | A,B = map(int, raw_input().split())
C = input()
D = [input() for _ in range(N)]
D = sorted(D)[::-1]
price = A
cal = C
ans = cal/price
for d in D:
cal += d
price += B
ans = max(ans, cal/price)
print ans |
s755047972 | p00490 | u749355444 | 1480493024 | Python | Python3 | py | Runtime Error | 0 | 0 | 213 | A,B = map(int, raw_input().split())
C = input()
D = [input() for _ in range(N)]
D = sorted(D)[::-1]
price = A
cal = C
ans = cal/price
for d in D:
cal += d
price += B
ans = max(ans, cal/price)
print ans |
s559742873 | p00490 | u749355444 | 1480493033 | Python | Python | py | Runtime Error | 0 | 0 | 213 | A,B = map(int, raw_input().split())
C = input()
D = [input() for _ in range(N)]
D = sorted(D)[::-1]
price = A
cal = C
ans = cal/price
for d in D:
cal += d
price += B
ans = max(ans, cal/price)
print ans |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.