-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiebreak.m
55 lines (45 loc) · 1.63 KB
/
tiebreak.m
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
weight_fix = round(weight);
temp_fillmap = vec_fillmap.' * weight_fix;
temp_fillmap = temp_fillmap + ~fill_without_num_1(:);
empty_region = bwlabel(reshape(~temp_fillmap,[total_row,total_col]),4);
vec_empty_region = empty_region(:).';
for ii = 1:max(vec_empty_region)
[empty_region_row, empty_region_col] = find(empty_region == ii);
empty_id = sort(find(any((puzzle_row == empty_region_row.') & (puzzle_col == empty_region_col.'),2)));
if isempty(empty_id)
continue
end
match_pos = (vec_empty_region == ii);
waitlist = all(match_pos - vec_fillmap >= 0, 2);
%
waitlist(weight<1e-3) = 0;
ind = find(waitlist);
temp_min_value = sum(match_pos);
temp_ind_best = [];
for jj = 1:max_iter
temp_match_pos = match_pos;
temp_ind = ind(randperm(length(ind)));
temp_ind_selected = [];
for kk = 1:length(temp_ind)
if all(temp_match_pos - vec_fillmap(temp_ind(kk),:) >=0)
temp_match_pos = temp_match_pos - vec_fillmap(temp_ind(kk),:);
temp_ind_selected = [temp_ind_selected temp_ind(kk)];%#ok
end
if all(temp_match_pos == 0)
break
end
end
temp_value = sum(temp_match_pos);
if temp_value < temp_min_value
temp_min_value = temp_value;
temp_ind_best = temp_ind_selected;
end
if temp_min_value == 0
weight_fix(temp_ind_best) = 1;
break
elseif jj == max_iter
weight_fix(temp_ind_best) = 1;
end
end
end
weight_fix = (weight_fix == 1);