from sys import stdin, stdout def parity(table): res = 0 for i_row, row in enumerate(table): for i_col, c in enumerate(row): if c != ".": res += i_row res += i_col res %= 2 return res def degree(table): res = 0 for row in table: for c, d in zip(row[:-1], row[1:]): if c != d: res += 1 for row, next_row in zip(table[:-1], table[1:]): for c, d in zip(row, next_row): if c != d: res += 1 return res def calculate_pieces(table): res = 0 for row in table: for c in row: if c != ".": res += 1 return res def total_moves(n, m): return 2 * n * m - n - m def binomial(n, k): ''' Binomial coefficient aka the "choose" function n! / (k! * (n - k)!) ''' p = 1 for i in range(1, min(k, n - k) + 1): p *= n p //= i n -= 1 return p def edges_per_move(n, m, pieces): return binomial(n * m - 2, pieces - 1) n, m = [int(x) for x in stdin.readline().split()] start_table = [] for _ in range(n): start_table.append(stdin.readline().strip()) _ = stdin.readline() end_table = [] for _ in range(n): end_table.append(stdin.readline().strip()) if parity(start_table) != parity(end_table): stdout.write("0\n") else: pieces = calculate_pieces(end_table) res = degree(end_table)/ (total_moves(n, m) * edges_per_move(n, m, pieces)) stdout.write(f"{res:.15f}\n")
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 | from sys import stdin, stdout def parity(table): res = 0 for i_row, row in enumerate(table): for i_col, c in enumerate(row): if c != ".": res += i_row res += i_col res %= 2 return res def degree(table): res = 0 for row in table: for c, d in zip(row[:-1], row[1:]): if c != d: res += 1 for row, next_row in zip(table[:-1], table[1:]): for c, d in zip(row, next_row): if c != d: res += 1 return res def calculate_pieces(table): res = 0 for row in table: for c in row: if c != ".": res += 1 return res def total_moves(n, m): return 2 * n * m - n - m def binomial(n, k): ''' Binomial coefficient aka the "choose" function n! / (k! * (n - k)!) ''' p = 1 for i in range(1, min(k, n - k) + 1): p *= n p //= i n -= 1 return p def edges_per_move(n, m, pieces): return binomial(n * m - 2, pieces - 1) n, m = [int(x) for x in stdin.readline().split()] start_table = [] for _ in range(n): start_table.append(stdin.readline().strip()) _ = stdin.readline() end_table = [] for _ in range(n): end_table.append(stdin.readline().strip()) if parity(start_table) != parity(end_table): stdout.write("0\n") else: pieces = calculate_pieces(end_table) res = degree(end_table)/ (total_moves(n, m) * edges_per_move(n, m, pieces)) stdout.write(f"{res:.15f}\n") |