import numpy as np
def generate_moves(n, m, plansza):
# częstotliwości kolorów w rzędach i kolumnach:
# utwórz listę n pustych map:
czestosci_rzedy = [{} for _ in range(n)]
czestosci_kolumny = [{} for _ in range(m)]
# for i in range(n):
# #for j in range(m):
# czestosci_rzedy[i] = Counter(plansza[i])
# j = 0
# for zn in plansza[i]:
# #zn = plansza[i][j]
# # ...
# czestosci_kolumny[j][zn] = czestosci_kolumny[j].get(zn, 0) + 1
# j += 1
for i in range(plansza.shape[0]):
unique, counts = np.unique(plansza[i, :], return_counts=True)
czestosci_rzedy[i] = dict(zip(unique, counts))
for i in range(plansza.shape[1]):
unique, counts = np.unique(plansza[:, i], return_counts=True)
czestosci_kolumny[i] = dict(zip(unique, counts))
# return 0, []
n_max = n
m_max = m
# 2. Inicjalizuj listę ruchów
ruchy = []
while n_max > 0 and m_max > 0:
if len(czestosci_rzedy) > 0 and m_max > 0:
rzedy_do_usuniecia = []
kolory_do_zmniejszenia = {}
for i in range(n):
for x in czestosci_rzedy[i].items():
if x[1] == m_max:
n_max -= 1
ruchy.append(f'R {i+1} {x[0]}')
rzedy_do_usuniecia.append(i)
kolory_do_zmniejszenia[x[0]] = kolory_do_zmniejszenia.get(x[0], 0) + 1
for j in range(m):
if len(czestosci_kolumny[j]) > 0:
for kol_cz in kolory_do_zmniejszenia.items():
if kol_cz[0] in czestosci_kolumny[j]:
czestosci_kolumny[j][kol_cz[0]] -= kol_cz[1]
if czestosci_kolumny[j][kol_cz[0]] == 0:
del czestosci_kolumny[j][kol_cz[0]]
for i in rzedy_do_usuniecia:
czestosci_rzedy[i] = {}
if len(czestosci_kolumny) > 0 and n_max > 0:
kolumny_do_usuniecia = []
for j in range(m):
for x in czestosci_kolumny[j].items():
if x[1] == n_max:
m_max -= 1
ruchy.append(f'K {j+1} {x[0]}')
kolumny_do_usuniecia.append(j)
for i in range(n):
if len(czestosci_rzedy[i]) > 0:
czestosci_rzedy[i][x[0]] -= 1
for j in kolumny_do_usuniecia:
czestosci_kolumny[j] = {}
ruchy.reverse()
return len(ruchy), ruchy
# 1. Wczytaj dane
n, m = map(int, input().split())
p0 = np.array([input() for _ in range(n)])
plansza = np.array([list(s) for s in p0])
moves_count, moves = generate_moves(n, m, plansza)
print(moves_count)
for move in moves:
print(move)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import numpy as np def generate_moves(n, m, plansza): # częstotliwości kolorów w rzędach i kolumnach: # utwórz listę n pustych map: czestosci_rzedy = [{} for _ in range(n)] czestosci_kolumny = [{} for _ in range(m)] # for i in range(n): # #for j in range(m): # czestosci_rzedy[i] = Counter(plansza[i]) # j = 0 # for zn in plansza[i]: # #zn = plansza[i][j] # # ... # czestosci_kolumny[j][zn] = czestosci_kolumny[j].get(zn, 0) + 1 # j += 1 for i in range(plansza.shape[0]): unique, counts = np.unique(plansza[i, :], return_counts=True) czestosci_rzedy[i] = dict(zip(unique, counts)) for i in range(plansza.shape[1]): unique, counts = np.unique(plansza[:, i], return_counts=True) czestosci_kolumny[i] = dict(zip(unique, counts)) # return 0, [] n_max = n m_max = m # 2. Inicjalizuj listę ruchów ruchy = [] while n_max > 0 and m_max > 0: if len(czestosci_rzedy) > 0 and m_max > 0: rzedy_do_usuniecia = [] kolory_do_zmniejszenia = {} for i in range(n): for x in czestosci_rzedy[i].items(): if x[1] == m_max: n_max -= 1 ruchy.append(f'R {i+1} {x[0]}') rzedy_do_usuniecia.append(i) kolory_do_zmniejszenia[x[0]] = kolory_do_zmniejszenia.get(x[0], 0) + 1 for j in range(m): if len(czestosci_kolumny[j]) > 0: for kol_cz in kolory_do_zmniejszenia.items(): if kol_cz[0] in czestosci_kolumny[j]: czestosci_kolumny[j][kol_cz[0]] -= kol_cz[1] if czestosci_kolumny[j][kol_cz[0]] == 0: del czestosci_kolumny[j][kol_cz[0]] for i in rzedy_do_usuniecia: czestosci_rzedy[i] = {} if len(czestosci_kolumny) > 0 and n_max > 0: kolumny_do_usuniecia = [] for j in range(m): for x in czestosci_kolumny[j].items(): if x[1] == n_max: m_max -= 1 ruchy.append(f'K {j+1} {x[0]}') kolumny_do_usuniecia.append(j) for i in range(n): if len(czestosci_rzedy[i]) > 0: czestosci_rzedy[i][x[0]] -= 1 for j in kolumny_do_usuniecia: czestosci_kolumny[j] = {} ruchy.reverse() return len(ruchy), ruchy # 1. Wczytaj dane n, m = map(int, input().split()) p0 = np.array([input() for _ in range(n)]) plansza = np.array([list(s) for s in p0]) moves_count, moves = generate_moves(n, m, plansza) print(moves_count) for move in moves: print(move) |
English