5 typedef struct Neighbour {
6 struct Neighbour *next;
11 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
12 static sqlite3_stmt *ss_neigh;
14 static int ports[MAX_ROUTELEN];
16 static Neighbour *get_neighbours(int isle) {
17 Neighbour **np= &neighbours[isle];
19 if (head) return head;
21 SQL_BIND(ss_neigh, 1, isle);
22 while (SQL_STEP(ss_neigh)) {
23 Neighbour *add= mmalloc(sizeof(*add));
24 add->islandid= sqlite3_column_int(ss_neigh, 0);
25 add->dist= sqlite3_column_int(ss_neigh, 1);
29 SQL_MUST( sqlite3_reset(ss_neigh) );
35 static double best_absolute, best_perleague;
37 static void process_route(int nports, int totaldist) {
38 double absolute= value_route(nports, ports);
39 double perleague= absolute / totaldist;
41 if (absolute < best_absolute && perleague < best_perleague) return;
43 #define CHK(absperl) \
44 fprintf(stderr,#absperl " %15f", absperl); \
45 if (absperl < best_##absperl) fputs(" ",stderr); \
46 else { best_##absperl= absperl; fputs("** ",stderr); }
51 fputs(" route",stderr);
54 for (i=0; i<nports; i++)
55 fprintf(stderr," %d",ports[i]);
59 static void recurse(int last_isle,
60 int nports, /* excluding last_isle */
61 int totaldist /* including last_isle */) {
62 ports[nports++]= last_isle;
63 process_route(nports, totaldist);
64 if (nports >= MAX_ROUTELEN) return;
67 for (add= get_neighbours(last_isle); add; add=add->next) {
68 int newdist= totaldist + add->dist;
69 if (newdist > max_dist) continue;
71 recurse(add->islandid, nports, newdist);
75 void search(int start_isle) {
76 recurse(start_isle,0,0);
79 void setup_search(void) {
80 neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
83 "SELECT biid, dist FROM routes WHERE aiid=?");