63_1_Sec5_Q3 (49)
# 6331304421 1295946 (2020-12-04 22:03) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output #################################################################################### # read requirement about type of island require_coord = input() if require_coord.upper() == 'Y': require_type = True else: require_type = False # read model width, length, height = tuple([int(i) for i in input().split(',')]) model = list() for i in range(height): floor = list() for _ in range(length): line = list() x = input() for block in x: if block.lower() == 'x': line.append(1) else: line.append(0) floor.append(line) model.append(to_cluster(np.array(floor, np.bool))) # for calculating island previous_floor = np.array([[1] * width] * length, np.bool) blank_floor = np.array([[0] * width] * length, np.bool) have_block_below = np.array([[0] * width] * length, np.bool) # list of islands island_all = list() # count height h = 0 for merged_floor in model: islands = list() for piece in merged_floor: # merge if none of them have supporting block below them if not np.logical_and(piece, previous_floor).any(): # saving coordinate for i in range(length): for j in range(width): # extracting coord and save it if piece[i][j]: islands.append([h, i, j]) # sort islands first islands.sort() # combine all piece and then it becomes new previous floor full_floor = blank_floor for piece in merged_floor: full_floor = np.logical_or(full_floor, piece) # full floor is required before finding the type of island # specify type of island if require_type: i_type = list() m_type = list() for order in range(len(islands)): i = islands[order][1] j = islands[order][2] if full_floor[i][j] and have_block_below[i][j]: typed_island = islands[order][0:1] + ['I'] + islands[order][1:] i_type.append(typed_island) else: typed_island = islands[order][0:1] + ['M'] + islands[order][1:] m_type.append(typed_island) # merge i_type with island_all first for i in i_type: island_all.append(i) # then m_type for i in m_type: island_all.append(i) # case not required_type else: for i in islands: island_all.append(i) # updating some var previous_floor = full_floor have_block_below = np.logical_or(have_block_below, full_floor) # add h by 1 h += 1 if island_all: for isle in island_all: isle = [str(e) for e in isle] print(','.join(isle)) else: print('There is no island') ''' Y 3,3,3 --- -x- --- --x xxx --- xxx --- xxx ''' ''' Y 2,2,3 x- -- -- -x -- x- ''' |
# 6331306721 1296389 (2020-12-06 18:59) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index - 1]) or ( col_index < width and a[row_index][col_index - 1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index - 1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output act = input() x, y, h = [int(i) for i in input().split(',')] mat = [[[1 if i == 'x' else 0 for i in input()] for _ in range(y)] for __ in range(h)] if h == 1: print('There is no island') exit() result = [] for l in range(1, h): upper = np.array(mat[l], np.bool) lower = np.array(mat[l - 1], np.bool) for up in np.array(to_cluster(upper)): if not(np.any(up & lower)): for i, row in enumerate(up): for j, col in enumerate(row): if col == 1: if 1 in [mat[floor][i][j] for floor in range(l)]: result.append((l, 'I', i, j)) else: result.append((l, 'M', i, j)) if not result: print('There is no island') elif act == 'N': result.sort(key=lambda t: (t[0], t[2], t[3])) for l, island_type, i, j in result: print(str(l) + ',' + str(i) + ',' + str(j)) elif act == 'Y': result.sort(key=lambda t: (t[0], t[1], t[2], t[3])) for l, island_type, i, j in result: print(str(l) + ',' + str(island_type) + ',' + str(i) + ',' + str(j)) |
# 6331307321 1296174 (2020-12-05 12:09) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def pairing_pos(part): pos = np.where(part == True) out=[] for i in range(len(pos[0])): out.append((pos[0][i],pos[1][i])) return out def is_island(part,building,base,floor): out=[] if np.sum(part & building[floor-1]) == 0 : pos = pairing_pos(part) for x,y in pos: if base[x,y] == False: # need main support out.append((floor,'M',x,y)) else: # need inner support out.append((floor,'I',x,y)) return out key = input().strip() W,L,H = [int(e) for e in input().split(',')] building=[] part_in_building=[] for i in range (H): temp='' for j in range(L): temp+=input().strip() floor=np.array(list(temp)).reshape((L,W)) alike=[1 if x=='x' else 0 for x in temp] floor2 = np.array(alike,np.bool).reshape((L,W)) building.append(floor2) part_in_building.append(to_cluster(floor2)) checkbase=np.zeros((L,W),dtype=bool) all_island=[] for i in range(H): for part_floor in part_in_building[i]: if i != 0: all_island+=is_island(part_floor,building,checkbase,i) checkbase |= part_floor #output process if len(all_island) == 0: print('There is no island') elif key == 'N': temp=[] for floor,type_support,row,col in all_island: out = str(floor)+','+str(row)+','+str(col) temp.append([floor,row,col]) temp.sort() for floor,row,col in temp: print(str(floor)+','+str(row)+','+str(col)) elif key == 'Y': all_island.sort() for floor,type_support,row,col in all_island: out = str(floor)+','+type_support+','+str(row)+','+str(col) print(out) |
# 6331308021 1295857 (2020-12-04 20:48) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output showtype = input() W,L,H = [int(e) for e in input().split(',')] data = np.ndarray((H,L,W),np.bool) for i in range(H): for j in range(L): x = input() for k in range(W): if x[k] == 'x': data[i,j,k] = True else: data[i,j,k] = False islands = [] def checkclusterisland(data,W,L,i,e,showtype): island = [] for r in range(L): for c in range(W): if e[r,c] == True and data[i-1,r,c] == True: return #not island elif e[r,c] == True: if showtype == 'Y': if True in data[:i,r,c]: island.append([i,'I',r,c]) else: island.append([i,'M',r,c]) else: if True in data[:i,r,c]: island.append([i,r,c]) else: island.append([i,r,c]) return island for i in range(1,H): cluster = to_cluster(data[i,:,:]) for e in cluster: if checkclusterisland(data,W,L,i,e,showtype) != None: islands += (checkclusterisland(data,W,L,i,e,showtype)) islands.sort() if len(islands) == 0: print('There is no island') else: for e in islands: print(','.join([str(f) for f in e])) |
# 6331309621 1296579 (2020-12-09 00:20) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output #--------------------------------------------------------------------------------------------- cmd = input().strip().upper() w,l,h = [int(e) for e in input().strip().split(',')] arr = [] for i in range(h): toa = [] for j in range(l): k = input().strip() cv = [] for e in k: if e == 'x': cv.append(True) elif e == '-': cv.append(False) toa.append(cv) arr.append(toa) island = [] alb = [np.array(e) for e in arr ] new = [] k =[] for e in alb: new.append(to_cluster(e)) fin1 = [] all_ = [] for i in range(1,len(new)): gr =[] cl = [] for k in range(len(new[i])): li = [] is_island =[] for j in range(new[i][k].shape[0]): for t in range(new[i][k].shape[1]): if new[i][k][j,t] == True: if alb[i-1][j,t] == False : is_island.append(True) if i >= 2: checkI = 0 for q in range(i): if alb[q][j,t] == True: li.append((i,'I',j,t)) checkI = 1 break if checkI == 0: li.append((i,'M',j,t)) else: li.append((i,'M',j,t)) else: is_island.append(False) gr.append(li) cl.append(is_island) all_.append(gr) fin1.append(cl) fin_is = [] for i in range(len(fin1)): for j in range(len(fin1[i])): if False not in fin1[i][j]: fin_is.append(all_[i][j]) if len(fin_is) == 0: print('There is no island') elif cmd == 'N': re = [] for e in fin_is: for i in range(len(e)): re.append([e[i][0],e[i][2],e[i][3]]) re.sort() for i in range(len(re)): if re[i]!= []: print(str(re[i][0])+','+str(re[i][1])+','+str(re[i][2])) elif cmd == 'Y': re2 = [] for e in fin_is: for i in range(len(e)): re2.append(e[i]) re2.sort() for i in range(len(re2)): if re2[i]!= []: print(str(re2[i][0])+','+re2[i][1]+','+str(re2[i][2])+','+str(re2[i][3])) |
# 6331310121 1296543 (2020-12-08 22:10) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.zeros_like(a, bool) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(lcs[row_index - 1] & ccs[row_index]): if merged_to is None: ccs[:, :] = lcs | ccs # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = merged_to | ccs # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output # convert input to 3d numpy array type bool where True = 'x', False = '-' q = input() w, l, h = [int(e) for e in input().split(',')] raw_data = [] for i in range(l*h): raw_data += [1 if e == 'x' else 0 for e in list(input().strip())] new_data = np.array(raw_data, bool).reshape(h,l,w) # [sub] array containing highest height in each row,col default is -1 sub = np.ones((l,w), int) * (-1) # for each area in height 0: # make [sub] of the area equal to 0 sub[new_data[0]] = 0 # for i in range(1, h) # for each cluster: # if there is at least a [sub] in the area of that cluster that is i - 1: no sup needed # else: # for each area in the cluster: # if [sub] of that area is -1: main sup # else: inner sup # make [sub] of the area of that cluster equal to i output = [] for i in range(1, h): for cluster in to_cluster(new_data[i]): if i-1 not in sub[cluster]: for j in range(l): for k in range(w): if cluster[j,k]: sup_type = 'M' if sub[j,k] == -1 else 'I' output.append((str(i),sup_type,str(j),str(k))) sub[cluster] = i # sort the output by h, l, w if len(output) == 0: print("There is no island") elif q == 'N': output = sorted(output, key = lambda x:int(x[3])) output = sorted(output, key = lambda x:int(x[2])) output = sorted(output, key = lambda x:int(x[0])) for i in range(len(output)): print(output[i][0] + ',' + output[i][2] + ',' + output[i][3]) else: output = sorted(output, key = lambda x:int(x[3])) output = sorted(output, key = lambda x:int(x[2])) output = sorted(output, key = lambda x:x[1]) output = sorted(output, key = lambda x:int(x[0])) for i in range(len(output)): print(','.join(output[i])) |
# 6331311821 1296197 (2020-12-05 13:55) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def eachisland(block,lvl): d=[] row=block.shape[0] col=block.shape[1] for i in range(row): for j in range(col): if block[i][j]==True: d.append([lvl,i,j]) return d def yfunc(d,island): floor,row,col=int(island[0]),int(island[1]),int(island[2]) for i in range(floor-1,-1,-1): if d[i][row][col] == 'X' or d[i][row][col] == 1: return [floor,'I',row,col] return [floor,'M',row,col] func=input() w,l,h=[int(e) for e in input().split(',')] d=[] islander=[] for i in range(h): d1=[] for j in range(l): box=input() d1.append(list(box)) d.append(d1) # d=[[list('-----'),list('xxxxx'),list('-----')],[list('-----'),list('--x--'),list('-----')],[list('-----'),list('--x--'),list('--x--')],[list('-----'),list('xxx-x'),list('----x')],[list('xxxxx'),list('-----'),list('----x')]] if h==1: print('There is no island') else: for i in range(1,h): xt1=d for i2 in range(h): for j in range(l): for k in range(w): if d[i2][j][k]=='x': xt1[i2][j][k]=1 elif d[i2][j][k]=='-': xt1[i2][j][k]=0 a=np.array(xt1[i],np.bool) length=len(to_cluster(a)) #จำนวนกล่องในชั้นนั้นๆ for j in range(length): block=to_cluster(a)[j] ;c=0 #2d for row in range(l): for col in range(w): if block[row][col]==True and d[i-1][row][col]==1: c+=1 if c==0: someisland=eachisland(block,i) for q in someisland: islander.append(q) if func=='N': for e in sorted(islander): #islander=[[1,2,3],[1,3,2]] print(','.join([str(f) for f in e])) #e=[1,2,3] elif func=='Y': all_islander=[] if islander==[]: print('There is no island') else: all_islander = sorted([yfunc(d,e) for e in islander]) for e in all_islander: print(','.join([str(f) for f in e])) |
# 6331315321 1296643 (2020-12-09 19:45) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output s = str(input()) W, L, H = input().split(",") W = int(W); L = int(L); H = int(H) lst = [[[0 for i in range(W)] for j in range(L)] for k in range(H)] for i in range(H): for j in range(L): x = str(input()) for k in range(W): if x[k] == "x": lst[i][j][k] = 1 c = np.array(lst[0],np.bool) ans = [] if H != 1: for i in range(H-1): a = np.array(lst[i],np.bool) b = np.array(lst[i+1],np.bool) b2 = to_cluster(b) for j in b2: if True not in a&j: for x in range(L): for y in range(W): m = c&j if j[x][y] == True and m[x][y] == True: ans.append([i+1,"I",x,y]) elif j[x][y] == True: ans.append([i+1,"M",x,y]) c = c|b if len(ans) == 0: print("There is no island") elif s == "N": ans1 = [] for k in ans: ans1.append([k[0],k[2],k[3]]) ans1.sort() for j in ans1: print(str(j[0])+","+str(j[1])+","+str(j[2])) elif s == "Y": ans.sort() for k in ans: print(str(k[0])+","+str(k[1])+","+str(k[2])+","+str(k[3])) |
# 6331319921 1295868 (2020-12-04 21:06) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def check_island(b,a,L,W): for x in range(L): for y in range(W): if b[x][y] == 1 and a[i-1][x][y] == 1: return False return True command = input() W,L,H = [int(i) for i in input().split(',')] a = [] for i in range(H): b = [] for j in range(L): z = input() c = [] for k in range(len(z)): if z[k] == 'x': c.append(1) elif z[k] == '-' : c.append(0) b.append(c) a.append(b) a = np.array(a) split = [] for i in a: split.append(to_cluster(i)) ans = [] for i in range(1,len(split)): for j in split[i]: if check_island(j,a,L,W): if command == 'N': for x in range(L): for y in range(W): if j[x][y] == 1:ans.append([i,x,y]) elif command == 'Y': for x in range(L): for y in range(W): main = True if j[x][y] == 1 and a[0][x][y] == 0: for k in range(1,i-1): if a[k][x][y] == 1: main = False if main == True: ans.append([i,'M',x,y]) elif main == False : ans.append([i,'I',x,y]) elif j[x][y] == 1 and a[0][x][y] == 1: ans.append([i,'I',x,y]) ans.sort() if len(ans) == 0: print('There is no island') else: for i in ans: string = '' for j in i: string += str(j) + ',' string = string.strip(',') print(string) |
# 6331321021 1296529 (2020-12-08 21:13) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output Nans =[] ;Yans = [] mode = input() w,l,h = input().split(",") w = int(w) ; l = int(l) ; h = int(h) matrix = [] ; matrix2 = [] for i in range(h) : story = np.ndarray((l,w),bool) for j in range(l) : n = input().strip() s = "" for k in range(len(n)) : if n[k] == "-" : s += "0" elif n[k] == "x" : s += "1" for L in range(w) : story[j,L] = int(s[L]) use = to_cluster(story) matrix.append(use) ;matrix2.append(story) for i in range(1,len(matrix)) : for j in range(len(matrix[i])) : c = np.max(matrix2[i-1] & matrix[i][j]) if not c : for x in range(len(matrix[i][j])) : for y in range(len(matrix[i][j][x])) : if matrix[i][j][x][y] == 1 : Nans.append([int(i),int(x),int(y)]) for k in range(i) : found = False if np.max(matrix2[k][x][y]) == 1 and [int(i),"I",int(x),int(y)] not in Yans : Yans.append([int(i),"I",int(x),int(y)]) found = True ;break if [int(i),"M",int(x),int(y)] not in Yans : if not found : Yans.append([int(i),"M",int(x),int(y)]) Yans.sort() Nans.sort() Nans2 = [] ; Yans2 = [] for i in range(len(Nans)) : Nans2.append([str(Nans[i][0]),str(Nans[i][1]),str(Nans[i][2])]) for i in range(len(Yans)) : Yans2.append([str(Yans[i][0]),str(Yans[i][1]),str(Yans[i][2]),str(Yans[i][3])]) if len(Nans) == 0 : print("There is no island") elif mode == "N" : for m in Nans2 : print(",".join(m)) elif mode == "Y" : for m in Yans2 : print(",".join(m)) |
# 6331322721 1296059 (2020-12-05 00:25) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output solution = input() information = input().split(',') allbox = [] boola = True for i in range(int(information[2])) : box = [] for j in range(int(information[1])) : col = [] while len(col) != int(information[0]) : inn = input() for h in inn : if h == 'x' : col.append(1) elif h == '-' : col.append(0) box.append(col) allbox.append(box) numAllbox = [] island = [] for i in allbox : a = np.array(i) numAllbox.append(a) base = numAllbox[0] numAllbox_cluster = [] for i in numAllbox[1::] : b = to_cluster(i) numAllbox_cluster.append(b) for i in range(len(numAllbox_cluster)) : subisland = [] for j in numAllbox_cluster[i] : if 2 not in numAllbox[i]+j : subisland.append(j) island.append(subisland) printer = [] if solution == 'N' : n = 0 for i in range(len(island)) : if island[i] != [] : new = island[i] for j in range(int(information[1])) : for k in range(int(information[0])) : for l in new : if l[j][k] == 1 : print(str(i+1)+','+str(j)+','+str(k)) n+=1 if n == 0 : print('There is no island') if solution == 'Y' : n = 0 i = 0 for i in range(len(island)) : if island[i] != [] : new = island[i] for j in range(int(information[1])) : for k in range(int(information[0])) : for l in new : if l[j][k] == 1 : x = j y = k f = i+1 count = 0 for m in range(f) : count = count + numAllbox[m][x][y] if count >=1 : sedsaktee = 'I' else : sedsaktee = 'M' printer.append([int(i+1),sedsaktee,int(j),int(k)]) n+=1 if n == 0 : print('There is no island') printer.sort() if n != 0 and printer != []: for i in printer : yayy = '' for j in i : if yayy == '' : yayy += str(j) else : yayy = yayy + ',' + str(j) print(yayy) |
# 6331323321 1296479 (2020-12-07 10:03) # 6331323321 ทำเองแน่นอน 100% import numpy as np import copy def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output cmd = input().strip() W,L,H = input().split(',') W,L,H = int(W),int(L),int(H) body = np.zeros((H,W,L),bool) # สร้าง array แทนรูปชิ้นงานจากข้อมูลนำเข้า for h in range(H): plane = np.zeros((W,L),bool) for l in range(L): a = list(input().strip()) for w in range(W): if a[w] == 'x': a[w] = 1 elif a[w] == '-': a[w] = 0 plane[:,l] = np.array(a) body[h,:,:] = plane[:,:] ################################################## support = [] stick = {} # สร้าง stick (หมายถึงโครงสร้างทุกชั้นในแต่ละ row และ column ของชิ้นงาน) for l in range(L): for w in range(W): stick[(l,w)]=body[:,w,l] ################################################## # ไล่ทำงานทีละชั้นจากล่างขึ้นบน (ไม่นับชั้นล่างสุด) clusters = {} clusters2 = {} for h in range(1,H): cluster = to_cluster(body[h]) # เปลี่ยน array ทั้งหมดเป็นลิสต์เพื่อนำไปเปรียบเทียบต่อไป for i in range(len(cluster)): cluster[i] = list(cluster[i]) for j in range(len(cluster[i])): cluster[i][j] = list(cluster[i][j]) clusters[h] = copy.deepcopy(cluster) clusters2[h] = copy.deepcopy(cluster) # เช็ค cluster ต่างๆในชั้นนั้นๆ ว่ามี island หรือไม่ for c in clusters[h]: for w in range(W): for l in range(L): to_break = False # เช็คสำหรับทุก block ที่มีชิ้นงาน if c[w][l] and stick[(l,w)][:h][-1]: # ถ้ามีโครงสร้างมารองรับ cluster อยู่แล้วถือว่าไม่มี island แน่นอน # ลบ cluster นั้นออกเพราะไม่ต้องการ support clusters2[h].remove(c) to_break = True break if to_break: break for h in range(1,H): # เก็บข้อมูล block ที่มี island for c in clusters2[h]: for w in range(W): for l in range(L): # เช็คสำหรับทุก block ที่มีชิ้นงาน if c[w][l]: b = list(stick[(l,w)][:h]) # ถ้าข้างล่างของ block นั้นไม่มีชิ้นงานเลย แสดงว่า block นั้นต้องการ main support if b == [False]*len(b): support.append([h,'M',l,w]) # ถ้าข้างล่างของ block ยังมีช่องว่างบ้าง หรือชิ้นงานบ้าง แสดงว่า block นั้นต้องการ inner support elif False in b: support.append([h,'I',l,w]) support.sort() if cmd == 'Y': if not support: print('There is no island') else: for t in support: print(','.join([str(e) for e in t])) elif cmd == 'N': if not support: print('There is no island') else: for t in support: t.pop(1) support.sort() for t in support: print(','.join([str(e) for e in t])) |
# 6331325621 1288253 (2020-11-17 10:59) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output tyn=input() w,l,h=[ int(e) for e in input().strip().split(',')] build=[] islandlist=[] for i in range(h): layer=[] for j in range(l): row=[] x=input() for k in x: if k=='x':row.append(1) elif k=='-':row.append(0) layer.append(row) build.append(layer) for la in range(1,len(build)): cluster=to_cluster(np.array(build[la])) for piece in cluster: island=True for i in range(l): for j in range(w): if piece[i][j]==True: if build[la-1][i][j]==True: island=False break if island==False:break else: for i in range(l): for j in range(w): if piece[i][j]==True: islandlist.append([la,i,j]) if tyn=='Y':#position and type out=[] for i in range(len(islandlist)): block=islandlist[i] for lay in range(block[0]-1): if build[lay][block[1]][block[2]]: out.append([block[0]]+['I']+block[1:]) break else: out.append([block[0]]+['M']+block[1:]) if out: for i in sorted(out): print(','.join([str(e)for e in i])) else: print('There is no island') elif tyn=='N': #postition if islandlist: for i in sorted(islandlist): print(','.join([str(e)for e in i])) else: print('There is no island') |
# 6331326221 1296344 (2020-12-06 16:26) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output Q = input() w,l,h = input().split(',') area = [] areaa = [] xx = [] for i in range(int(l)*int(h)): a = input() for j in a: if j=='x': xx.append(True) else: xx.append(False) areaa.append(xx) xx = [] if (i+1)%int(l)==0: # 1 H areaa = np.array(areaa) areaa = to_cluster(areaa) area.append(areaa) areaa = [] count = 0 for i in range(1,int(h)): finali = [] finalm = [] final = [] for u in area[i]: #upblock found = False for uu in area[i-1]: #lowblock for j in range(int(l)): for k in range(int(w)): if u[j,k]==uu[j,k]==True: found = True if found == False: for jj in range(int(l)): for kk in range(int(w)): if u[jj,kk]==True: fou = False for x in range(i): for y in area[x]: if y[jj,kk]==True: fou = True if Q=='Y': if fou==True: finali.append((jj,kk)) count += 1 else: finalm.append((jj,kk)) count += 1 elif Q=='N': if fou==True: final.append((jj,kk)) count += 1 else: final.append((jj,kk)) count += 1 finali.sort() for asdf in finali: print(str(i)+','+'I'+','+str(asdf[0])+','+str(asdf[1])) finalm.sort() for asdf in finalm: print(str(i)+','+'M'+','+str(asdf[0])+','+str(asdf[1])) final.sort() for asdf in final: print(str(i)+','+str(asdf[0])+','+str(asdf[1])) if count==0: print('There is no island') |
# 6331327921 1296680 (2020-12-09 23:21) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def check_island(e): for w in range(q[0]) : for l in range(q[1]) : if e[l,w] == True and kept2[i-1][l][w] == True : return False return True def find_position(e,i) : position = [] for w in range(q[0]) : for l in range(q[1]) : if e[l,w] == True and kept2[i-1][l][w] == False : position.append([i,l,w]) return position #-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- p = input() qq = input().split(',') #w,L,H q = [] for e in qq : q.append(int(e)) k = [] kept1 = [] kept2 = [] kept = [] position2 = [] for i in range(q[1]*q[2]) : x = input() x =x.replace("-","0") x =x.replace("x","1") for e in x : k.append(int(e)) kept1.append(k) k = [] if i%q[1] == q[1]-1 : kept2.append(kept1) kept1 = [] for i in range(len(kept2)) : a = np.array(kept2[i],np.bool) kept.append(np.array(to_cluster(a))) for i in range(1,len(kept)) : for e in kept[i] : if check_island(e) == True : for k in find_position(e,i): position2.append(k) if len(position2) == 0 : print("There is no island") elif p == "N" : position2.sort() for e in position2 : print(str(e[0])+","+str(e[1])+","+str(e[2])) elif p == "Y" : for i in position2 : check = 0 for j in range(i[0]): if kept2[j][i[1]][i[2]] == True : i.insert(1,"I") check = 1 break if check == 0 : i.insert(1,"M") position2.sort() for e in position2 : print(str(e[0])+","+str(e[1])+","+str(e[2])+","+str(e[3])) |
# 6331329121 1296293 (2020-12-05 20:39) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def changeto01(name): #change x- to 01 final = [] for i in name: info = [] for j in i: if j == '-': info.append(0) else: info.append(1) final.append(info) return final mode = input() w,l,h = [int(i) for i in input().split(',')] test = [] #first floor for i in range(l): test.append(input().strip()) floorbase = changeto01(test) #overall when look from above (to check which type of island it is) floorbefore = changeto01(test) floorcurrent = [] island = [] for i in range(h-1): test = [] #next floor for j in range(l): test.append(input().strip()) floorcurrent = changeto01(test) x = to_cluster(np.array(floorcurrent)) #divide into sections for k in x: k = k.tolist() check = False for pos1 in range(len(k)): #row for pos2 in range(len(k[0])): #column if (floorbefore[pos1][pos2] == 1) and (k[pos1][pos2] == 1): #check if this floor have support or not check = True if check == False: #check which type of island it is when compared to floorbase for pos1 in range(len(k)): for pos2 in range(len(k[0])): if k[pos1][pos2] == 1: if floorbase[pos1][pos2] == 0: island.append((i+1,pos1,pos2,',M,')) floorbase[pos1][pos2] = 1 else: island.append((i+1,pos1,pos2,',I,')) for pos1 in range(len(k)): for pos2 in range(len(k[0])): if (k[pos1][pos2] == 1) and (floorbase[pos1][pos2] == 0): floorbase[pos1][pos2] = 1 #add base floorbefore = changeto01(test) #change this floor to floor before #----------print answer--------------- if len(island) == 0: print('There is no island') else: if mode == 'N': island = sorted(island) for i in island: print(str(i[0]) + ',' + str(i[1]) + ',' + str(i[2])) else: island = sorted(island, key = lambda x: (x[0],x[3])) for i in island: print(str(i[0]) + str(i[3]) + str(i[1]) + ',' + str(i[2])) |
# 6331330721 1296615 (2020-12-09 13:44) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output cmd = input().strip() W,L,H = [int(e) for e in input().strip().split(',')] mo = [[[0 for k in range(W)] for j in range(L)] for i in range(H)] t = {'-':0,'x':1} for i in range(H): for j in range(L): l = input().strip() for k in range(W): mo[i][j][k] = t[l[k]] Yresult = [] result = [] base = [[0 for j in range(W)] for i in range(L)] for i in range(1,len(mo)): layer_before = mo[i-1] layer = mo[i] for peice in to_cluster(np.array(layer,np.bool)): peice = list(peice) ok = False pos = [] for l in range(len(peice)): for w in range(len(peice[l])): if peice[l][w]: if layer_before[l][w] == 1: ok = True break else: pos.append((l,w)) if ok: break if not ok: for l,w in pos: if cmd == 'Y': if base[l][w] == 0: Yresult.append([i,'M',l,w]) else: Yresult.append([i,'I',l,w]) elif cmd == 'N': result.append([i,l,w]) for j in range(L): for k in range(W): base[j][k] += layer_before[j][k] Yresult.sort() result.sort() if len(result) == 0 and len(Yresult) == 0: print('There is no island') else: if cmd == 'N': for i,l,w in result: print(','.join([str(i),str(l),str(w)]).strip()) elif cmd == 'Y': for i,t,l,w in Yresult: print(','.join([str(i),t,str(l),str(w)]).strip()) |
# 6331332021 1296526 (2020-12-08 20:20) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output #-----------------------------------------------------------------# method = input() W,L,H = [int(e) for e in input().split(',')] data = [] data2 = [] for i in range(H): a = np.ndarray((L,W),np.bool) for j in range(L): b = input() for e in range(len(b)): if b[e] == 'x': b = b[:e] + '1' + b[e+1:] elif b[e] == '-': b = b[:e] + '0' + b[e+1:] for k in range(W): a[j,k] = int(b[k]) #to_cluster(a) data.append(to_cluster(a)) # list = [ [numpy,numpy] , [numpy,numpy] ] data2.append(a) #---check island---# haveisland = False support = 'M' outy = [] outn = [] for i in range(1,H): #floor for e in data[i]: block = [] z = np.where(e == True) z0 = z[0].tolist() z1 = z[1].tolist() for j in range(len(z0)): block.append([z0[j],z1[j]]) Thisgroupis_island = True for r,c in block: if data2[i-1][r,c] == True: Thisgroupis_island = False break if Thisgroupis_island: for r,c in block: if data2[i-1][r,c] == False: haveisland = True for k in range(i-2,-1,-1): # previous floor if data2[k][r,c] == True: support = 'I' break outn.append([i,r,c]) outy.append([i,support,r,c]) support = 'M' outn.sort() outy.sort() if not haveisland: print('There is no island') else: if method == 'N': realn = [] for i,r,c in outn: realn.append([str(i),str(r),str(c)]) for e in realn: print(','.join(e)) elif method == 'Y': realy = [] for i,sup,r,c in outy: realy.append([str(i),sup,str(r),str(c)]) for e in realy: print(','.join(e)) |
# 6331333621 1296342 (2020-12-06 16:07) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output typee=input() x=input().split(",") c=[] b=[] for i in range(int(x[1])*int(x[2])): a=input() aa=[] for j in a: if j=="x": aa.append(1) else: aa.append(0) b.append(aa) if i%int(x[1])==int(x[1])-1: c.append(b) b=[] #print(c) final=[] for i in c: d=np.array(i,np.bool) dd=to_cluster(d) #print(d) final.append(dd) #print(final) #print(final[0]) #print(final[0][0]) breaking=False ans=[] anss=[] for i in range(1,len(final)):#แต่ละชั้น ไม่นับชั้นแรกสุด ans=[] for j in range(len(final[i])):#แต่ละก้อน(ที่แยกกันแล้ว)ในชั้นนั้นๆ breaking=False ans=[] for k in range(int(x[0])): if breaking: break for kk in range(int(x[1])): #print(final[i][j][k][kk]) #print(ans) if final[i][j][kk][k]==True: if c[i-1][kk][k]==1: ans=[] breaking=True break#แปลว่าแผ่นที่ติดกันนี้เสดแล้ว ans.append([i,kk,k]) if breaking==False: anss.append(ans) #print(anss) finalans=[] if typee=="Y": for i in anss: for j in i:#แต่ละตัวคำตอบ #print(j) a=j[0] count=0 while a>0: a-=1 if c[a][j[1]][j[2]]==1: finalans.append([j[0],"I",j[1],j[2]]) count=1 break if count==0: finalans.append([j[0],"M",j[1],j[2]]) elif typee=="N": for i in anss: for j in i: finalans.append([j[0],j[1],j[2]]) #print(finalans) finalans.sort() word=[] for i in finalans: for j in i: word.append(str(j)) print(",".join(word)) word=[] # print(",".join(i)) if len(finalans)==0: print("There is no island") |
# 6331334221 1296239 (2020-12-05 18:11) import numpy as np a=input() d=input().split(',') dimension=[] for i in d : dimension.append(int(i)) cluster=[] for n in range(dimension[1]*dimension[2]): aa=[] e=input() for j in e : if j=='-': aa.append(0) if j=='X' or j== 'x': aa.append(1) cluster.append(aa) clustera=np.array(cluster,np.bool) def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output ans=[] c=0 position=[] positionans=[] for i in range(1,dimension[2]) : base=clustera[(i-1)*dimension[1]:(dimension[1]*(i-1))+dimension[1]] check=clustera[(i)*dimension[1]:(dimension[1]*i)+dimension[1]] for n in to_cluster(check): ans.append(c) if position != []: positionans.append(position) position=[] c=0 eiei=False for h in range(dimension[1]): if eiei: break for j in range(dimension[0]): if base[h][j]==True and n[h][j]==True: c=0 eiei=True position=[] break if base[h][j]==False and n[h][j]==True : c+=1 position.append([int(i),int(h),int(j)]) if position != []: positionans.append(position) ans.append(c) c='M' ans2=[] for i in positionans: for y in i: for n in range(0,int(y[0])-1) : base=clustera[(n)*dimension[1]:(dimension[1]*(n))+dimension[1]] if base[int(y[1])][int(y[2])]==True : c='I' break if base[int(y[1])][int(y[2])]==False : c='M' ans2.append([y[0],c,y[1],y[2]]) ans2.sort() ans3=[] for i in positionans: for y in i: ans3.append(y) ans3.sort() ans4=[] for y in ans3: ans4.append([str(y[0]),str(y[1]),str(y[2])]) ans5=[] for y in ans2: ans5.append([str(y[0]),str(y[1]),str(y[2]),str(y[3])]) if sum(ans)==0: print('There is no island') else: if a=='N': for i in ans4: print(','.join(i)) if a=='Y': for i in ans5: print(','.join((i))) |
# 6331337121 1296206 (2020-12-05 14:25) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax( np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax( np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or( merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range( len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output question = input() w, l, h = [int(i) for i in input().split(',')] data = [] for i in range(h): layer = [] for j in range(l): layer.append(list(input())) data.append(layer) data = np.array(data) == 'x' island = [] for layer in range(1, h): clusters = to_cluster(data[layer]) main = np.sum(data[:layer], axis=0) == 0 for cluster in clusters: if True not in data[layer-1] & cluster: for row in range(l): for col in range(w): if cluster[row, col]: if question == 'Y': if main[row, col]: island.append( (layer, 'M', row, col)) else: island.append( (layer, 'I', row, col)) else: island.append((layer, row, col)) island.sort() if island == []: print('There is no island') else: for i in island: print(','.join([str(x) for x in i])) |
# 6331338821 1296282 (2020-12-05 19:16) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output mode = input() w,l,h = input().split(",") w,l,h = int(w),int(l),int(h) model = [] island = [] for i in range(h): floor = [] for j in range(l): floor.append([float(a.replace("x","1").replace("-","0")) for a in input()]) model.append(np.array(floor) == 1) for i in range(h): isl = 1 #is island if i ==0: continue cf = to_cluster(model[i]) #list of list of sluster for j in range(len(cf)): isl = 1 #scan for that cluster for row in range(l): #row = l for col in range(w): #column = w if cf[j][row][col] == True: #but scan only x if model[i-1][row][col] == True: isl = 0 #not island if isl == 1: for row in range(l): for col in range(w): if cf[j][row][col] == True: mtype = "M" for b in range(i): if model[b][row][col] == True: mtype = "I" island.append((i,mtype,row,col)) if len(island) == 0: print("There is no island") else: if mode == "Y": island.sort() for r in island: print(str(r[0])+","+r[1]+","+str(r[2])+","+str(r[3])) elif mode == "N": q = [] for r in island: q.append((r[0],r[2],r[3])) q.sort() for r in q: print(str(r[0])+","+str(r[1])+","+str(r[2])) |
# 6331341621 1296782 (2020-12-10 22:21) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output op = [] keep = [] types = input().strip() wide,length,high = [int(e) for e in input().strip().split(',')] for i in range(high): inp_all_to_list = [] for j in range(length): inp = input().strip() inp_to_list = [] for e in inp: if e =='-': inp_to_list.append(0) else: inp_to_list.append(1) inp_all_to_list.append(inp_to_list) keep.append(inp_all_to_list) a = np.array(inp_all_to_list,np.bool) if i==0: floor = np.ones_like(a) upper_floor_array = to_cluster(a) for k in range(len(upper_floor_array)): check = floor & upper_floor_array[k] if not check.any(): for r in range(upper_floor_array[k].shape[0]): for c in range(upper_floor_array[k].shape[1]): if upper_floor_array[k][r,c]==True: op.append([i,r,c]) floor = a if op==[]: print('There is no island') else: if types=='N': op.sort() for l in range(len(op)): print(str(op[l][0])+','+str(op[l][1])+','+str(op[l][2])) if types=='Y': op_y = [] main = True for m in range(len(op)): h,r,c = op[m] for flo in range(h-1,-1,-1): if keep[flo][r][c]==1: op_y.append([h,'I',r,c]) main = False break if main: op_y.append([h,'M',r,c]) main =True op_y.sort() for l in range(len(op_y)): print(','.join([str(e) for e in op_y[l]])) |
# 6331343921 1295775 (2020-12-04 19:32) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def get_coor(A): li = [] w,l = A.shape for j in range(w): for i in range(l): if A[j,i]: li.append((j,i)) return li def is_inner(work,cur_fl,coor): for i in range(cur_fl-1,-1,-1): for clus in work[i]: if clus[coor]: return True return False mode = input().strip() l,w,h = [int(n) for n in input().split(',')] work = [] for k in range(h): floor = [] for j in range(w): line = input().strip().translate(str.maketrans('Xx-','110')) floor.append([bool(int(c)) for c in line]) work.append(to_cluster(np.array(floor))) island = [] if h > 1: for k in range(1,h): bf,tf = work[k-1:k+1] for tc in tf: is_island = True for bc in bf: if True in (bc & tc): is_island = False if is_island: coor0 = get_coor(tc) if mode == 'Y': coor1 = [] for coor in coor0: if is_inner(work,k,coor): typ = 'I' else: typ = 'M' coor1.append((typ,)+coor) else: coor1 = coor0 coors =[(k,) + e for e in coor1] island += coors if island == []: print('There is no island') else: island.sort() for isl in island: isn = [str(x) for x in isl] print(','.join(isn)) |
# 6331344521 1296028 (2020-12-04 23:22) #----------------------------------------------------------DO NOT TOUCH----------------------------------------------------- import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output #-------------------------------------------------------------------------------------------------------------------- cmd = input() width,lenght,height = input().split(',') width = int(width) ; lenght = int(lenght) ; height = int(height) mp = [] for level in range(height) : t = [] for ln in range(lenght) : tt = list(input().strip()) for ch in range(len(tt)) : if tt[ch] == 'x' : tt[ch] = 1 else : tt[ch] = 0 t.append(tt) mp.append(t) ans_I = [] ans_M = [] ans_t = [] I_base = np.array(mp[0],np.bool) M_base = np.array(mp[0],np.bool) for lv in range(1,len(mp)) : tarr = np.array(mp[lv],np.bool) cluster = to_cluster(tarr) for arr in cluster : # print(arr) # print("----------------") t = [] if np.sum(M_base & arr) : pass else : for i in range(arr.shape[0]): for j in range(arr.shape[1]): if arr[i,j] != 1 : continue if arr[i,j] == I_base[i,j] : ans_I.append((lv,0,i,j)) ans_t.append((lv,i,j)) else : ans_M.append((lv,1,i,j)) ans_t.append((lv,i,j)) I_base = I_base | tarr M_base = tarr if not len(ans_I) and not len(ans_M) : print("There is no island") elif cmd == 'N' : ans_t.sort() for i in ans_t : print("{},{},{}".format(i[0],i[1],i[2])) elif cmd == 'Y' : t = ans_I + ans_M t.sort() for i in t : if i[1] == 0 : print("{},I,{},{}".format(i[0],i[2],i[3])) else : print("{},M,{},{}".format(i[0],i[2],i[3])) # print(ans_I) # print(ans_M) # Y # 5,3,5 # ----- # xxxxx # ----- # ----- # --x-- # ----- # ----- # --x-- # ----- # ----- # xxx-x # ----x # xxxxx # ----- # ----x |
# 6331345121 1296535 (2020-12-08 21:46) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def find_island(a): ans = [] h, l, w = a.shape visited = np.zeros((l, w)) visited = visited==1 for i in range(1, h): ''' for row in range(l): for col in range(w): if a[i-1, row, col]: visited[row, col] = True ''' visited[a[i-1]] = True layer0 = a[i-1].copy() layer1 = to_cluster(a[i].copy()) for island in layer1: res = island&layer0 if np.sum(res)==0: for row in range(l): for col in range(w): if island[row, col]: if visited[row, col]: t = 'I' else: t = 'M' ans.append([i , t, row, col]) return ans def main(): op = input().strip() w, l, h = [int(e) for e in input().split(',')] arr = [] for i in range(h*l): x = input().strip() for e in x: if e=='x': arr.append(True) else: arr.append(False) arr = np.array(arr) arr = np.resize(arr, (h, l, w)) ans = find_island(arr) ans2 = [[a, c, d] for a, b, c, d in ans] ans2.sort() ans.sort() # print(ans) if len(ans)==0: print('There is no island') elif op == 'N': print('\n'.join([str(a)+','+str(b)+','+str(c) for a, b, c in ans2])) elif op == 'Y': print('\n'.join([str(a)+','+str(b)+','+str(c)+','+str(d) for a, b, c, d in ans])) main() |
# 6331346821 1296807 (2020-12-10 23:26) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output decision = input().strip() w,l,h = input().strip().split(",") w = int(w) l = int(l) h = int(h) inf = [] od = [] for q in range(h): od = [] for i in range(l): v = input().strip() list_em = [] for v2 in v: if v2 == "-": list_em.append(False) else: list_em.append(True) od.append(list_em) inf.append(od) inf = np.array(inf) no_island = True output = [] for e in range(1,h): under = inf[e-1] upper = to_cluster(inf[e]) for cluster in upper: if np.max(under&cluster) == 0: for r in range(l): for c in range(w): if cluster[r, c]: no_island = False if decision == "N": output.append([e, r, c]) if decision == "Y": if np.max(inf[:e ,r,c]) >0: A = "I" else: A = "M" output.append([e, A, r, c]) b = sorted(output) if no_island: print("There is no island") else: for c in b: print(",".join([str(e) for e in c])) |
# 6331348021 1296701 (2020-12-09 23:59) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examplesa >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output q = input() info = input().split(',') width = int(info[0]) length = int(info[1]) height = int(info[2]) if height == 1: print('There is no island') a = input() a1 = [] for i in range(len(a)): a1.append(a[i]) b = [] c = [] for i in range(length*height): b.append(a1) if (i % length) == (length - 1): c.append(b) b = [] if i == (length*height) - 1: break a = input() a1 = [] for j in range(len(a)): a1.append(a[j]) for i in range(len(c)): for j in range(len(c[i])): for k in range(len(c[i][j])): if c[i][j][k] == 'x': c[i][j][k] = 1 else: c[i][j][k] = 0 c = np.array(c) d = [] m = [] for i in range(len(c)): d.append(np.array(to_cluster(c[i]))) for i in range(1, len(d)): for e in (d[i]): n = [] for x in range(width): for y in range(length): if (e[y, x] == True) and (c[i-1][y, x] == True): n.append('M') if (e[y, x] == True) and (c[i-1][y, x] == False): n.append([i, y, x]) if 'M' not in n: m.append(n) p = [] for e in m: for k in e: p.append(k) p.sort() if len(p) == 0: print('There is no island') elif q == 'N': for e in p: print(str(e[0])+','+str(e[1])+','+str(e[2])) elif q == 'Y': r = [] for e in p: r1 = False for i in range(e[0]-1, -1, -1): if c[i][e[1], e[2]] == 1: r.append([e[0], 'I', e[1], e[2]]) r1 = True break if not r1: r.append([e[0], 'M', e[1], e[2]]) r.sort() for e in r: print(str(e[0])+','+str(e[1])+','+str(e[2])+','+str(e[3])) |
# 6332001421 1288244 (2020-11-17 10:58) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output ty=input() w,l,h=input().split(',') w=int(w) l=int(l) h=int(h) a=[] b=[] ans=[] for i in range(l): b.append([-1]*w) for i in range(h): a.append([]) for j in range(l): a[i].append([]) bb=input() for k in range(w): if bb[k]=='x': a[i][j].append(1) else: a[i][j].append(0) for i in range(h): to=np.array(a[i],np.bool) bang=to_cluster(to) for e in bang : ch=True we=[] for x in range(l): for y in range(w): if e[x][y] and b[x][y]!=i-1 : if b[x][y]==-1 : we.append((i,'M',x,y)) else : we.append((i,'I',x,y)) b[x][y]=i elif e[x][y] and b[x][y]==i-1: ch=False b[x][y]=i if ch : ans=ans+we if len(ans)==0: print('There is no island') else : #ans.sort() if ty=='N': anss=[(a,c,d) for a,b,c,d in ans] anss.sort() for e in anss: print(str(e[0])+','+str(e[1])+','+str(e[2])) else : ans.sort() for e in ans: print(str(e[0])+','+str(e[1])+','+str(e[2])+','+str(e[3])) |
# 6332002021 1296710 (2020-12-10 01:00) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def main(): ans = list() arr = list() op = input() m, n, t = [int(e) for e in input().split(',')] for k in range(t) : temp = list() for i in range(n): temp.append([0 if e == '-' else 1 for e in input()]) arr.append(temp) for st,e in enumerate(arr) : if st == 0 : continue state = np.array(to_cluster(np.array(e,np.bool))) for k in state : bottom = False for i in range(n) : for j in range(m) : if k[i][j] == True and arr[st-1][i][j] == 1 : bottom = True if not bottom: for i in range(n) : for j in range(m) : if k[i][j] == True : mode = 'M' for o in range(st-1,-1,-1) : if arr[o][i][j] == 1 : mode = 'I' ans.append((st,mode,i,j)) if op == 'Y' : ans.sort(key=lambda x: (x[0],x[1],x[2],x[3])) else: ans.sort(key=lambda x: (x[0],x[2],x[3])) if len(ans) == 0 : print('There is no island') else: for e in ans: if op == 'Y' : print(','.join([str(k) for k in e])) else: t = (str(e[0]),str(e[2]),str(e[3])) print(','.join(t)) main() |
# 6332003721 1296305 (2020-12-05 22:06) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def haveRoot(mat,bfmat,w,l,floor,layer) : new=[] # print(mat) # print('--') # print(bfmat) for i in range(l) : for j in range(w) : if mat[i][j]==True : if mat[i][j] == bfmat[i][j] : return [True] # elif i>0 and mat[i][j]==bfmat[i-1][j] : # return [True] # elif i<l-1 and mat[i][j]==bfmat[i+1][j] : # return [True] # elif j>0 and mat[i][j]==bfmat[i][j-1] : # return [True] # elif j<w-1 and mat[i][j]==bfmat[i][j+1] : # return [True] if mat[i][j] : # print(layer,i,j) if floor[i][j] : new.append([layer,'I',i,j]) else : new.append([layer,'M',i,j]) return [False,new] def updateFloor(mat,floor,w,l) : for i in range(l) : for j in range(w) : if mat[i][j] : floor[i][j]=1 if __name__ == '__main__': state = input() support= [] w,l,h = [int(e) for e in input().split(',')] boxs = np.zeros((h,l,w),dtype=np.bool) for layer in range(h) : for i in range(l) : x = input() for j in range(len(x)) : if x[j] == 'x' : boxs[layer,i,j] = 1 floor= boxs[0] # print(floor) for layer in range(1,h) : islands = to_cluster(boxs[layer]) # print(islands) for island in islands : d = haveRoot(island,boxs[layer-1],w,l,floor,layer) if not d[0] : support.extend(d[1]) updateFloor(boxs[layer],floor,w,l) if len(support) ==0 : print("There is no island") else : ans = '' if state=='N' : tmp = [] for i in support : tmp.append([i[0],i[2],i[3]]) tmp = sorted(tmp) for i in tmp : ans +=str(i[0])+','+str(i[1])+','+str(i[2])+'\n' else : support = sorted(support) for i in support : ans +=str(i[0])+','+i[1]+','+str(i[2])+','+str(i[3])+'\n' print(ans[:-1]) # N # 5,5,3 # ----- # ----- # --X-- # ----- # ----- # --X-- # ----- # --X-- # --X-- # --X-- # --X-- # --X-- # --X-- # --X-- # --X-- |
# 6332006621 1296765 (2020-12-10 20:30) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output def see_through(m,n,l,a): m -= 1 while m > -1: if a[m][n][l] == True: return True m -= 1 return False def check_support(m,n,a,b): for i in range(m): for j in range(n): if a[i][j] & b[i][j] == True: return True return False command = input() size = input().split(',') height = int(size[2]) row = int(size[1]) column = int(size[0]) model = [] for i in range(height): level = [] for j in range(row): in_row = [] string = input() for e in string: if e == 'x': in_row.append(1) elif e == '-': in_row.append(0) level.append(in_row) model.append(level) ans = [] for i in range(1,height): modelx = np.array(model[i],dtype=bool) for e in to_cluster(modelx): cluster = e.tolist() for j in range(row): for k in range(column): if cluster[j][k] == True and check_support(row,column,cluster,model[i-1]) == False: if see_through(i,j,k,model) == True: ans.append([i,'I',j,k]) else: ans.append([i,'M',j,k]) if len(ans) == 0: print('There is no island') else: if command == 'Y': ans.sort(key=lambda x : (x[0],x[1],x[2],x[3])) for e in ans: print(','.join([str(e[0]),str(e[1]),str(e[2]),str(e[3])])) elif command == 'N': ans.sort(key=lambda x : (x[0],x[2],x[3])) for e in ans: print(','.join([str(e[0]),str(e[2]),str(e[3])])) |
# 6332008921 1288564 (2020-11-17 11:23) def play(a,x,y,n,m,tmp): if tmp[x][y] == 1: return tmp[x][y] = 1 if x+1<n and a[x+1][y]=='x': play(a,x+1,y,n,m,tmp) if x-1>=0 and a[x-1][y]=='x': play(a,x-1,y,n,m,tmp) if y+1<m and a[x][y+1]=='x': play(a,x,y+1,n,m,tmp) if y-1>=0 and a[x][y-1]=='x': play(a,x,y-1,n,m,tmp) def inout(a,x,y,H): for i in range(H): if a[i][x][y] == 'x': return 1 return 0 opr = input() W,L,H = [int(i) for i in input().split(',')] a = [] mark = [] support = [] for i in range(H): b = [] for j in range(L): c = input() b.append(c) a.append(b) mark = [[0 for i in range(W)] for j in range(L)] for i in range(L): for j in range(W): if a[0][i][j] == 'x': mark[i][j] = 1 for k in range(1,H): tmp = [[0 for i in range(W)] for j in range(L)] for i in range(L): for j in range(W): if a[k][i][j] == 'x' and mark[i][j] == 1: play(a[k],i,j,L,W,tmp) for i in range(L): for j in range(W): if a[k][i][j] == 'x' and tmp[i][j] == 0: if inout(a,i,j,k) == 1: #inner tmp[i][j] = 1 support.append([k,'I',i,j]) else: #main tmp[i][j] = 1 support.append([k,'M',i,j]) mark = [] for i in tmp: mark.append(i) if len(support) == 0: print('There is no island') if opr == 'N': for x in support: print(str(x[0])+','+str(x[2])+','+str(x[3])) else: support.sort() for x in support: print(str(x[0])+','+str(x[1])+','+str(x[2])+','+str(x[3])) |
# 6332009521 1295812 (2020-12-04 20:05) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output q = input().strip() # question = Y / N W, L, H = input().strip().split(',') W = int(W) L = int(L) H = int(H) info = [np.array([[0 if s == '-' else 1 for s in input().strip()] for j in range(L)], dtype=np.bool) for i in range(H)] # input island = False # check that there is a island output = list() for i in range(H-1): lower_level = info[i] upper_level = to_cluster(info[i+1]) for c in upper_level: # check every section of upper_level if np.sum(lower_level*c) == 0: # there is no intersection between upper_level and lower_level for col in range(W): for row in range(L): if c[row, col]: # the position of True on the upper_section island = True status = 'M' # Inner / Main for level in range(i): if info[level][row, col]: # there is a block under this position status = 'I' if q == 'Y': output.append([i+1, status, row, col]) else: output.append([i+1, row, col]) if not island: print('There is no island') else: for c in sorted(output): print(','.join([str(e) for e in c])) |
# 6332011721 1288552 (2020-11-17 11:22) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output type = input() W,L,H = input().split(",") W,L,H = int(W),int(L),int(H) table = list() for i in range(H): floor=list() for j in range(L): inpus = input() wid = list() for k in inpus: if(k=='x'): wid.append(1) else: wid.append(0) floor.append(wid) table.append(floor) ans = list() for i in range(H): floor=list() for j in range(L): wid=list() for k in range(W): wid.append(0) floor.append(wid) ans.append(floor) request=list() for i in range(len(table)): floor = to_cluster(np.array(table[i], np.bool)) if(i==0): for j in floor: for a in range(len(j)): for b in range(len(j[a])): if(j[a][b]): ans[i][a][b]=1 else: for j in floor: is_island=1 for a in range(len(j)): for b in range(len(j[a])): if(j[a][b] and ans[i-1][a][b]==1): is_island=0 for a in range(len(j)): for b in range(len(j[a])): if(j[a][b] and is_island==0): ans[i][a][b]=1 elif(j[a][b]): typec='M' for c in range(0,i): if(ans[c][a][b]==1): typec='I' ans[i][a][b]=1 request.append([i,typec,a,b]) if(len(request)==0): print("There is no island") elif(type=='N'): request.sort(key=lambda x:(x[0],x[2],x[3])) for i in request: print(str(i[0])+","+str(i[2])+","+str(i[3])) else: request.sort(key=lambda x:(x[0],x[1],x[2],x[3])) for i in request: print(str(i[0])+","+i[1]+","+str(i[2])+","+str(i[3])) |
# 6332014621 1296201 (2020-12-05 14:22) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output Type = input() inp = input().split(',') W = int(inp[0]) L = int(inp[1]) H = int(inp[2]) List_Floor = [] Prev_Array = np.array([]) OR_list = [[False for i in range(W)] for j in range(L)] ans = [] for line in range(L*H) : inp2 = input() temp_list = [] for j in range(W) : if inp2[j] == '-' : temp_list.append(0) else : temp_list.append(1) List_Floor.append(temp_list) if ((line+1) % L) == 0 : Array = to_cluster(np.array(List_Floor, np.bool)) #print(Array) if (line+1) // L != 1 : #print("test") # print(len(Prev_Array),len(Array)) # print("test:",Prev_Array[::][Prev_Array[::] and Array[::]]) check_list = [False]*len(Array) for A in range(len(Array)) : for B in range(len(Prev_Array)) : for i in range(L) : for j in range(W) : OR_list[i][j] = OR_list[i][j] or Prev_Array[B][i,j] if Prev_Array[B][i,j] and Array[A][i,j] : check_list[A] = True break if check_list[A] : break if check_list[A] : break #print("Test",check_list[A]) for A in range(len(Array)) : if not check_list[A] : for i in range(L) : for j in range(W) : if Array[A][i,j] : if Type == 'N' : ans.append((((line+1) // L)-1,i,j)) else : if OR_list[i][j] : ans.append((((line+1) // L)-1,'I',i,j)) else : ans.append((((line+1) // L)-1,'M',i,j)) Prev_Array = Array List_Floor.clear() if len(ans) == 0 : print("There is no island") else : ans.sort() if Type == 'N' : for e in ans : print(e[0],end=",") print(e[1],end=",") print(e[2]) else : for e in ans : print(e[0],end=",") print(e[1],end=",") print(e[2],end=",") print(e[3]) #print("Test") ''' print(List_pre_array) Array = to_cluster(np.array(List_pre_array, np.bool)) print(Array) ''' |
# 6332015221 1295740 (2020-12-04 19:03) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output type = input() size = input().split(",") W,L,H = int(size[0]),int(size[1]),int(size[2]) record = [] check = [] for i in range(L): check.append([0]*W) for i in range(H): m = [] for j in range(L): n = input() z = [] h = 0 for e in n: if e=='-': e = 0 else: e = 1 if check[j][h] == 0: check[j][h] = i+1 z.append(e) h += 1 m.append(z) m = np.array(m,np.bool) m = np.array(to_cluster(m)) record.append(m) ans = [] for i in range(1,H): t_1 = [] t_2 = [] for x in record[i]: c_1 = [] for x_0 in range(L): for y_0 in range(W): if x[x_0][y_0]: c_1.append((x_0,y_0)) t_1.append(set(c_1)) for x in record[i-1]: c_2 = [] for x_0 in range(L): for y_0 in range(W): if x[x_0][y_0]: c_2.append((x_0,y_0)) t_2.append(set(c_2)) for x in t_1: c_pair = False for y in t_2: if len(x & y) != 0: c_pair = True if c_pair == False: for k in x: ans.append([i,k[0],k[1]]) ans.sort() if len(ans) == 0: print("There is no island") else: if type == "Y": for x in range(len(ans)): if check[ans[x][1]][ans[x][2]] < ans[x][0]:#I ans[x].insert(1,"I") else:#M ans[x].insert(1,"M") for x in sorted(ans) : print(str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])) else: for x in ans: print(str(x[0])+","+str(x[1])+","+str(x[2])) |
# 6332016921 1296160 (2020-12-05 11:41) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output Q = input() kwang,yaw,sung = [int(i) for i in input().split(",")] mark = [[0]*kwang for i in range(yaw)] start = 0 list_ = [] list2_ = [] first = True cnt = 0 ans = [] list2_.append([]) for i in range(yaw*sung): inp = input().strip() for j in range(len(inp)): if inp[j]=='x': list_.append(1) else: list_.append(0) list2_[start].append(list_.copy()) list_.clear() if (i % yaw) == yaw - 1: sep = to_cluster(np.array(list2_[start])) if first: first = False else: for j in range(len(sep)): for a in range(yaw): for b in range(kwang): if(prev[a][b]==1 and sep[j][a,b]==1): cnt += 1 if prev[a][b]==1: mark[a][b] = 1 if cnt==0: for a in range(yaw): for b in range(kwang): if(Q=='Y'): if(sep[j][a,b]==1 and mark[a][b]): ans.append([start,'I',a,b]) elif sep[j][a,b]==1 and not mark[a][b]: ans.append([start,'M',a,b]) else: if(sep[j][a,b]==1): ans.append([start,a,b]) cnt = 0 list2_.append([]) prev = np.array(list2_[start]) start += 1 if Q=='Y': ans = sorted(ans,key = lambda x:(x[0],x[1],x[2],x[3])) else: ans = sorted(ans,key=lambda x:(x[0],x[1],x[2])) if(len(ans)==0): print("There is no island") elif(Q=='Y'): for i in range(len(ans)): print("{},{},{},{}".format(ans[i][0],ans[i][1],ans[i][2],ans[i][3])) else: for i in range(len(ans)): print("{},{},{}".format(ans[i][0],ans[i][1],ans[i][2])) #print(ans) |
# 6332019821 1296547 (2020-12-08 23:04) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output mode = input().strip() island = [] pri = [] m,n,l = [int(i) for i in input().split(',')] for k in range(l): f = [] for i in range(n): s = [j for j in input().strip()] t = [] for w in s: if w == 'x': t.append(1) else: t.append(0) f.append(t) pri.append(f) ch = [[False for i in range(m)] for j in range(n)] last = [[True]*m]*n for t in range(l): temp = np.array(pri[t],np.bool) temp = np.array(to_cluster(temp)) for cl in temp: blo = [] fl = True for i in range(len(cl)): for j in range(len(cl[0])): ty = 'M' if cl[i][j]: if ch[i][j]: ty = 'I' blo.append((i,j,ty)) ch[i][j] = True if last[i][j]: fl = False if fl: for i,j,ty in blo: island.append((t,ty,i,j)) last = pri[t] if mode == 'N': for i in sorted(island,key=lambda x: (x[0],x[2],x[3])): print("{},{},{}".format(i[0],i[2],i[3])) if mode == 'Y': for i in sorted(island): print("{},{},{},{}".format(i[0],i[1],i[2],i[3])) if len(island)==0: print("There is no island") ''' N 5,3,5 ----- xxxxx ----- ----- --x-- ----- ----- --x-- ----- ----- xxx-x ----x xxxxx ----- ----x ''' |
# 6332022621 1288558 (2020-11-17 11:22) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output cmd = input().strip() x = input().strip() l,w,h = [int(e) for e in x.split(',')] ans = [] allfloor = [] for i in range(h): lst = [] for j in range(w): inline = [] s = input().strip() for e in s: if e=='x': inline.append(1) else: inline.append(0) lst.append(inline) allfloor.append(lst) cluster = to_cluster(np.array(lst)) if i==0: continue cnt = 0 for e in cluster: closest = -1 cnt+=1 for H in range(i): for j in range(w): for k in range(l): if allfloor[H][j][k]==1 and e[j][k]==1: closest = max(closest,H) if closest!=i-1: for j in range(w): for k in range(l): if e[j][k]==0: continue eachclosest = -1 for H in range(i): if allfloor[H][j][k]==1: eachclosest = max(eachclosest,H) if eachclosest==-1: t = 'M' else: t = 'I' ans.append((i,t,j,k)) ans.sort() if len(ans)==0: print('There is no island') elif cmd=='N': ans = [(e[0],e[2],e[3]) for e in ans] ans.sort() for e in ans: print(str(e[0])+','+str(e[1])+','+str(e[2])) else: for e in ans: print(str(e[0])+','+str(e[1])+','+str(e[2])+','+str(e[3])) |
# 6332024921 1295648 (2020-12-04 17:08) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output yorn = input() ans = [] ans2 = [] d = [int(i) for i in input().split(',')] f = [] d[0],d[1] = d[1],d[0] mark = [] for i in range(d[2]): mark.append([]) for j in range(d[0]): mark[i].append([]) for k in range(d[1]): mark[i][j].append(0) for i in range(d[2]): f.append([]) for j in range(d[0]): x = [k for k in input()] a = [] for k in range(len(x)): if x[k] == "x": a.append(1) mark[i][j][k] = 1 else: a.append(0) if mark[i-1][j][k] == 1: mark[i][j][k] = 1 f[i].append(a) if i != 0: cluster = to_cluster(np.array(f[i],np.bool)) for k in range(len(cluster)): ch = 0 for l in range(len(cluster[k])): for m in range(len(cluster[k][l])): if f[i-1][l][m] == 1 and cluster[k][l][m] == True: ch = 1 if ch == 0 : for l in range(len(cluster[k])): for m in range(len(cluster[k][l])): if i > 1 and mark[i-2][l][m] == 1 and cluster[k][l][m] == True: ans.append([i,"I",l,m]) ans2.append([i,l,m]) elif cluster[k][l][m] == True: ans.append([i,"M",l,m]) ans2.append([i,l,m]) ans.sort() ans2.sort() if len(ans) == 0: print("There is no island") elif yorn == "N": for i in ans2: print(str(i[0])+","+str(i[1])+","+str(i[2])) else: for i in ans: print(str(i[0])+","+str(i[1])+","+str(i[2])+","+str(i[3])) """ N 5,5,3 ----- ----- --x-- ----- ----- --x-- ----- --x-- --x-- --x-- --x-- --x-- --x-- --x-- --x-- Y 2,2,3 x- -- -- -x -- x- N 3,3,1 xx- x-- x-- Y 5,3,5 ----- xxxxx ----- ----- --x-- ----- ----- --x-- ----- ----- xxx-x ----x xxxxx ----- ----x """ |
# 6332027821 1296449 (2020-12-06 21:38) import numpy as np def to_cluster(a): height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output Q = input() dim = [int(i) for i in input().split(',')] stru = [[[]]*dim[1] for i in range(dim[2])] ansY = [] ansN = [] for i in range(dim[2]): for j in range(dim[1]): stru[i][j] = input() pros = [[[] for j in range(dim[1])] for i in range(dim[2])] for i in range(dim[2]): for j in range(dim[1]): for k in range(dim[0]): if stru[i][j][k] == '-': pros[i][j].append(0) else: pros[i][j].append(1) nppro = np.array(pros) accu = np.array(nppro[0]) bef = np.array(nppro[0]) for i in range(1, dim[2]): tmp = to_cluster(nppro[i]) for j in range(len(tmp)): isisland = True joi = tmp[j] | bef if np.sum(tmp[j]) > np.sum(joi)-np.sum(bef): isisland = False if isisland: for i1 in range(dim[1]): for j1 in range(dim[0]): if tmp[j][i1][j1] == 1: if accu[i1][j1] == 1: ansY.append((i, 'I', i1, j1)) ansN.append((i, i1, j1)) else: ansY.append((i, 'M', i1, j1)) ansN.append((i, i1, j1)) bef = nppro[i] accu = accu | nppro[i] ansY.sort() ansN.sort() if Q == 'N': for i in ansN: print(str(i[0])+','+str(i[1])+','+str(i[2])) else: for i in ansY: print(str(i[0])+','+i[1]+','+str(i[2])+','+str(i[3])) if len(ansY) == 0: print('There is no island') |
# 6332028421 1295809 (2020-12-04 20:04) def fill(plate,floor,x,y): if 0<=x<len(floor) and 0<=y<len(floor[0]) and floor[x][y] and not plate[x][y]: plate[x][y]=True fill(plate,floor,x+1,y) fill(plate,floor,x-1,y) fill(plate,floor,x,y+1) fill(plate,floor,x,y-1) mode=input().strip().upper() c,r,h=[int(e) for e in input().split(",")] topView=[[False for j in range(c)] for i in range(r)] lastFloor=None ans=[] for k in range(h): floor=[] for i in range(r): floor.append(list(input().strip().upper())) floor=[[True if floor[i][j]=="X" else False for j in range(c)] for i in range(r)] if lastFloor!=None: plate=[[False for j in range(c)] for i in range(r)] for i in range(r): for j in range(c): if lastFloor[i][j]: fill(plate,floor,i,j) for i in range(r): for j in range(c): if floor[i][j]==True and plate[i][j]==False: ans.append((k,"I" if topView[i][j] else "M",i,j)) lastFloor=floor topView=[[topView[i][j] or floor[i][j] for j in range(c)] for i in range(r)] if mode=="N": ans=[(e[0],e[2],e[3]) for e in ans] ans.sort() if len(ans)==0: print("There is no island") else: for i in ans: if mode=="N": print("{},{},{}".format(i[0],i[1],i[2])) elif mode=="Y": print("{},{},{},{}".format(i[0],i[1],i[2],i[3])) |
# 6332029021 1288619 (2020-11-17 11:27) import numpy as np # NO am = input() m,n,h = [int(i) for i in input().split(',')] ns = [] ls = [[-1 for i in range(m)] for i in range(n)] def opkeyn(z): return z[0],z[2],z[3],z[1] def opkeyy(z): return z[0],z[1],z[2],z[3] def find(b,i,j,f): #print("find(",i,j,f,")") global n,m,ls,ns #found = False st = 0 q = [[i,j]] qi = 0 b[i][j] = 2 while(qi < len(q)): ci = q[qi][0] cj = q[qi][1] #print(ci,cj) qi +=1 if ls[ci][cj] == f-1: st = 1 #elif ls[ci][cj] != -1 and st == 0: # st = 2 # si = ci # sj = cj if(ci-1 >= 0): if b[ci-1][cj] == 1: b[ci-1][cj] = 2 q.append([ci-1,cj]) if(cj-1 >=0): if b[ci][cj-1] == 1: b[ci][cj-1] = 2 q.append([ci,cj-1]) if(ci+1 < n): if b[ci+1][cj] == 1: b[ci+1][cj] = 2 q.append([ci+1,cj]) if(cj+1 < m): if b[ci][cj+1] == 1: b[ci][cj+1] = 2 q.append([ci,cj+1]) if st == 1: pass #for i in q: # b[i[0]][i[1]] = 3 #elif st == 2: # ns.append([f,'I',si,sj]) else: for i in q: if ls[i[0]][i[1]] == -1: ns.append([f,'M',i[0],i[1]]) else: ns.append([f,'I',i[0],i[1]]) return def fill(b,i,j,f): return # first floor for i in range(n): k = input() for j in range(m): if k[j] == 'x': ls[i][j] = 0 # after that for ch in range(1,h): #print("NEXT FLOOR") cb = [[0 for i in range(m)] for i in range(n)] for i in range(n): k = input() for j in range(m): if k[j] == 'x': cb[i][j] = 1 for i in range(n): for j in range(m): if cb[i][j] == 1: find(cb,i,j,ch) for i in range(n): for j in range(m): if cb[i][j] != 0: ls[i][j] = ch if len(ns) == 0: print("There is no island") else: if am == 'N': ns = sorted(ns,key=opkeyn) else: ns = sorted(ns,key=opkeyy) for i in ns: #print(i) i = [str(j) for j in i] if am == 'N': print(','.join([i[0],i[2],i[3]])) else: #print(1/0) print(','.join(i)) # LETS NOT USE THIS def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output |
# 6332031221 1288182 (2020-11-17 10:51) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output type = input() m,n,h = [int(i) for i in input().split(',')] check = [[0 for j in range(m)] for i in range(n)] bf = [[0 for j in range(m)] for i in range(n)] ans = [] for k in range(h): tab = [[0 for j in range(m)] for i in range(n)] for i in range(n): s = input() for j in range(m): if s[j]=='x': tab[i][j] = 1 if k!=0: cluster = to_cluster(np.array(tab,np.bool)) for x in cluster: ch=1 for i in range(n): for j in range(m): if x[i][j] and bf[i][j]: ch = 0 if ch: for i in range(n): for j in range(m): if not x[i][j]: continue if not check[i][j]: s='M' else: s='I' if type=='Y': ans.append((k,s,i,j)) else: ans.append((k,i,j)) bf = tab for i in range(n): for j in range(m): check[i][j]|=tab[i][j] if len(ans)==0: print("There is no island") else: ans.sort() for i in ans: s = "" for j in i: s+=str(j)+',' print(s[:-1]) |
# 6332032921 1296157 (2020-12-05 11:38) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output t = input() w, l, h = list(map(int, input().split(','))) arr = [[[False for e in range(w)] for e in range(l)] for e in range(h)] for i in range(h): for j in range(l): p = input() for k in range(len(p)): if p[k] == 'x': arr[i][j][k] = True x = np.array(arr) chk = np.zeros((h, l, w), np.bool) island = [] for i in range(h): convert = to_cluster(x[i]) if i == 0: for q in convert: for j in range(l): for k in range(w): if q[j][k]: chk[i][j][k] = True else: for q in convert: pl = False for j in range(l): for k in range(w): if q[j][k] and chk[i - 1][j][k]: pl = True for j in range(l): for k in range(w): if q[j][k]: if not pl: if not chk[i - 1][j][k]: low = i - 1 text = 'M' while low >= 0: if x[low][j][k]: text = 'I' break low -= 1 if t == 'Y': island.append((i, text, j, k)) else: island.append((i, j, k)) chk[i][j][k] = True if len(island) == 0: print('There is no island') exit() island.sort() for q in island: temp = ','.join(list(map(str, q))) print(temp) |
# 6332033521 1296603 (2020-12-09 11:45) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output mode = input() l,w,h = [int(i) for i in input().split(",")] data = [] for i in range(h): t = [[0 for i in range(l)] for i in range(w)] for j in range(w): temp = input() for k in range(l): if temp[k] == "-": t[j][k] = 0 else: t[j][k] = 1 data.append(t) ans = [] for i in range(1,h): k = np.array(data[i],np.bool) for a in to_cluster(k): chk = False for j in range(w): for k in range(l): if a[j][k] == True and data[i-1][j][k] == 1: chk = True if not chk: #found island for j in range(w): for k in range(l): if a[j][k] == True: if mode == "N": ans.append([i,j,k]) else: b = "M" inner = False for ii in range(i): if data[ii][j][k] == 1: inner = True if inner: b = "I" ans.append([i,b,j,k]) if len(ans) == 0: print("There is no island") else: for i in sorted(ans): print(','.join([str(s) for s in i])) |
# 6332034121 1296772 (2020-12-10 21:32) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output q_type = input() w,l,h = input().split(',') w = int(w) h = int(h) l = int(l) cube = [] for i in range(h): tmp1 = [] for j in range(l): s = input() s = list(s) for k in range(len(s)): #print(s[k]) if s[k] == 'x': s[k] = 1 else : s[k] = 0 tmp1.append(s) cube.append(tmp1) #print(cube) cube = np.array(cube,np.bool) #print(cube.shape) ans = [] for i in range(1,h): sep = to_cluster(cube[i]) for e in sep: #print(e) cnt = 0 floor = [] idx = [] for j in range(l): for k in range(w): if e[j,k] == True: chk_floor = False for a in range(i-1,-1,-1): if cube[a,j,k] == True: floor.append(a) idx.append([j,k]) chk_floor = True break if chk_floor == False: floor.append(-1) idx.append([j,k]) if not i-1 in floor: for ee in range(len(floor)): if q_type == 'N': ans.append([i,idx[ee][0],idx[ee][1]]) elif floor[ee] == -1 and q_type == 'Y': ans.append([i,'M',idx[ee][0],idx[ee][1]]) elif floor[ee] != i-1 and q_type == 'Y': ans.append([i,'I',idx[ee][0],idx[ee][1]]) ans.sort() if len(ans) == 0: print('There is no island') for e in ans: print(','.join(str(a) for a in e)) |
# 6332039321 1296216 (2020-12-05 15:33) import numpy as np def to_cluster(a): """ Seperate cluster from 2d array. # Parameters a: array_like - 2 dimension array of `True` or `False` with dtype np.bool. # Returns cluster_list: List - a list of 2d array each with only one cluster. # Notes This function doesn't guarantee order of output. # Examples >>> a = np.array([ [1, 0, 1], [1, 0, 1], [1, 0, 1], ], np.bool) >>> np.array(to_cluster(a)) array([[[ True, False, False], [ True, False, False], [ True, False, False]], [[False, False, True], [False, False, True], [False, False, True]]]) """ height, width = a.shape output = [] last_cross_section = [] start_row = np.argmax(np.pad(np.max(a, axis=1), ((0, 1),), mode='constant', constant_values=np.inf)) for row_index in range(start_row, height): cross_section = [] seg_start = np.argmax(np.pad(a[row_index], ((0, 1),), mode='constant', constant_values=np.inf)) for col_index in range(seg_start, width + 1): if (col_index == width and a[row_index][col_index-1]) or (col_index < width and a[row_index][col_index-1] and not a[row_index][col_index]): seg = np.full_like(a, False) seg[row_index, seg_start:col_index] = True cross_section.append(seg) if col_index < width and not a[row_index][col_index-1] and a[row_index][col_index]: seg_start = col_index marked_remove = set() for lcs in last_cross_section: merged_to = None for ic, ccs in enumerate(cross_section): if np.any(np.logical_and(lcs[row_index - 1], ccs[row_index])): if merged_to is None: ccs[:, :] = np.logical_or(lcs, ccs) # inplace merge merged_to = ccs else: # merge into the same lcs, so need to remove from last_cross_section merged_to[:, :] = np.logical_or(merged_to, ccs) # inplace merge marked_remove.add(ic) if merged_to is None: output.append(lcs) last_cross_section = [cross_section[ic] for ic in range(len(cross_section)) if ic not in marked_remove] output.extend(last_cross_section) return output opr=input() w,ll,h=[int(e) for e in input().split(',')] same=set() ab=[] ansn=[] ansy=[] for j in range(ll): la=input() arr=[] for k in la: arr.append(k=='x') ab.append(arr) ab=np.array(ab) gg=to_cluster(ab) for i in gg: for m in range(len(i)): for n in range(len(i[m])): if(i[m][n]==True): same.add((m,n)) for i in range(1,h): ba=[] for j in range(ll): la=input() arr=[] for k in la: arr.append(k=='x') ba.append(arr) a=np.array(ba) g=to_cluster(a) for k in g: fou=False for l in gg: for m in range(len(k)): for n in range(len(k[m])): if(k[m][n]==l[m][n] and k[m][n]==True): fou=True if(fou==False): for m in range(len(k)): for n in range(len(k[m])): if(k[m][n]==True): if((m,n) in same): ansy.append((i,'I',m,n)) else: ansy.append((i,'M',m,n)) same.add((m,n)) ansn.append((i,m,n)) else: for m in range(len(k)): for n in range(len(k[m])): if(k[m][n]==True): same.add((m,n)) gg=g ansn.sort() ansy.sort() if(opr=='N'): if(len(ansn)==0): print('There is no island') else: for i in ansn: print(str(i[0])+','+str(i[1])+','+str(i[2])) else: if(len(ansy)==0): print('There is no island') else: for i in ansy: print(str(i[0])+','+str(i[1])+','+str(i[2])+','+str(i[3])) """ N 5,5,3 ----- ----- --x-- ----- ----- --x-- ----- --x-- --x-- --x-- --x-- --x-- --x-- --x-- --x-- Y 5,5,3 ----- ----- --x-- ----- ----- --x-- ----- --x-- --x-- --x-- --x-- --x-- --x-- --x-- --x-- Y 2,2,3 x- -- -- -x -- x- Y 5,3,5 ----- xxxxx ----- ----- --x-- ----- ----- --x-- ----- ----- xxx-x ----x xxxxx ----- ----x """ |