// Snake and Hunter (MO 244949): exact game value on a W x H grid. (C source; save as snake.c) // Snake (maximizer) and hunter (minimizer) alternate extending a self-avoiding path // at either end; snake makes the first edge. Value = final number of edges under optimal play. // Method: for threshold T decide "snake can force length >= T" by DFS with transposition table; // prune with length>=T (win) and an upper bound from the free region reachable from the ends. // Build: gcc -O2 -o snake snake.c ; run: ./snake 6 6 (prints "6x6 value 31" in ~20 s) #include #include #include #include typedef uint64_t u64; static int W,H,N; static u64 nb[64]; static u64 FULL; #define TBITS 26 static u64 *tk; static uint8_t *tv; // key, value: 1=win,2=loss (for current T) static long nodes; static u64 flood(u64 seed,u64 freeb){ u64 r=seed,prev=0; while(r!=prev){prev=r; u64 x=r; u64 add=0; while(x){int i=__builtin_ctzll(x); x&=x-1; add|=nb[i];} r|=add&freeb;} return r; } static int T; static int solve(u64 mask,int u,int v){ // mask = visited vertices; snake length = popcount-1 int len=__builtin_popcountll(mask)-1; if(len>=T) return 1; u64 freeb=FULL&~mask; u64 opts=(nb[u]|nb[v])&freeb; if(!opts) return 0; u64 reach=flood(opts,freeb); if(len+__builtin_popcountll(reach)>(64-TBITS); for(int p=0;p<4;p++){ u64 s=(h+p)&((1ULL<0)m|=1ULL<<(i-1); if(x+10)m|=1ULL<<(i-W); if(y+1 %s (%ld nodes)\n",W,H,t,r?"yes":"no",nodes); if(r) best=t; else break; } printf("%dx%d value %d\n",W,H,best); return 0; }