-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_euler_345.rb
103 lines (91 loc) · 3.72 KB
/
project_euler_345.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Matrix Sum
# Problem 345
# We define the Matrix Sum of a matrix as the maximum sum of matrix elements with each element being the only one in his row and column. For example, the Matrix Sum of the matrix below equals 3315 ( = 863 + 383 + 343 + 959 + 767):
#
# 7 53 183 439 863
# 497 383 563 79 973
# 287 63 343 169 583
# 627 343 773 959 943
# 767 473 103 699 303
#
# Find the Matrix Sum of:
#
# 7 53 183 439 863 497 383 563 79 973 287 63 343 169 583
# 627 343 773 959 943 767 473 103 699 303 957 703 583 639 913
# 447 283 463 29 23 487 463 993 119 883 327 493 423 159 743
# 217 623 3 399 853 407 103 983 89 463 290 516 212 462 350
# 960 376 682 962 300 780 486 502 912 800 250 346 172 812 350
# 870 456 192 162 593 473 915 45 989 873 823 965 425 329 803
# 973 965 905 919 133 673 665 235 509 613 673 815 165 992 326
# 322 148 972 962 286 255 941 541 265 323 925 281 601 95 973
# 445 721 11 525 473 65 511 164 138 672 18 428 154 448 848
# 414 456 310 312 798 104 566 520 302 248 694 976 430 392 198
# 184 829 373 181 631 101 969 613 840 740 778 458 284 760 390
# 821 461 843 513 17 901 711 993 293 157 274 94 192 156 574
# 34 124 4 878 450 476 712 914 838 669 875 299 823 329 699
# 815 559 813 459 522 788 168 586 966 232 308 833 251 631 107
# 813 883 451 509 615 77 281 613 459 205 380 274 302 35 805
# https://en.wikipedia.org/wiki/Hungarian_algorithm
m = [[ 7, 53, 183, 439, 863, 497, 383, 563, 79, 973, 287, 63, 343, 169, 583],
[627, 343, 773, 959, 943, 767, 473, 103, 699, 303, 957, 703, 583, 639, 913],
[447, 283, 463, 29, 23, 487, 463, 993, 119, 883, 327, 493, 423, 159, 743],
[217, 623, 3, 399, 853, 407, 103, 983, 89, 463, 290, 516, 212, 462, 350],
[960, 376, 682, 962, 300, 780, 486, 502, 912, 800, 250, 346, 172, 812, 350],
[870, 456, 192, 162, 593, 473, 915, 45, 989, 873, 823, 965, 425, 329, 803],
[973, 965, 905, 919, 133, 673, 665, 235, 509, 613, 673, 815, 165, 992, 326],
[322, 148, 972, 962, 286, 255, 941, 541, 265, 323, 925, 281, 601, 95, 973],
[445, 721, 11, 525, 473, 65, 511, 164, 138, 672, 18, 428, 154, 448, 848],
[414, 456, 310, 312, 798, 104, 566, 520, 302, 248, 694, 976, 430, 392, 198],
[184, 829, 373, 181, 631, 101, 969, 613, 840, 740, 778, 458, 284, 760, 390],
[821, 461, 843, 513, 17, 901, 711, 993, 293, 157, 274, 94, 192, 156, 574],
[ 34, 124, 4, 878, 450, 476, 712, 914, 838, 669, 875, 299, 823, 329, 699],
[815, 559, 813, 459, 522, 788, 168, 586, 966, 232, 308, 833, 251, 631, 107],
[813, 883, 451, 509, 615, 77, 281, 613, 459, 205, 380, 274, 302, 35, 805]]
m1 = [[ 7, 53, 183, 439, 863],
[497, 383, 563, 79, 973],
[287, 63, 343, 169, 583],
[627, 343, 773, 959, 943],
[767, 473, 103, 699, 303]]
# checks if solution was found(indexes of elements with 0 value in each row doesn't intersect)
def check_matrix(m)
indexes_by_rows = []
m.each_with_index do |row, i|
row.each_with_index do |el, j|
if m[i][j] == 0
indexes_by_rows[i] ||= []
indexes_by_rows[i] << j
end
end
end
(0..indexes_by_rows).each do |i|
(0..indexes_by_rows).each do |j|
end
end
indexes_by_rows
end
# step 1
# m1.each_with_index do |row, i|
# min = row.min
# row.each_with_index do |el, j|
# m1[i][j] = m1[i][j] - min
# end
# end
require 'pp'
pp m1
p '--'
columns_min = []
# step 2
(0..m1.size - 1).each do |i|
(0..m1.size - 1).each do |j|
columns_min[i] = m1[j][i] if columns_min[i] == nil || columns_min[i] > m1[j][i]
end
end
p columns_min
p '--'
(0..m1.size - 1).each do |i|
(0..m1.size - 1).each do |j|
m1[j][i] = m1[j][i] - columns_min[i]
end
end
pp m1
# p check_matrix(m1)