-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-colouring_pstamat.plg
46 lines (38 loc) · 1.36 KB
/
map-colouring_pstamat.plg
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
:- set_flag(print_depth, 1000).
:- lib(ic).
map_coloring(File, NColors, Colors) :-
read_input(File, Data),
length(Data, N),
length(Colors, N),
Colors #:: 1..NColors,
post_constraints(Data, Data, Colors, Colors),
search(Colors, 0, most_constrained, indomain, complete, []).
read_input(File, Lines) :-
open(File, read, S),
get_lines(S, Lines),
close(S).
get_lines(S, Lines) :-
read_string(S, end_of_line, _, Line) ->
split_string(Line, " \t", "", Words),
atoms_strings([Color, Country|Countries], Words),
Lines = [(Color, Country, Countries)|Ls],
get_lines(S, Ls)
;
Lines = [].
atoms_strings([], []).
atoms_strings([Atom|Atoms], [String|Strings]) :-
atom_string(Atom, String),
atoms_strings(Atoms, Strings).
post_constraints([], _, [], _).
post_constraints([(_, _, Countries)|RestCoun], Data, [Color|RestCol], Colors) :-
post_cons(Countries, Color, Data, Colors),
post_constraints(RestCoun, Data, RestCol, Colors).
post_cons([], _, _, _).
post_cons([Country|Countries], Color, Data, Colors) :-
post_one(Country, Color, Data, Colors),
post_cons(Countries, Color, Data, Colors).
post_one(Country, Color1, [(_, Country, _)|_], [Color2|_]):-
Color1 #\= Color2.
post_one(Country1, Color1, [(_, Country2, _)|Countries], [_|Colors]):-
Country1 \= Country2,
post_one(Country1, Color1, Countries, Colors).