+/**/
+
+#include "rscommon.h"
+
+typedef struct Neighbour {
+ struct Neighbour *next;
+ int islandid;
+ int dist;
+} Neighbour;
+
+static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
+static sqlite3_stmt *ss_neigh;
+
+static int ports[MAX_ROUTELEN];
+
+static Neighbour *get_neighbours(int isle) {
+ Neighbour **np= &neighbours[isle];
+ Neighbour *head= *np;
+ if (head) return head;
+
+ SQL_BIND(ss_neigh, 1, isle);
+ while (SQL_STEP(ss_neigh)) {
+ Neighbour *add= mmalloc(sizeof(*add));
+ add->islandid= sqlite3_column_int(ss_neigh, 0);
+ add->dist= sqlite3_column_int(ss_neigh, 1);
+ add->next= head;
+ head= add;
+ }
+ SQL_MUST( sqlite3_reset(ss_neigh) );
+
+ *np= head;
+ return head;
+}
+
+static double bestsofar;
+
+static void process_route(int nports) {
+ double value= value_route(nports, ports);
+ if (value < bestsofar) return;
+
+ fprintf(stderr,"value %20f route", value);
+ int i;
+ for (i=0; i<nports; i++)
+ fprintf(stderr," %d",ports[i]);
+ putc('\n',stderr);
+
+ bestsofar= value;
+}
+
+static void recurse(int last_isle,
+ int nports, /* excluding last_isle */
+ int totaldist /* including last_isle */) {
+ ports[nports++]= last_isle;
+ process_route(nports);
+ if (nports >= MAX_ROUTELEN) return;
+
+ Neighbour *add;
+ for (add= get_neighbours(last_isle); add; add=add->next) {
+ int newdist= totaldist + add->dist;
+ if (newdist > max_dist) continue;
+
+ recurse(add->islandid, nports, newdist);
+ }
+}
+
+void search(int start_isle) {
+ recurse(start_isle,0,0);
+}
+
+void setup_search(void) {
+ neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
+
+ SQL_PREPARE(ss_neigh,
+ "SELECT biid, dist FROM routes WHERE aiid=?");
+}