n, m = map(int, input().split()) rzedy = [] for i in range(n): rzedy.append(input()) kolumny = ["".join(i) for i in zip(*rzedy)] def convert(a): res = [] for k, v in enumerate(a): d = {} for z in v: d.setdefault(z, 0) d[z] += 1 res.append((d, k)) return res kolumny2 = convert(kolumny) rzedy2 = convert(rzedy) #__import__('pprint').pprint(rzedy) #__import__('pprint').pprint(kolumny) #__import__('pprint').pprint(rzedy2) #__import__('pprint').pprint(kolumny2) answers = [] dlugosc = lambda x: len(x[0]) for _ in range(n + m): if len(kolumny2) == 0 or len(rzedy2) == 0: break rzedy2.sort(key=dlugosc, reverse=True) kolumny2.sort(key=dlugosc, reverse=True) if len(rzedy2[-1][0]) == 1: rzad = rzedy2.pop() k = list(rzad[0].keys())[0] answers.append(('R', rzad[1]+1, k)) #print(k) for i in range(len(kolumny2)): if kolumny2[i][0][k] == 1: del kolumny2[i][0][k] else: kolumny2[i][0][k] -= 1 continue if len(kolumny2[-1][0]) == 1: kolumna = kolumny2.pop() k = list(kolumna[0].keys())[0] answers.append(('K', kolumna[1]+1, k)) #print(k) for i in range(len(rzedy2)): if rzedy2[i][0][k] == 1: del rzedy2[i][0][k] else: rzedy2[i][0][k] -= 1 continue #__import__('pprint').pprint(answers) print(len(answers)) for i in reversed(answers): print(*i)
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 | n, m = map(int, input().split()) rzedy = [] for i in range(n): rzedy.append(input()) kolumny = ["".join(i) for i in zip(*rzedy)] def convert(a): res = [] for k, v in enumerate(a): d = {} for z in v: d.setdefault(z, 0) d[z] += 1 res.append((d, k)) return res kolumny2 = convert(kolumny) rzedy2 = convert(rzedy) #__import__('pprint').pprint(rzedy) #__import__('pprint').pprint(kolumny) #__import__('pprint').pprint(rzedy2) #__import__('pprint').pprint(kolumny2) answers = [] dlugosc = lambda x: len(x[0]) for _ in range(n + m): if len(kolumny2) == 0 or len(rzedy2) == 0: break rzedy2.sort(key=dlugosc, reverse=True) kolumny2.sort(key=dlugosc, reverse=True) if len(rzedy2[-1][0]) == 1: rzad = rzedy2.pop() k = list(rzad[0].keys())[0] answers.append(('R', rzad[1]+1, k)) #print(k) for i in range(len(kolumny2)): if kolumny2[i][0][k] == 1: del kolumny2[i][0][k] else: kolumny2[i][0][k] -= 1 continue if len(kolumny2[-1][0]) == 1: kolumna = kolumny2.pop() k = list(kolumna[0].keys())[0] answers.append(('K', kolumna[1]+1, k)) #print(k) for i in range(len(rzedy2)): if rzedy2[i][0][k] == 1: del rzedy2[i][0][k] else: rzedy2[i][0][k] -= 1 continue #__import__('pprint').pprint(answers) print(len(answers)) for i in reversed(answers): print(*i) |