-
Notifications
You must be signed in to change notification settings - Fork 0
/
d5.py
521 lines (423 loc) · 11.9 KB
/
d5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# BFS not use Queue
def BFS (V,Adj,s):
level = {s: 0}
parent = {s : None}
i = 1
frontier = [s] # previous level, i 1
while frontier:
next = [ ] # next level, i
for u in frontier:
for v in Adj [u]:
if v not in level: # not yet seen
level[v] = i # level[u] + 1
parent[v] = u
next.append(v)
frontier = next
i += 1
import queue
# # Breadth First Search: Shortest Reach
# q = int(input())
# def bfs_shortest_reach(q):
# for i in range(q):
# nm = list(map(int, input().split()))
# n = nm[0]
# m = nm[1]
# visited = [False for index in range(n + 1)]
# dist = [-1 for index in range(n + 1)]
# adj_list = [[] for index in range(n + 1)]
# for j in range(m):
# e1, e2 = map(int, input().split())
# adj_list[e1].append(e2)
# adj_list[e2].append(e1)
# q = queue.Queue()
# start = int(input())
# visited[start] = True
# q.put(start)
# dist[start] = 0
# while not q.empty():
# u = q.get()
# for v in adj_list[u]:
# if (not visited[v]):
# visited[v] = True
# q.put(v)
# dist[v] = dist[u] + 6
# result = ''
# for j in range(1, len(dist)):
# if (j != start):
# result = result + str(dist[j]) + " "
# print(result)
# bfs_shortest_reach(q)
# Breadth First Search: Shortest Reach
# q = int(input())
# def bfs_shortest_reach(q):
# for i in range(q):
# nm = list(map(int, input().split()))
# n = nm[0]
# m = nm[1]
# visited = [False for index in range(n + 1)]
# dist = [-1 for index in range(n + 1)]
# adj_list = [[] for index in range(n + 1)]
# for j in range(m):
# e1, e2 = map(int, input().split())
# adj_list[e1].append(e2)
# adj_list[e2].append(e1)
# q = queue.Queue()
# start = int(input())
# visited[start] = True
# q.put(start)
# dist[start] = 0
# while not q.empty():
# u = q.get()
# for v in adj_list[u]:
# if (not visited[v]):
# visited[v] = True
# q.put(v)
# dist[v] = dist[u] + 6
# result = ''
# for j in range(1, len(dist)):
# if (j != start):
# result = result + str(dist[j]) + " "
# print(result)
# bfs_shortest_reach(q)
# # Validate The Maze
# def validate_the_maze(m, n, case, matrix):
# len_case = len(case)
# sx, sy = 0, 0 # first maze gate
# ex, ey = 0, 0 # second maze gate
# max_openings = 0 # it must equal 2
# # get two gate
# for i in range(len_case):
# p1, p2 = case[i]
# if (p1 == 0 or p2 == 0 or p1 == m - 1 or p2 == n - 1):
# max_openings += 1
# if (max_openings > 2):
# print("invalid")
# return
# else:
# if (sx == 0 and sy == 0):
# sx = p1
# sy = p2
# else:
# ex = p1
# ey = p2
# if (max_openings < 2):
# print("invalid")
# return
# # # visited point
# vis = [[False for j in range(n)] for i in range(m)]
# vis[sx][sy] = True
# # queue for store current point
# q = queue.Queue()
# q.put((sx, sy))
# while not q.empty():
# x, y = q.get()
# # points right arround current point
# arrounds = []
# arrounds.append((x + 1, y))
# arrounds.append((x, y + 1))
# arrounds.append((x - 1, y))
# arrounds.append((x, y - 1))
# for arround in arrounds:
# a_x, a_y = arround
# # check if point inside matrix
# if (a_x >= 0 and a_y >= 0 and a_x < m and a_y < n):
# # check if point not visited and is posible move
# if ((not vis[a_x][a_y]) and matrix[a_x][a_y] == '.'):
# vis[a_x][a_y] = True
# q.put(arround)
# if (vis[ex][ey]):
# print('valid')
# else:
# print('invalid')
# # Input
# t = int(input())
# cases = []
# matrixs = []
# for i in range(t):
# mn = list(map(int, input().split()))
# m = mn[0]
# n = mn[1]
# case = []
# matrix = [[] for index in range(m)]
# for j in range(m):
# row = input()
# row = [x for x in row]
# matrix[j] = row
# for k in range(len(row)):
# if (row[k] == '.'):
# case.append((j, k))
# cases.append(case)
# matrixs.append(matrix)
# # Run and output
# validate_the_maze(m, n, cases[i], matrixs[i])
# # Dhoom 4
# def dhoom_4(s_key, b_secret_key, num_b_keys, b_keys):
# MAX = 100000 + 1
# dist = [-1] * MAX
# q = queue.Queue()
# q.put(s_key)
# dist[s_key] = 0
# while not q.empty():
# u = q.get()
# for key in b_keys:
# new_key = (u * key) % 100000
# if dist[new_key] == -1:
# dist[new_key] = dist[u] + 1
# q.put(new_key)
# if (new_key == b_secret_key):
# print(dist[b_secret_key])
# return
# print(dist[b_secret_key])
# # Input
# ab = list(map(int, input().split()))
# s_key = ab[0]
# b_secret_key = ab[1]
# num_b_keys = int(input())
# b_keys = list(map(int, input().split()))
# # Run and output
# dhoom_4(s_key, b_secret_key, num_b_keys, b_keys)
# # Guilty Prince
# def guilty_prince(m, n, start, matrixs, index):
# # m is number of row
# # n is number of column
# sx, sy = start # first maze gate
# # # visited point
# vis = [[False for j in range(n)] for i in range(m)]
# vis[sx][sy] = True
# # queue for store current point
# q = queue.Queue()
# q.put((sx, sy))
# result = 1
# while not q.empty():
# x, y = q.get()
# # points right arround current point
# arrounds = []
# arrounds.append((x + 1, y))
# arrounds.append((x, y + 1))
# arrounds.append((x - 1, y))
# arrounds.append((x, y - 1))
# for arround in arrounds:
# a_x, a_y = arround
# # check if point inside matrix
# if (a_x >= 0 and a_y >= 0 and a_x < m and a_y < n):
# # check if point not visited and is posible move
# if ((not vis[a_x][a_y]) and matrix[a_x][a_y] == '.'):
# vis[a_x][a_y] = True
# result += 1
# q.put(arround)
# result = 'Case ' + str(index) + ': ' + str(result)
# print(result)
# # Input
# t = int(input())
# matrixs = []
# starts = []
# for i in range(t):
# mn = list(map(int, input().split()))
# m = mn[1]
# n = mn[0]
# matrix = [[] for index in range(m)]
# for j in range(m):
# row = input()
# row = [x for x in row]
# matrix[j] = row
# for k in range(len(row)):
# if (row[k] == '@'):
# starts.append((j, k))
# break
# matrixs.append(matrix)
# # Run and output
# guilty_prince(m, n, starts[i], matrixs[i], i + 1)
# # Kefa and Park
# def kefa_and_park(n, m, a, edges):
# MAX = 100000 + 5
# cat = [0] * MAX
# visited = [False] * MAX
# graph = [[] for _ in range(MAX)]
# for edge in edges:
# u, v = edge
# graph[u - 1].append(v - 1)
# graph[v - 1].append(u - 1)
# restaurants = 0
# q = queue.Queue()
# visited[0] = True
# q.put(0)
# cat[0] = (1 if a[0] == 1 else 0)
# while not q.empty():
# u = q.get()
# for v in graph[u]:
# if not visited[v]:
# visited[v] = True
# if a[v] == 1:
# cat[v] = cat[u] + 1
# # Number of cat <= max
# if cat[v] <= m:
# # Check if v is a leaf aka vertex has no children
# if len(graph[v]) == 1:
# restaurants += 1
# # Go down to the leaf of v
# else:
# q.put(v)
# print(restaurants)
# # Input
# nm = list(map(int, input().split()))
# n = nm[0] # the number of vertices of the tree
# m = nm[1] # the maximum number of consecutive vertices with cats that is still ok for Kefa.
# a = list(map(int, input().split()))
# edges = []
# for i in range(n - 1):
# edge = list(map(int, input().split()))
# edge = (edge[0], edge[1])
# edges.append(edge)
# # Run and output
# kefa_and_park(n, m, a, edges)
# Slick
def is_valid_point (ax, ay, n, m):
return ax >= 0 and ay >= 0 and ax < n and ay < m
def slick(n, m, i, j, matrix, result):
q = queue.Queue()
q.put((i, j))
spilled_oil = 0
if matrix[i][j] == 1:
spilled_oil += 1
matrix[i][j] = 0
while not q.empty():
x, y = q.get()
arrounds = []
arrounds.append((x + 1, y))
arrounds.append((x, y + 1))
arrounds.append((x - 1, y))
arrounds.append((x, y - 1))
for arround in arrounds:
ax, ay = arround
if (not is_valid_point(ax, ay, n, m)):
continue
if matrix[ax][ay] == 1:
q.put((ax, ay))
spilled_oil += 1
matrix[ax][ay] = 0
if spilled_oil != 0:
if spilled_oil in results:
results[spilled_oil] += 1
else:
results[spilled_oil] = 1
# Input
while True:
n, m = map(int, input().split())
if (n == 0 and m == 0):
break
results = {}
matrix = [[] for index in range(n)]
for i in range(n):
row = list(map(int, input().split()))
matrix[i] = row
for i in range(n):
for j in range(m):
if (matrix[i][j] != 0):
slick(n, m, i, j, matrix, results)
# Output
number_of_slicks = 0
values = results.values()
number_of_slicks = sum(values)
print(number_of_slicks)
if number_of_slicks > 0:
results = {size:num for size, num in sorted(results.items())}
for size, num in results.items():
print(size, num)
# # Ice Cave
# def is_valid_point (ax, ay, n, m):
# return ax >= 0 and ay >= 0 and ax < n and ay < m
# def ice_cave(n, m, s, e, matrix):
# q = queue.Queue()
# q.put(s)
# sx, sy = s
# ex, ey = e
# matrix[sx][sy] = 'X'
# while not q.empty():
# x, y = q.get()
# # points right arround current point
# arrounds = []
# arrounds.append((x + 1, y))
# arrounds.append((x, y + 1))
# arrounds.append((x - 1, y))
# arrounds.append((x, y - 1))
# for arround in arrounds:
# ax, ay = arround
# if (ax == ex and ay == ey and matrix[ax][ay] == 'X'):
# print('YES')
# return
# if (is_valid_point(ax, ay, n, m) and matrix[ax][ay] == '.'):
# matrix[ax][ay] = 'X'
# q.put(arround)
# print('NO')
# # Input
# matrixs = []
# nm = list(map(int, input().split()))
# n = nm[0] # row
# m = nm[1] # column
# matrix = [[] for index in range(n)]
# for j in range(n):
# row = input()
# row = [x for x in row]
# matrix[j] = row
# s_input = list(map(int, input().split()))
# s = (s_input[0] - 1, s_input[1] - 1)
# e_input = list(map(int, input().split()))
# e = (e_input[0] - 1, e_input[1] - 1)
# # Run and output
# ice_cave(n, m, s, e, matrix)
# Sheep
def is_valid_point (ax, ay, n, m):
return ax >= 0 and ay >= 0 and ax < n and ay < m
def sheep(n, m, sx, sy, matrix):
global survived_sheep, survived_wolf
q = queue.Queue()
q.put((sx, sy))
current_sheep = (1 if matrix[sx][sy] == 'k' else 0)
current_wolf = (1 if matrix[sx][sy] == 'v' else 0)
connected_outside = False
matrix[sx][sy] = '#'
while not q.empty():
x, y = q.get()
if (x == 0 or x == n - 1 or y == 0 or y == m - 1):
connected_outside = True
# points right arround current point
arrounds = []
arrounds.append((x + 1, y))
arrounds.append((x, y + 1))
arrounds.append((x - 1, y))
arrounds.append((x, y - 1))
for arround in arrounds:
ax, ay = arround
if not is_valid_point(ax, ay, n, m):
connected_outside = True
continue
if (matrix[ax][ay] != '#'):
current_sheep += (1 if matrix[ax][ay] == 'k' else 0)
current_wolf += (1 if matrix[ax][ay] == 'v' else 0)
matrix[ax][ay] = '#'
q.put(arround)
if connected_outside:
survived_sheep += current_sheep
survived_wolf += current_wolf
else:
if current_sheep > current_wolf:
survived_sheep += current_sheep
else:
survived_wolf += current_wolf
# Input
nm = list(map(int, input().split()))
n = nm[0] # row
m = nm[1] # column
matrix = [[] for index in range(n)]
for j in range(n):
row = input()
row = [x for x in row]
matrix[j] = row
survived_sheep = 0
survived_wolf = 0
for i in range(n):
for j in range(m):
if (matrix[i][j] != '#'):
sheep(n, m, i, j, matrix)
# Run and output
print(survived_sheep, survived_wolf)