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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
s330983816 | p00741 | u766477342 | 1420337502 | Python | Python3 | py | Runtime Error | 0 | 0 | 578 | while 1:
W, H = list(map(int,input().split()))
if W == 0:break
C = [list(map(int, input().split())) for i in range(H) ]
def work(x,y):
ax = [-1, 0, 1, -1, 1, -1, 0, 1]
ay = [-1, -1, -1, 0, 0, 1, 1, 1]
if C[y][x] != 1:return False
C[y][x] = -1 #mark
for _ax in ax:
for _ay in ay:
if 0 <= _ax + x < W and 0 <= _ay + y < H:
work(_ax + x, _ay + y)
return True
cnt = 0
for x in range(W):
for y in range(H):
cnt += work(x,y)
print(cnt) |
s078533651 | p00741 | u316268279 | 1421209975 | Python | Python3 | py | Runtime Error | 0 | 0 | 740 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(len(dxdy)):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((i,j),Map,H,W,c)
print(c-1) |
s927168368 | p00741 | u316268279 | 1421211575 | Python | Python3 | py | Runtime Error | 0 | 0 | 740 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(len(dxdy)):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((i,j),Map,H,W,c)
print(c-1) |
s830911176 | p00741 | u316268279 | 1421211790 | Python | Python3 | py | Runtime Error | 0 | 0 | 732 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(8):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((i,j),Map,H,W,c)
print(c-1) |
s845816290 | p00741 | u316268279 | 1421211956 | Python | Python3 | py | Runtime Error | 0 | 0 | 740 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(len(dxdy)):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((0,0),Map,H,W,c)
print(c-1) |
s624524082 | p00741 | u316268279 | 1421212967 | Python | Python3 | py | Runtime Error | 0 | 0 | 732 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(4):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((i,j),Map,H,W,c)
print(c-1) |
s816205053 | p00741 | u316268279 | 1421213085 | Python | Python3 | py | Runtime Error | 0 | 0 | 732 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W,c):
dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
for i in range(3):
Map[s[0]][s[1]] = c
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == 1:
dfs(nextPos,Map,H,W,c)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(map(int,input().split())) for i in range(0,H)]
c = 1
for i in range(H):
for j in range(W):
if Map[i][j] == 1:
c += 1
dfs((i,j),Map,H,W,c)
print(c-1) |
s308452010 | p00741 | u633068244 | 1421783727 | Python | Python | py | Runtime Error | 0 | 0 | 480 | def dfs(x, y):
C[y][x] = 0
for dx,dy in zip([1,1,0,-1,-1,-1,0,1],[0,1,1,1,0,-1,-1,-1]):
if 0 <= x+dx < w and 0 <= y+dy < h and C[y+dy][x+dx] == 1:
dfs(x+dx, y+dy)
while 1:
w,h = map(int,raw_input().split())
if w == h == 0: break
C = [map(int,raw_input().split()) for _ in xrange(h)]
ans = 0
for y in range(h):
for x in range(w):
if C[y][x] == 1:
dfs(x, y)
ans += 1
print ans |
s902360511 | p00741 | u731235119 | 1421922066 | Python | Python | py | Runtime Error | 0 | 0 | 1067 | def travel(here, lb, ub, field, label):
if field[here[1]][here[0]] == 1:
field[here[1]][here[0]] = label
if lb[0] < here[0] :
travel((here[0]-1, here[1]), lb, ub, field, label)
if lb[1] < here[1]:
travel((here[0]-1, here[1]-1), lb, ub, field, label)
if lb[1] < here[1] :
travel((here[0], here[1]-1), lb, ub, field, label)
if ub[0] > here[0] :
travel((here[0]+1, here[1]-1), lb, ub, field, label)
if ub[0] > here[0] :
travel((here[0]+1, here[1]), lb, ub, field, label)
if ub[1] > here[1]:
travel((here[0]+1, here[1]+1), lb, ub, field, label)
if ub[1] > here[1] :
travel((here[0], here[1]+1), lb, ub, field, label)
if lb[0] < here[0] :
travel((here[0]-1, here[1]+1), lb, ub, field, label)
while 1:
w, h = map(int, raw_input().split(" "))
if (w,h) == (0,0):
break
island = []
for j in range(h):
island.append(map(int, raw_input().split(" ")))
label = 1
for j in range(h):
for i in range(w):
if island[j][i] == 1:
label += 1
travel((i,j), (0,0), (w-1,h-1), island, label)
print label - 1 |
s798469392 | p00741 | u120360464 | 1422864424 | Python | Python | py | Runtime Error | 0 | 0 | 883 | #! /usr/bin/env python
# -*- coding: utf-8 -*-
def rec(y, x, used, TILE):
if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
return 0
else:
used[y][x] = 1
rec(y-1, x-1, used, TILE)
rec(y-1, x, used, TILE)
rec(y-1, x+1, used, TILE)
rec(y, x-1, used, TILE)
rec(y, x, used, TILE)
rec(y, x+1, used, TILE)
rec(y+1, x-1, used, TILE)
rec(y+1, x, used, TILE)
rec(y+1, x+1, used, TILE)
return 1
(W, H) = map(int, raw_input().split())
while W!=0:
used = [[0]*W for i in range(H)]
TILE = []
s = []
for i in range(H):
TILE.append(map(int, raw_input().split()))
Sum = 0
for i in range(H):
for j in range(W):
Sum += rec(i, j, used, TILE)
print Sum
#print used.count(1)
(W, H) = map(int, raw_input().split()) |
s858086583 | p00741 | u120360464 | 1422866808 | Python | Python | py | Runtime Error | 0 | 0 | 929 | #! /usr/bin/env python
# -*- coding: utf-8 -*-
def rec(y, x, used, TILE):
if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
return 0
else:
used[y][x] = 1
rec(y-1, x-1, used, TILE)
rec(y-1, x, used, TILE)
rec(y-1, x+1, used, TILE)
rec(y, x-1, used, TILE)
rec(y, x, used, TILE)
rec(y, x+1, used, TILE)
rec(y+1, x-1, used, TILE)
rec(y+1, x, used, TILE)
rec(y+1, x+1, used, TILE)
return 1
(W, H) = map(int, raw_input().split())
while W!=0 :
used = [[0]*W for i in range(H)]
TILE = []
for i in range(H):
TILE.append(map(int, raw_input().split()))
Sum = 0
for i in range(H):
for j in range(W):
if used[i][j] == 0 and TILE[i][j] == 1:
Sum += rec(i, j, used, TILE)
print Sum
#print used.count(1)
(W, H) = map(int, raw_input().split()) |
s322222028 | p00741 | u124909914 | 1426138421 | Python | Python3 | py | Runtime Error | 0 | 0 | 574 | m = []
def walk(i,j):
if i < 0 or j < 0 \
or i >= len(m) or j >= len(m[i]):
return
if m[i][j] == 0:
return
m[i][j] = 0
for k in range(-1,2):
for l in range(-1,2):
walk(i+k,j+l)
while True:
w, h = map(int, input().split())
if w == 0: break
for i in range(h):
m.append(list(map(int, input().split())))
ans = 0
for i in range(len(m)):
for j in range(len(m[i])):
if m[i][j] == 1:
walk(i,j)
ans += 1
print(ans)
m = [] |
s903009363 | p00741 | u885631908 | 1435043031 | Python | Python3 | py | Runtime Error | 0 | 0 | 453 | w, h = map(int,input().split())
c = [list(map(int,input().split())) for j in range(h)]
count = 0
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if(0 <= nx < w and 0 <= ny <= h and c[nx][ny] == 1 ):
dfs(nx, ny)
for i in range(h):
for j in range(w):
if (c[i][j] == 1):
dfs(i,j)
count += 1
print(count) |
s711216833 | p00741 | u885631908 | 1435043159 | Python | Python3 | py | Runtime Error | 0 | 0 | 478 | # -*- coding: utf-8 -*-
w, h = map(int,input().split())
c = [list(map(int,input().split())) for j in range(h)]
count = 0
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if(0 <= nx < w and 0 <= ny <= h and c[nx][ny] == 1 ):
dfs(nx, ny)
for i in range(h):
for j in range(w):
if (c[i][j] == 1):
dfs(i,j)
count += 1
print(count) |
s590866479 | p00741 | u885631908 | 1435044981 | Python | Python3 | py | Runtime Error | 0 | 0 | 568 | # -*- coding: utf-8 -*-
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if (c[i][j] == 1):
dfs(i,j)
count += 1
print(count) |
s118253971 | p00741 | u885631908 | 1435045158 | Python | Python3 | py | Runtime Error | 0 | 0 | 566 | # -*- coding: utf-8 -*-
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if c[i][j] == 1:
dfs(i,j)
count += 1
print(count) |
s415440444 | p00741 | u885631908 | 1435045239 | Python | Python3 | py | Runtime Error | 0 | 0 | 562 | # -*- coding: utf-8 -*-
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if c[i][j] == 1:
dfs(i,j)
count += 1
print(count) |
s620648098 | p00741 | u885631908 | 1435045374 | Python | Python3 | py | Runtime Error | 0 | 0 | 566 | # -*- coding: utf-8 -*-
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if c[i][j] == 1:
dfs(i,j)
count += 1
print(count) |
s985738346 | p00741 | u885631908 | 1435045441 | Python | Python3 | py | Runtime Error | 0 | 0 | 542 | def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if c[i][j] == 1:
dfs(i,j)
count += 1
print(count) |
s951702792 | p00741 | u885631908 | 1435143491 | Python | Python3 | py | Runtime Error | 0 | 0 | 566 | # -*- coding: utf-8 -*-
def dfs(x,y):
c[x][y] = 0
for dx in [-1,0,1]:
for dy in [-1,0,1]:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
dfs(nx, ny)
while True:
w, h = map(int,input().split())
if w == 0 and h == 0 :
break
c = [list(map(int,input().split())) for j in range(h)]
count = 0
for i in range(h):
for j in range(w):
if c[i][j] == 1:
dfs(i,j)
count += 1
print(count) |
s008848033 | p00741 | u966364923 | 1436628577 | Python | Python3 | py | Runtime Error | 0 | 0 | 675 | def dfs(tiles, W, H, x, y):
tiles[y][x] = '0'
for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
dfs(tiles, W, H, x+dx, y+dy)
return
def main():
while True:
W,H = [int(x) for x in input().split()]
if W==0 and H==0:
exit()
tiles = [[c for c in input().split()] for h in range(H)]
cnt = 0
for h in range(H):
for w in range(W):
if tiles[h][w] == '1':
cnt += 1
dfs(tiles, W, H, w, h)
print(cnt)
if __name__ == '__main__':
main() |
s153854768 | p00741 | u966364923 | 1436628857 | Python | Python3 | py | Runtime Error | 0 | 0 | 719 | import sys
def dfs(tiles, W, H, x, y):
tiles[y][x] = '0'
for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
dfs(tiles, W, H, x+dx, y+dy)
return
def main():
while True:
W,H = [int(x) for x in input().split()]
if W==0 and H==0:
exit()
tiles = [[c for c in input().split()] for h in range(H)]
cnt = 0
for h in range(H):
for w in range(W):
if tiles[h][w] == '1':
cnt += 1
dfs(tiles, W, H, w, h)
print(cnt)
if __name__ == '__main__':
sys.getrecursionlimit(10000)
main() |
s192600304 | p00741 | u489809100 | 1449145802 | Python | Python | py | Runtime Error | 0 | 0 | 818 | def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s204116606 | p00741 | u489809100 | 1449145867 | Python | Python | py | Runtime Error | 0 | 0 | 860 | import sys
sys.setrecursionlimit(100000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s853417183 | p00741 | u489809100 | 1449145880 | Python | Python | py | Runtime Error | 0 | 0 | 861 | import sys
sys.setrecursionlimit(1000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s542809058 | p00741 | u489809100 | 1449145928 | Python | Python | py | Runtime Error | 0 | 0 | 862 | import sys
sys.setrecursionlimit(10000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s077531170 | p00741 | u489809100 | 1449145940 | Python | Python | py | Runtime Error | 0 | 0 | 863 | import sys
sys.setrecursionlimit(100000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s596000402 | p00741 | u489809100 | 1449145955 | Python | Python | py | Runtime Error | 0 | 0 | 864 | import sys
sys.setrecursionlimit(1000000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s814757686 | p00741 | u489809100 | 1449145968 | Python | Python | py | Runtime Error | 0 | 0 | 867 | import sys
sys.setrecursionlimit(1000000000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("$" * (2 + h)))
for i in range(0, w) : array.append(list("$" + raw_input().replace(" ", "") + "$"))
array.append(list("$" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s804376353 | p00741 | u489809100 | 1449146293 | Python | Python | py | Runtime Error | 0 | 0 | 818 | def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("0" * (2 + h)))
for i in range(0, w) : array.append(list("0" + raw_input().replace(" ", "") + "0"))
array.append(list("0" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s093489680 | p00741 | u489809100 | 1449146323 | Python | Python | py | Runtime Error | 0 | 0 | 862 | import sys
sys.setrecursionlimit(10000000)
def check(x, y):
array[x][y] = "0"
if array[x + 1][y] == "1" : check(x + 1, y)
if array[x - 1][y] == "1" : check(x - 1, y)
if array[x][y + 1] == "1" : check(x, y + 1)
if array[x][y - 1] == "1" : check(x, y - 1)
if array[x + 1][y + 1] == "1" : check(x + 1, y + 1)
if array[x + 1][y - 1] == "1" : check(x + 1, y - 1)
if array[x - 1][y + 1] == "1" : check(x - 1, y + 1)
if array[x - 1][y - 1] == "1" : check(x - 1, y - 1)
while True:
h,w = map(int, raw_input().split())
if h == 0 and w == 0 : break
array = []
array.append(list("0" * (2 + h)))
for i in range(0, w) : array.append(list("0" + raw_input().replace(" ", "") + "0"))
array.append(list("0" * (2 + h)))
count = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
if array[i][j] == "1":
check(i, j)
count += 1
print count |
s619518286 | p00741 | u488601719 | 1453800417 | Python | Python | py | Runtime Error | 0 | 0 | 653 | vx = [1, -1, 0, 0, 1, 1, -1, -1]
vy = [0, 0, 1, -1, 1, -1, 1, -1]
def dfs(col, row):
c[col][row] = 0
for x, y in zip(vx, vy):
ny = col + y
nx = row + x
if nx < 0 or ny < 0 or nx >= w or ny >= h:
continue
if c[ny][nx] == 0:
continue
dfs(ny, nx)
while True:
w, h = map(int, raw_input().split())
if w == 0:
break
c = []
for _ in range(h):
c.append(map(int, raw_input().split()))
cnt = 0
for col in range(h):
for row in range(w):
if c[col][row] == 1:
cnt += 1
dfs(col, row)
print cnt |
s538106669 | p00741 | u488601719 | 1453800439 | Python | Python | py | Runtime Error | 0 | 0 | 653 | vx = [1, -1, 0, 0, 1, 1, -1, -1]
vy = [0, 0, 1, -1, 1, -1, 1, -1]
def dfs(col, row):
c[col][row] = 0
for x, y in zip(vx, vy):
ny = col + y
nx = row + x
if nx < 0 or ny < 0 or nx >= w or ny >= h:
continue
if c[ny][nx] == 0:
continue
dfs(ny, nx)
while True:
w, h = map(int, raw_input().split())
if w == 0:
break
c = []
for _ in range(h):
c.append(map(int, raw_input().split()))
cnt = 0
for col in range(h):
for row in range(w):
if c[col][row] == 1:
cnt += 1
dfs(col, row)
print cnt |
s585109656 | p00741 | u248416507 | 1464074912 | Python | Python | py | Runtime Error | 0 | 0 | 793 |
def visit(x, y, area):
move = [(-1, 0), (0, 1), (1, 0), (0, -1), (1, -1), (1, 1), (-1, 1), (-1, -1)]
for i in move:
if 0 <= (x + i[0]) < w and 0 <= (y + i[1]) < h and area[y + i[1]][x + i[0]] == 1:
area[y + i[1]][x + i[0]] = 0
visit(x + i[0], y + i[1], area)
def solve():
area = []
ans = 0
for i in range(h):
area.append(map(int, raw_input().split()))
#print 'w: ' + str(w) + ',h: ' + str(h)
for i in range(h):
for j in range(w):
if area[i][j] == 1:
ans += 1
visit(j, i, area)
print ans
if __name__ == "__main__":
while True:
w , h = 0, 0
w, h = map(int, raw_input().split())
if w == 0 and h == 0:
break
solve() |
s168470209 | p00741 | u336892997 | 1465810090 | Python | Python3 | py | Runtime Error | 0 | 0 | 785 | def solve():
w, h = [int(x) for x in input().split()]
if w == h == 0:
return False
field = [[int(x) for x in input().split()] for _ in range(h)]
ans = 0
for x in range(w):
for y in range(h):
if field[y][x] > 0:
mark(field, w, h, x, y)
ans += 1
print(ans)
return True
def mark(field, w, h, x, y):
if x < 0 or x >= w or y < 0 or y >= h:
return
if field[y][x] != 1:
return
field[y][x] = -1
mark(field, w, h, x+1, y+1)
mark(field, w, h, x+1, y)
mark(field, w, h, x+1, y-1)
mark(field, w, h, x, y+1)
mark(field, w, h, x, y-1)
mark(field, w, h, x-1, y+1)
mark(field, w, h, x-1, y)
mark(field, w, h, x-1, y-1)
while solve():
pass |
s713101965 | p00741 | u660912567 | 1481281969 | Python | Python3 | py | Runtime Error | 0 | 0 | 475 | k = [-1,0,1]
def search(x,y,m):
if x<0 or x>=len(m[0]) or y<0 or y>=len(m): return
if m[y][x]==0: return
m[y][x] = 0
for i in k:
for j in k:
search(x+i,y+j,m)
while True:
c = 0
w,h = map(int,input().split())
if w==0: break
m = [list(map(int,input().split())) for i in range(h)]
for i in range(h):
for j in range(w):
if m[i][j]==1:
search(j, i, m)
c += 1
print(c) |
s380211737 | p00741 | u660912567 | 1481908561 | Python | Python3 | py | Runtime Error | 0 | 0 | 454 | d = [-1,0,1]
def search(x,y,m):
if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
m[y][x] = '0'
for dy in d:
for dx in d:
search(x+dx,y+dy,m)
while True:
w,h = map(int,input().split())
if w==h==0: break
m = [input().split() for _ in range(h)]
c = 0
for y in range(h):
for x in range(w):
if m[y][x]=='1':
c += 1
search(x,y,m)
print(c) |
s200327464 | p00741 | u661290476 | 1487494259 | Python | Python3 | py | Runtime Error | 0 | 0 | 536 | def erase(x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
if c[ny][nx] == '1':
erase(nx, ny)
while True:
w, h = map(int, input().split())
c = [[i for i in input().split()] for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(x, y)
print(ans) |
s695047334 | p00741 | u661290476 | 1487494551 | Python | Python3 | py | Runtime Error | 0 | 0 | 570 | def erase(x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
if c[ny][nx] == '1':
erase(nx, ny)
while True:
w, h = map(int, input().split())
if w == h == 0:
break
c = [[i for i in input().split()] for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(x, y)
print(ans) |
s704981177 | p00741 | u661290476 | 1487495272 | Python | Python3 | py | Runtime Error | 0 | 0 | 588 | def erase(w, h, x, y):
c[y][x] = '0'
for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx <= w -1 and 0 <= ny <= h - 1:
if c[ny][nx] == '1':
erase(w, h, nx, ny)
while True:
w, h = map(int, input().split())
if w == h == 0:
break
c = [[i for i in input().split()] for _ in range(h)]
ans = 0
for y in range(h):
for x in range(w):
if c[y][x] == '1':
ans += 1
erase(w, h, x, y)
print(ans) |
s428399193 | p00741 | u078042885 | 1487498861 | Python | Python3 | py | Runtime Error | 0 | 0 | 407 | def f(x,y):
a[y][x]='0'
for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
dx+=x;dy+=y
if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
w,h=map(int,input().split())
if w==0:break
a=[list(input().split()) for _ in[0]*h];b=0
for i in range(w):
for j in range(h):
if a[j][i]=='1':
b+=1;f(i,j)
print(b) |
s677201114 | p00741 | u078042885 | 1487498991 | Python | Python3 | py | Runtime Error | 0 | 0 | 447 | import sys
sys.setrecursionlimit(1<<32)
def f(x,y):
a[y][x]='0'
for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
dx+=x;dy+=y
if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
w,h=map(int,input().split())
if w==0:break
a=[list(input().split()) for _ in[0]*h];b=0
for i in range(w):
for j in range(h):
if a[j][i]=='1':
b+=1;f(i,j)
print(b) |
s650858426 | p00741 | u923668099 | 1495616620 | Python | Python3 | py | Runtime Error | 0 | 0 | 839 | import sys
def solve():
while 1:
w, h = map(int, sys.stdin.readline().split())
if w == h == 0:
return
room = [[int(j) for j in sys.stdin.readline().split()] for i in range(h)]
cnt = 0
for i in range(h):
for j in range(w):
if room[i][j]:
cnt += 1
dfs(w, h, room, i, j)
print(cnt)
def dfs(w, h, room, i, j):
if i < 0 or i >= h or j < 0 or j >= w or (not room[i][j]):
return
room[i][j] = 0
dfs(w, h, room, i + 1, j)
dfs(w, h, room, i - 1, j)
dfs(w, h, room, i, j + 1)
dfs(w, h, room, i, j - 1)
dfs(w, h, room, i + 1, j + 1)
dfs(w, h, room, i + 1, j - 1)
dfs(w, h, room, i - 1, j + 1)
dfs(w, h, room, i - 1, j - 1)
if __name__ == '__main__':
solve() |
s024614965 | p00741 | u502536402 | 1524374814 | Python | Python3 | py | Runtime Error | 0 | 0 | 814 | #!python
# -*- coding: utf-8 -*-
import sys
import numpy as np
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y, x]:
return
map_[y, x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = np.array([list(map(int, input().split())) for h in range(H)])
# print(map_)
island_num = 0
while np.amax(map_):
x = np.argmax(map_) % W
y = np.argmax(map_) // W
# print(map_)
# print("x: {} | y: {}".format(x, y))
DFS(x, y)
island_num += 1
print('\t', island_num)
|
s121003760 | p00741 | u502536402 | 1524374852 | Python | Python3 | py | Runtime Error | 0 | 0 | 808 | #!python
# -*- coding: utf-8 -*-
import sys
import numpy as np
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y, x]:
return
map_[y, x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = np.array([list(map(int, input().split())) for h in range(H)])
# print(map_)
island_num = 0
while np.amax(map_):
x = np.argmax(map_) % W
y = np.argmax(map_) // W
# print(map_)
# print("x: {} | y: {}".format(x, y))
DFS(x, y)
island_num += 1
print(island_num)
|
s845864334 | p00741 | u502536402 | 1524385852 | Python | Python3 | py | Runtime Error | 0 | 0 | 808 | #!python
# -*- coding: utf-8 -*-
import sys
import numpy as np
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y, x]:
return
map_[y, x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = np.array([list(map(int, input().split())) for h in range(H)])
# print(map_)
island_num = 0
while np.amax(map_):
x = np.argmax(map_) % W
y = np.argmax(map_) // W
# print(map_)
# print("x: {} | y: {}".format(x, y))
DFS(x, y)
island_num += 1
print(island_num)
|
s931229307 | p00741 | u502536402 | 1524386760 | Python | Python3 | py | Runtime Error | 0 | 0 | 845 | #!python
# -*- coding: utf-8 -*-
import sys
import numpy as np
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y * W + x]:
return
map_[y * W + x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = []
for h in range(H):
map_ += list(map(int, input().split()))
# print(map_)
island_num = 0
while any(map_):
next_idx = map_.index(1)
x = next_idx % W
y = next_idx // W
# print(map_)
# print("x: {} | y: {}".format(x, y))
DFS(x, y)
island_num += 1
print(island_num)
|
s223014431 | p00741 | u502536402 | 1524386779 | Python | Python3 | py | Runtime Error | 0 | 0 | 825 | #!python
# -*- coding: utf-8 -*-
import sys
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y * W + x]:
return
map_[y * W + x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = []
for h in range(H):
map_ += list(map(int, input().split()))
# print(map_)
island_num = 0
while any(map_):
next_idx = map_.index(1)
x = next_idx % W
y = next_idx // W
# print(map_)
# print("x: {} | y: {}".format(x, y))
DFS(x, y)
island_num += 1
print(island_num)
|
s645649142 | p00741 | u502536402 | 1524387022 | Python | Python3 | py | Runtime Error | 0 | 0 | 705 | import sys
sys.setswitchinterval(10**8)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if not map_[y * W + x]:
return
map_[y * W + x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = []
for h in range(H):
map_ += list(map(int, input().split()))
island_num = 0
while any(map_):
next_idx = map_.index(1)
x = next_idx % W
y = next_idx // W
DFS(x, y)
island_num += 1
print(island_num)
|
s767661715 | p00741 | u502536402 | 1524583714 | Python | Python3 | py | Runtime Error | 0 | 0 | 699 | #!python
# -*- coding: utf-8 -*-
import sys
sys.setswitchinterval(10**9)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if map_[y][x] == 0:
return
map_[y][x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = [list(map(int, input().split())) for h in range(H)]
island_num = 0
for x in range(W):
for y in range(H):
if map_[y][x] == 1:
DFS(x, y)
island_num += 1
print(island_num)
|
s566066104 | p00741 | u502536402 | 1524583800 | Python | Python3 | py | Runtime Error | 0 | 0 | 699 | #!python
# -*- coding: utf-8 -*-
import sys
sys.setswitchinterval(10**9)
next_s = ((-1, 1), (0, 1), (1, 1),
(-1, 0), (1, 0),
(-1, -1), (0, -1), (1, -1))
def DFS(x, y):
if not (0 <= x < W and 0 <= y < H):
return
if map_[y][x] == 0:
return
map_[y][x] = 0
for dx, dy in next_s:
DFS(x + dx, y + dy)
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
map_ = [list(map(int, input().split())) for h in range(H)]
island_num = 0
for x in range(W):
for y in range(H):
if map_[y][x] == 1:
DFS(x, y)
island_num += 1
print(island_num)
|
s022326171 | p00741 | u023471147 | 1527037233 | Python | Python3 | py | Runtime Error | 0 | 0 | 668 | w, h = 0, 0
def dfs(x, y, data):
data[x][y] = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
sx, sy = x + dx, y + dy
if sx < 0 or sx >= h or sy < 0 or sy >= w:
continue
if data[sx][sy] > 0:
data = dfs(sx, sy, data)
return data
while True:
w, h = map(int, input().split())
if w == h == 0:
break
data = []
for i in range(h):
data.append(list(map(int, input().split())))
ans = 0
for x in range(h):
for y in range(w):
if data[x][y] > 0:
ans += 1
data = dfs(x, y, data)
print (ans)
|
s683381761 | p00741 | u572046143 | 1527955473 | Python | Python3 | py | Runtime Error | 0 | 0 | 884 | def dfs(field, i, j, w, h):
field[i][j] = "0"
OFFSETS = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
for di, dj in OFFSETS:
if i+di < 0 or i+di >= h:
continue
if j+dj < 0 or j+dj >= w:
continue
if field[i+di][j+dj] == "1":
dfs(field, i+di, j+dj, w, h)
def main():
while True:
w, h = [int(s) for s in input().strip().split()]
if w == 0 and h == 0:
break
num_island = 0
field = []
for _ in range(h):
field.append(input().strip().split())
#print(field)
for i in range(h):
for j in range(w):
if field[i][j] == "1":
num_island += 1
dfs(field, i, j, w, h)
print(num_island)
if __name__ == "__main__":
main()
|
s262939366 | p00741 | u651355315 | 1529316158 | Python | Python3 | py | Runtime Error | 0 | 0 | 673 | # -*- coding: utf-8 -*-
def search(n, m, tile, N, M):
for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
if 0 <= dy + n < N and 0 <= dx + m < M:
if tile[dy + n][dx + m] == '1':
tile[dy + n][dx + m] = '2'
search(dy + n, dx + m, tile, N, M)
while True:
M, N = map(int, input().split())
if M == 0 and N == 0:
break
tile = [input().split() for n in range(N)]
ans = 0
for n in range(N):
for m in range(M):
if tile[n][m] == '1':
tile[n][m] = '2'
search(n, m, tile, N, M)
ans += 1
print(ans)
|
s376692078 | p00741 | u651355315 | 1529316173 | Python | Python3 | py | Runtime Error | 0 | 0 | 673 | # -*- coding: utf-8 -*-
def search(n, m, tile, N, M):
for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
if 0 <= dy + n < N and 0 <= dx + m < M:
if tile[dy + n][dx + m] == '1':
tile[dy + n][dx + m] = '2'
search(dy + n, dx + m, tile, N, M)
while True:
M, N = map(int, input().split())
if M == 0 and N == 0:
break
tile = [input().split() for n in range(N)]
ans = 0
for n in range(N):
for m in range(M):
if tile[n][m] == '1':
tile[n][m] = '2'
search(n, m, tile, N, M)
ans += 1
print(ans)
|
s271385216 | p00741 | u136916346 | 1530153932 | Python | Python3 | py | Runtime Error | 0 | 0 | 731 | def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
s287524542 | p00741 | u136916346 | 1530154342 | Python | Python3 | py | Runtime Error | 0 | 0 | 1102 | def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
s494968269 | p00741 | u136916346 | 1530154451 | Python | Python3 | py | Runtime Error | 0 | 0 | 1107 | def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
l[y-1][x+1]=0
fill(y-1,x+1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
s120665732 | p00741 | u136916346 | 1530154765 | Python | Python3 | py | Runtime Error | 0 | 0 | 1147 | import sys
sys.setrecursionlimit(10000)
def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
l[y-1][x+1]=0
fill(y-1,x+1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
s838105007 | p00741 | u500292616 | 1530688201 | Python | Python | py | Runtime Error | 0 | 0 | 1381 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
#------
|
s492978889 | p00741 | u500292616 | 1530688260 | Python | Python | py | Runtime Error | 0 | 0 | 1369 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s645256540 | p00741 | u500292616 | 1530688273 | Python | Python | py | Runtime Error | 0 | 0 | 1346 |
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s555100266 | p00741 | u500292616 | 1530688280 | Python | Python3 | py | Runtime Error | 0 | 0 | 1346 |
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s139661211 | p00741 | u500292616 | 1530688310 | Python | Python3 | py | Runtime Error | 0 | 0 | 1369 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s170287601 | p00741 | u500292616 | 1530688328 | Python | Python | py | Runtime Error | 0 | 0 | 1369 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s561051344 | p00741 | u500292616 | 1530688392 | Python | Python | py | Runtime Error | 0 | 0 | 1318 | h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s209767444 | p00741 | u500292616 | 1530688491 | Python | Python | py | Runtime Error | 0 | 0 | 1318 | h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s804578463 | p00741 | u500292616 | 1530688612 | Python | Python | py | Runtime Error | 0 | 0 | 1367 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s314047401 | p00741 | u500292616 | 1530688660 | Python | Python3 | py | Runtime Error | 0 | 0 | 1367 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s221341630 | p00741 | u500292616 | 1530689064 | Python | Python | py | Runtime Error | 0 | 0 | 1367 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s268187374 | p00741 | u500292616 | 1530689425 | Python | Python | py | Runtime Error | 0 | 0 | 1368 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s555522397 | p00741 | u500292616 | 1530693392 | Python | Python | py | Runtime Error | 0 | 0 | 1368 | # -*- coding: utf-8 -*-
#150 島は幾つある?
h,w=1,1
count=0
anser=0
def search(x,y):
#マップの外側か壁の場合は何もしない
#print(x,y)
global count
count+=1
if x<0 or x>w-1 or y<0 or y>h-1 or map_data[y][x]==0:
return
#以前に到達していたら何もしない
if reached[y][x] ==0:
return
reached[y][x]=0
#print("aa")
search(x+1,y)#右
search(x-1,y)#左
search(x,y+1)#上
search(x,y-1)#下
search(x-1,y-1)
search(x-1,y+1)
search(x+1,y-1)
search(x+1,y+1)
while h!=0 and w!=0:
anser=0
count=1
#グラフの入力方法------
w,h=map(int ,raw_input().split())
if h==0 and w==0:
break
map_data = [[0]*w for _ in range(h)]
reached = [[1]*w for _ in range(h)]
for i in range(h):
ct = raw_input().split()
for j in range(w):
map_data[i][j] = int(ct[j])
reached [i][j]=int(ct[j])
if ct[j]==1:
sx=j
sy=i
t=0
a=0
while a<=h:
if 1 in reached[t%h]:
search(reached[t%h].index(1),t%h)
anser+=1
#print("count",count)
count=1
a=0
elif count==h:
break
t+=1
a+=1
print(anser)
|
s160805073 | p00741 | u779627195 | 1352885304 | Python | Python | py | Runtime Error | 0 | 18000 | 769 | def destroy(m, x, y):
#print x,y
if w-1 >= x >= 0 and h-1 >= y >= 0:
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
destroy(m, x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(h):
for j in xrange(w):
if m[i][j] == 1:
c += 1
destroy(m, j, i)
print c |
s740984368 | p00741 | u779627195 | 1352885954 | Python | Python | py | Runtime Error | 0 | 18000 | 841 | def destroy(m, x, y):
#print x,y
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if not (i == 0 and j == 0):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0:
destroy(m, x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(m, i, j)
print c |
s769948074 | p00741 | u779627195 | 1352886597 | Python | Python | py | Runtime Error | 0 | 18000 | 920 | def destroy(m, x, y):
#print x,y
v[y][x] = True
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0:
if v[y+j][x+i] is False:
destroy(m, x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
v = [[False for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(m, i, j)
print c |
s050646372 | p00741 | u915761101 | 1352887057 | Python | Python | py | Runtime Error | 0 | 18000 | 872 | def destroy(m, x, y):
# print "(",x,y,")"
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if not (i == 0 and j == 0):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0 and m[y+j][x+i] == 1:
destroy(m, x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(m, i, j)
print c |
s031354073 | p00741 | u915761101 | 1352887148 | Python | Python | py | Runtime Error | 0 | 18000 | 869 | m=[]
def destroy(x, y):
# print "(",x,y,")"
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if not (i == 0 and j == 0):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0 and m[y+j][x+i] == 1:
destroy(x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(i, j)
print c |
s391643442 | p00741 | u915761101 | 1352887212 | Python | Python | py | Runtime Error | 0 | 18000 | 892 | import sys
m=[]
def destroy(x, y):
print>>sys.stderr, "(",x,y,")"
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if not (i == 0 and j == 0):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0 and m[y+j][x+i] == 1:
destroy(x+i, y+j)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(i, j)
print c |
s337814303 | p00741 | u915761101 | 1352887347 | Python | Python | py | Runtime Error | 0 | 18000 | 18 | def f():
f()
f() |
s155645409 | p00741 | u915761101 | 1352887402 | Python | Python | py | Runtime Error | 0 | 18000 | 57 | import sys
def f(n):
print>>sys.stderr, n
f(n+1)
f(1) |
s863487122 | p00741 | u915761101 | 1352887462 | Python | Python | py | Runtime Error | 0 | 18000 | 74 | import sys
def f(n):
if n%100==0:
print>>sys.stderr, n
f(n+1)
f(1) |
s458716348 | p00741 | u915761101 | 1352887614 | Python | Python | py | Runtime Error | 0 | 18000 | 931 | import sys
m=[]
def destroy(x, y,n):
if n%100==0:
print>>sys.stderr, "(",x,y,")",n
if m[y][x] == 1:
m[y][x] = 2
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if not (i == 0 and j == 0):
if w-1 >= x+i >= 0 and h-1 >= y+j >= 0 and m[y+j][x+i] == 1:
destroy(x+i, y+j,n+1)
while 1:
c = 0
w,h = map(int, raw_input().split())
if w == 0 and h == 0: break
m = [[0 for j in xrange(w)] for i in xrange(h)]
for i in xrange(h):
m[i] = map(int, raw_input().split())
for i in xrange(w):
for j in xrange(h):
if m[j][i] == 1:
c += 1
destroy(i, j,1)
print c |
s284527528 | p00741 | u104911888 | 1368855811 | Python | Python | py | Runtime Error | 0 | 0 | 480 | def dfs(x,y):
C[y][x]=0
for dx in range(-1,2):
for dy in range(-1,2):
mx=x+dx
my=y+dy
if 0<=mx<w and 0<=my<h and C[my][mx]==1:
dfs(mx,my)
while True:
w,h=map(int,raw_input().split())
if w==h==0:break
C=[map(int,raw_input().split()) for i in range(h)]
cnt=0
for y in range(h):
for x in range(w):
if C[y][x]==1:
dfs(x,y)
cnt+=1
print cnt |
s158288073 | p00741 | u104911888 | 1368855887 | Python | Python | py | Runtime Error | 0 | 0 | 491 | def dfs(x,y):
C[y][x]=0
for dx in range(-1,2):
for dy in range(-1,2):
mx=x+dx
my=y+dy
if 0<=mx<w and 0<=my<h and C[my][mx]==1:
dfs(mx,my)
return
while True:
w,h=map(int,raw_input().split())
if w==h==0:break
C=[map(int,raw_input().split()) for i in range(h)]
cnt=0
for y in range(h):
for x in range(w):
if C[y][x]==1:
dfs(x,y)
cnt+=1
print cnt |
s382748150 | p00741 | u104911888 | 1369571163 | Python | Python | py | Runtime Error | 0 | 0 | 477 | def dfs(i,j):
C[i][j]=-1
for x in range(-1,2):
for y in range(-1,2):
mx=x+j
my=y+i
if 0<=mx<w and 0<=my<h and C[my][mx]==1:
dfs(my,mx)
while True:
w,h=map(int,raw_input().split())
if w==h==0:break
C=[map(int,raw_input().split()) for i in range(h)]
cnt=0
for i in range(h):
for j in range(w):
if C[i][j]==1:
dfs(i,j)
cnt+=1
print cnt |
s826728061 | p00741 | u104911888 | 1369571378 | Python | Python | py | Runtime Error | 0 | 0 | 525 | def dfs(i,j):
C[i][j]=-1
for x in range(-1,2):
for y in range(-1,2):
mx=x+j
my=y+i
if mx<0 or my<0 or mx>=w or my>=h:
continue
if C[my][mx]==1:
dfs(my,mx)
while True:
w,h=map(int,raw_input().split())
if w==h==0:break
C=[map(int,raw_input().split()) for i in range(h)]
cnt=0
for i in range(h):
for j in range(w):
if C[i][j]==1:
dfs(i,j)
cnt+=1
print cnt |
s252813235 | p00741 | u633068244 | 1399521327 | Python | Python | py | Runtime Error | 0 | 0 | 422 | dxy = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dx,dy in dxy:
solve(w+dx,h+dy)
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(w,h)
ans += 1
print ans |
s419181813 | p00741 | u633068244 | 1399527599 | Python | Python | py | Runtime Error | 0 | 0 | 435 | global field
dxy = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dx,dy in dxy:
solve(w+dx,h+dy)
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(w,h)
ans += 1
print ans |
s567116641 | p00741 | u633068244 | 1399528040 | Python | Python | py | Runtime Error | 0 | 0 | 440 | dxy = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(field,w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dx,dy in dxy:
solve(field,w+dx,h+dy)
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(field,w,h)
ans += 1
print ans |
s946789338 | p00741 | u633068244 | 1399528083 | Python | Python | py | Runtime Error | 0 | 0 | 469 | dxy = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(field,w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dx,dy in dxy:
solve(field,w+dx,h+dy)
return 1
else:
return 0
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(field,w,h)
ans += 1
print ans |
s073863723 | p00741 | u633068244 | 1399528821 | Python | Python | py | Runtime Error | 0 | 0 | 440 | dwh = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(field,w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dw,dh in dwh:
solve(field,w+dw,h+dh)
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(field,w,h)
ans += 1
print ans |
s779310074 | p00741 | u633068244 | 1399528922 | Python | Python | py | Runtime Error | 0 | 0 | 448 | dwh = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]
def solve(field,w,h):
if 0 <= w < W and 0 <= h < H and field[h][w] == 1:
field[h][w] = 0
for dw,dh in dwh:
solve(field,w+dw,h+dh)
return
while 1:
W,H = map(int,raw_input().split())
if W == 0: break
field = [map(int,raw_input().split()) for i in range(H)]
ans = 0
for h in range(H):
for w in range(W):
if field[h][w] == 1:
solve(field,w,h)
ans += 1
print ans |
s316429501 | p00742 | u509278866 | 1535079648 | Python | Python3 | py | Runtime Error | 0 | 0 | 2146 | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
def f(n):
ss = [S() for _ in range(n)]
cs = set()
nz = set()
for s in ss:
cs |= set([c for c in s])
if len(s) > 1:
nz.add(s[0])
l = len(cs)
ci = {}
cw = [0] * l
for c,i in zip(cs, range(l)):
ci[c] = i
if c in nz:
nz.add(i)
for s in ss[:-1]:
w = 1
for c in s[::-1]:
cw[ci[c]] += w
w *= 10
w = 1
for c in ss[-1][::-1]:
cw[ci[c]] -= w
w *= 10
r = 0
lnz = l-1 not in nz
cw1 = cw[-1]
for a in itertools.permutations(range(10), l-1):
if 0 in a and a.index(0) in nz:
continue
tw = 0
for i in range(l-1):
tw += cw[i] * a[i]
if tw == 0:
if cw1 == 0:
for i in range(1, 10):
if i not in a:
r += 1
if lnz and 0 not in a:
r += 1
continue
if cw1 == 0 or tw % cw1:
continue
tk = -tw // cw1
if 0 < tk < 10 and tk not in a:
r += 1
return r
while True:
n = I()
if n == 0:
break
rr.append(f(n))
return '\n'.join(map(str, rr))
print(main())
|
s678376944 | p00742 | u082657995 | 1556804634 | Python | Python3 | py | Runtime Error | 0 | 0 | 844 | from collections import defaultdict
from itertools import permutations
while True:
N = int(input())
if N==0:
break
non_zero = set()
dd = defaultdict(int)
for _ in range(N-1):
s = input()
for i, c in enumerate(s[::-1]):
dd[c] += 10**i
if len(s) != 1:
non_zero.add(s[0])
s = input()
for i, c in enumerate(s[::-1]):
dd[c] -= 10 ** i
if len(s) != 1:
non_zero.add(s[0])
#char = dd.keys()
#coef = dd.values()
ans = 0
for t in permutations(range(10), r=len(dd)):
sum_=0
for i, (v, (ch, co)) in enumerate(zip(t, dd.items())):
if v==0 and ch in non_zero:
break
else:
sum_ += v*co
else:
if sum_==0:
ans += 1
print(ans)
|
s659688797 | p00742 | u082657995 | 1556805259 | Python | Python3 | py | Runtime Error | 0 | 0 | 829 | from collections import defaultdict
from itertools import permutations
while True:
N = int(input())
if N==0:
break
non_zero = set()
dd = defaultdict(int)
for _ in range(N-1):
s = input()
for i, c in enumerate(s[::-1]):
dd[c] += 10**i
if len(s) != 1:
non_zero.add(s[0])
s = input()
for i, c in enumerate(s[::-1]):
dd[c] -= 10 ** i
if len(s) != 1:
non_zero.add(s[0])
#char = dd.keys()
#coef = dd.values()
ans = 0
for t in permutations(range(10), r=len(dd)):
sum_=0
for v, (ch, co) in zip(t, dd.items()):
if v==0 and ch in non_zero:
break
else:
sum_ += v*co
else:
if sum_==0:
ans += 1
print(ans)
|
s841751376 | p00742 | u082657995 | 1556810757 | Python | Python3 | py | Runtime Error | 0 | 0 | 1118 |
from collections import defaultdict
from itertools import permutations
import gc
while True:
N = int(input())
if N==0:
break
non_zero = set()
dd = defaultdict(int)
for _ in range(N-1):
s = input()
for i, c in enumerate(s[::-1]):
dd[c] += 10**i
if len(s) != 1:
non_zero.add(s[0])
s = input()
for i, c in enumerate(s[::-1]):
dd[c] -= 10 ** i
if len(s) != 1:
non_zero.add(s[0])
#char = dd.keys()
#coef = dd.values()
n_coef_zero = 0
coef_zero = []
for ch, co in dd.items():
if co < 0:
break
elif co==0:
n_coef_zero += 1
coef_zero.append(ch)
else:
if n_coef_zero!=len(dd):
print(0)
continue
gc.collect()
ans = 0
for t in permutations(range(10), r=len(dd)):
sum_ = 0
for v, (ch, co) in zip(t, dd.items()):
if v == 0 and ch in non_zero:
break
else:
sum_ += v * co
else:
ans += sum_ == 0
print(ans)
|
s427690322 | p00742 | u082657995 | 1556810988 | Python | Python3 | py | Runtime Error | 0 | 0 | 1119 | from collections import defaultdict
from itertools import permutations
while True:
N = int(input())
if N==0:
break
non_zero = set()
dd = defaultdict(int)
for _ in range(N-1):
s = input()
for i, c in enumerate(s[::-1]):
dd[c] += 10**i
if len(s) != 1:
non_zero.add(s[0])
s = input()
for i, c in enumerate(s[::-1]):
dd[c] -= 10 ** i
if len(s) != 1:
non_zero.add(s[0])
#char = dd.keys()
#coef = dd.values()
n_coef_zero = 0
coef_zero = []
for ch, co in dd.items():
if co < 0:
break
elif co==0:
n_coef_zero += 1
coef_zero.append(ch)
else:
if n_coef_zero!=len(dd):
print(0)
continue
ans = 0
dditems = list(dd.items())
for t in permutations(range(10), r=len(dd)):
sum_ = 0
for v, (ch, co) in zip(t, dditems):
if v == 0 and ch in non_zero:
break
else:
sum_ += v * co
else:
ans += sum_ == 0
print(ans)
|
s997893253 | p00742 | u082657995 | 1556811047 | Python | Python3 | py | Runtime Error | 0 | 0 | 1124 | from collections import defaultdict
from itertools import permutations
while True:
N = int(input())
if N==0:
break
non_zero = set()
dd = defaultdict(int)
for _ in range(N-1):
s = input()
for i, c in enumerate(s[::-1]):
dd[c] += 10**i
if len(s) != 1:
non_zero.add(s[0])
s = input()
for i, c in enumerate(s[::-1]):
dd[c] -= 10 ** i
if len(s) != 1:
non_zero.add(s[0])
#char = dd.keys()
#coef = dd.values()
n_coef_zero = 0
coef_zero = []
for ch, co in dd.items():
if co < 0:
break
elif co==0:
n_coef_zero += 1
coef_zero.append(ch)
else:
if n_coef_zero!=len(dd):
print(0)
continue
ans = 0
dditems = list(dd.items())
for t in permutations(range(10), r=len(dditems)):
sum_ = 0
for v, (ch, co) in zip(t, dditems):
if v == 0 and ch in non_zero:
break
else:
sum_ += v * co
else:
ans += sum_ == 0
print(ans)
|
s440846598 | p00742 | u260980560 | 1501168743 | Python | Python3 | py | Runtime Error | 40000 | 8024 | 893 | from collections import deque
def solve(n, S):
C = {}; E = {}
for i, s in enumerate(S):
v = 1
for c in reversed(s):
if c not in C:
C[c] = 1
if i < n-1:
E[c] = E.get(c, 0) + v
else:
E[c] = E.get(c, 0) - v
v *= 10
if len(s) > 1:
C[s[0]] = 0
K = sorted(C, key=lambda x: C[x])
def dfs(c, D):
if c == len(K):
return sum(D.values()) == 0
res = 0
k = K[c]
for i in range(10-c):
v = deq.popleft()
if v or C[k]:
D[k] = v * E[k]
res += dfs(c+1, D)
deq.append(v)
return res
return dfs(0, {})
deq = deque(range(10))
while 1:
n = int(input())
if n == 0:
break
S = [input() for i in range(n)]
print(solve(n, S)) |
s670816197 | p00744 | u871901071 | 1405335538 | Python | Python3 | py | Runtime Error | 39860 | 98528 | 2125 | from collections import deque
inf = 1000000000
def bfs(graph, s, t):
V = len(graph)
q = deque([(s, -1, -1)])
prev = [(-1, -1)] * V
visited = [False] * V
while len(q) > 0:
(v, p, e) = q.popleft()
if visited[v]:
continue
visited[v] = True
prev[v] = p, e
for i in range(len(graph[v])):
e = graph[v][i]
if e['cap'] > 0:
q.append((e['to'], v, i))
if not visited[t]:
return None
else:
return prev
def decrease_graph(graph, s, t, prev):
((p, e), c) = prev[t], t
m = inf
while p != -1:
m = min(graph[p][e]['cap'], m)
((p, e), c) = prev[p], p
((p, e), c) = prev[t], t
while p != -1:
graph[p][e]['cap'] -= m
graph[c][graph[p][e]['rev']]['cap'] += m
((p, e), c) = prev[p], p
return m
def maximum_flow(graph, s, t):
V = len(graph)
g = [[] for _ in range(V)]
for v in range(V):
nei = graph[v]
for (u, cap) in nei:
g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
g[u].append({'to' : v, 'from': u, 'cap': 0, 'rev': len(g[v]) - 1})
sum = 0
while True:
prev = bfs(g, s, t)
if prev == None:
break
sum += decrease_graph(g, s, t, prev)
return sum
def gcd(x, y):
while y > 0:
x, y = y, x % y
return x
def main():
while True:
m, n = map(int, input().split())
if m == 0 and n == 0:
return
b = []
while len(b) < m:
b += list(map(int, input().split()))
r = []
while len(r) < n:
r += list(map(int, input().split()))
g = [[] for i in range(n + m + 2)]
for i in range(m):
g[0].append((i + 1, 1))
for i in range(n):
g[m + i + 1].append((n + m + 1, 1))
for i in range(m):
for j in range(n):
if gcd(b[i], r[j]) > 1:
g[i + 1].append((m + j + 1, 1))
print(maximum_flow(g, 0, n + m + 1))
main() |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.