Dancing links

来源:互联网 发布:ajax post提交json 编辑:程序博客网 时间:2024/05/16 01:06
constp:array[0..9,0..9] of integer=((0,0,0,0,0,0,0,0,0,0),(0,1,1,1,2,2,2,3,3,3),(0,1,1,1,2,2,2,3,3,3),(0,1,1,1,2,2,2,3,3,3),(0,4,4,4,5,5,5,6,6,6),(0,4,4,4,5,5,5,6,6,6),(0,4,4,4,5,5,5,6,6,6),(0,7,7,7,8,8,8,9,9,9),(0,7,7,7,8,8,8,9,9,9),(0,7,7,7,8,8,8,9,9,9));w:array[0..9,0..9] of integer=((0,0,0,0,0,0,0,0,0,0),(0,6,6,6,6,6,6,6,6,6),(0,6,7,7,7,7,7,7,7,6),(0,6,7,8,8,8,8,8,7,6),(0,6,7,8,9,9,9,8,7,6),(0,6,7,8,9,10,9,8,7,6),(0,6,7,8,9,9,9,8,7,6),(0,6,7,8,8,8,8,8,7,6),(0,6,7,7,7,7,7,7,7,6),(0,6,6,6,6,6,6,6,6,6));typelink=^node;node=recordi,j,k,p2:integer;up,down,left,right:link;end;varr,rt:array[1..9*9*9+1] of link;c,ct:array[1..9*9*4+1] of link;hash:array[1..9*9*4+1] of boolean;size:array[1..9*9*4+1]of integer;tmp,ans,i,j,k:integer;procedure add(i,j,k:integer);varpos,p2:integer;q:link;beginpos:=81*(i-1)+9*(j-1)+k;p2:=9*(i-1)+k;new(q);q^.i:=i; q^.j:=j; q^.k:=k; q^.p2:=p2;rt[pos]^.right:=q; q^.left:=rt[pos]; rt[pos]:=q;ct[p2]^.down:=q; q^.up:=ct[p2]; ct[p2]:=q;inc(size[p2]);p2:=9*9+9*(j-1)+k;new(q);q^.i:=i; q^.j:=j; q^.k:=k; q^.p2:=p2;rt[pos]^.right:=q; q^.left:=rt[pos]; rt[pos]:=q;ct[p2]^.down:=q; q^.up:=ct[p2]; ct[p2]:=q;inc(size[p2]);p2:=9*9*2+9*(p[i,j]-1)+k;new(q);q^.i:=i; q^.j:=j; q^.k:=k; q^.p2:=p2;rt[pos]^.right:=q; q^.left:=rt[pos]; rt[pos]:=q;ct[p2]^.down:=q; q^.up:=ct[p2]; ct[p2]:=q;inc(size[p2]);p2:=9*9*3+9*(i-1)+j;new(q);q^.i:=i; q^.j:=j; q^.k:=k; q^.p2:=p2;rt[pos]^.right:=q; q^.left:=rt[pos]; rt[pos]:=q;ct[p2]^.down:=q; q^.up:=ct[p2]; ct[p2]:=q;inc(size[p2]);end;procedure cover(pos:integer);var p,q:link;beginhash[pos]:=true;q:=c[pos]^.down;while q<>c[pos] dobeginp:=q^.right;while p<>q dobeginif (p^.p2>0) and not(hash[p^.p2]) thenbeginif p^.up<>nil then p^.up^.down:=p^.down; if p^.down<>nil then p^.down^.up:=p^.up; dec(size[p^.p2]);end;p:=p^.right;end;q:=q^.down;end;end;procedure recover(pos:integer);var p,q:link;beginq:=c[pos]^.up;while q<>c[pos] dobeginp:=q^.left;while p<>q dobeginif (p^.p2>0) and not(hash[p^.p2]) then beginif p^.up<>nil then p^.up^.down:=p; if p^.down<>nil then p^.down^.up:=p; inc(size[p^.p2]);end;p:=p^.left;end;q:=q^.up;end;hash[pos]:=false;end;procedure work(step,now:integer);varflag:boolean;i,min,pos:integer;p,q:link;beginflag:=false;pos:=-1; min:=32767;for i:=9*9*4 downto 1 dobeginif hash[i] then continue;if size[i]=0 then continue;if size[i]<min thenbeginmin:=size[i]; pos:=i;end;end;if pos=-1 thenbeginif step<81 then exit;if now>ans then ans:=now;exit;end;cover(pos);q:=c[pos]^.down;while q<>c[pos] dobeginp:=q^.right;while p<>q dobeginif p^.p2<>-1 then cover(p^.p2);p:=p^.right;end;work(step+1,now+q^.k*w[q^.i,q^.j]);p:=q^.left;while p<>q dobeginif p^.p2<>-1 then recover(p^.p2);p:=p^.left;end;q:=q^.down;end;recover(pos);end;beginfor i:=1 to 9*9*9 dobeginnew(r[i]);r[i]^.up:=nil; r[i]^.down:=nil; r[i]^.left:=nil; r[i]^.right:=nil; r[i]^.p2:=-1; rt[i]:=r[i];end;for i:=1 to 9*9*4 dobeginnew(c[i]);c[i]^.up:=nil; c[i]^.down:=nil; c[i]^.left:=nil; c[i]^.right:=nil; c[i]^.p2:=-1; ct[i]:=c[i];end;for i:=1 to 9 dobeginfor j:=1 to 9 dobeginread(tmp);if tmp<>0 then add(i,j,tmp)else for k:=1 to 9 do add(i,j,k); end;readln;end;for i:=1 to 9*9*9 dobeginrt[i]^.right:=r[i];r[i]^.left:=rt[i];end;for i:=1 to 9*9*4 dobeginct[i]^.down:=c[i];c[i]^.up:=ct[i];end;work(1,0);if ans=0 then writeln(-1)else writeln(ans);end.
NOIP2009提高组,靶形数独
0 0
原创粉丝点击