ID: 
Title:typ2when will I stop????
Author:utrStijn Helsen
Date:2006-04-12 16:51:302006-04-12 16:53:47
Score:840.2677846.5259
Result:8369184360
CPU Time:64.952163.8509
Status:PassedPassed
Code:
- function m=solver(tb,nM) - [m,b]=l0(tb,nM,1);st=sum(sum(b));if(st > 800) - [m2,b]=l0(tb,nM,0.9);st2=sum(sum(b));if st2<st - m=m2;end;end - function [l1,l2]=l0(tl2,nl1,z) - l2=zeros(size(tl2,1)+1,size(tl2,2)+2);l2(2:end,2:end-1)=tl2;[l3,l4]=size(l2);l1=ones(nl1,3);l5=0; - zz=zeros(1,floor(((l3-1)*(l4-2))/2));i=1;for r=l3:-1:2 - for cl=l4-2:-1:1 - if mod(r+cl,2) - zz(i)=(cl*l3)+r;i=i+1;end;end;end - l6=zeros(1,900);l7=zeros(1,900);l8=zeros(1,400);l9=zeros(numel(l2),1); - if (l3*l4)>10.5*nl1 - I0=40;I1=.817;I2=2.93;I3=1.566;elseif (l3*l4)>8.5*nl1 - I0=29;I1=.817;I2=2.93;I3=1.566;elseif l3*l4>=256 - I0=26;I1=.951;I2=2.8;I3=1.4;else - I0=26;I1=.945;I2=2.97;I3=1.46;end - I1=I1*z;l2=a37(l2,I3);I4=ceil(I2*nl1/10);I5=floor(0.66*nl1);while 1 - if l5==I5,l2=a37(l2,2/(1+I3));end - [I6,I7,I8] = I9(l2);if isempty(I6),[O0,l2]=O1(l2); - if O0(1) - l5=l5+1;l1(l5,:)=O0;if(l5==nl1) - break;end - continue - end - break - end - if l5<nl1-1 - [I7,O2]=sort(I7,2,'descend');I6=I6(O2);for k=1:min(numel(I6),I0) - O3=O4(l2,I8,I6(k));[O5,O6]=I9(O3);if (~isempty(O5)) - I4_=max(min(I4,nl1-l5-3),1);if numel(O6)<=I4_ - I7(k)=I7(k)+sum(O6);else - O6=sort(O6,2,'descend');I7(k)=I7(k)+sum(O6(1:I4_));end - end - if (I7(k)<I1*I7(1)) - break;end - end - end - [O7,O2]=max(I7);O8=I6(O2);l5=l5+1;l1(l5,:)=[mod(O8-1,l3),ceil(O8/l3)-1,0];l2=O4(l2,I8,O8); - if (l5==nl1) - break; - end - end - l1=l1(1:l5,:); - function [O5,O6,O9]=I9(l2); - [l3,l4]=size(l2);L0=0;ii=0;L1=0;L2=0;L3=0;c=0; - for zi=1:numel(zz) - i=zz(zi);c=l2(i);if c && (l2(i+1)==c||l2(i+l3)==c||l2(i-1)==c||l2(i-l3)==c) - L0=L0+1;l8(1)=i;l2(i)=0;L1=i;L2=0;L3=1;while (L3-L2) - L2=L2+1;ii=l8(L2);l9(ii)=L1;L1=ii;if l2(ii+1)==c - L3=L3+1;l8(L3)=ii+1;l2(ii+1)=0;end - if l2(ii-1)==c - L3=L3+1;l8(L3)=ii-1;l2(ii-1)=0;end - if l2(ii+l3)==c - L3=L3+1;l8(L3)=ii+l3;l2(ii+l3)=0;end - if l2(ii-l3)==c - L3=L3+1;l8(L3)=ii-l3;l2(ii-l3)=0;end - end - l7(L0)=c*L2;l6(L0)=ii;end - end - O5=l6(1:L0);O6=l7(1:L0);O9=l9;end - function B=O4(B,dj,y) - B(y)=0;L4=ceil(y/l3);L5=L4;L6=L4;while dj(y)~=y - y=dj(y);B(y)=0;L4=ceil(y/l3);L5=min(L5, L4);L6=max(L6, L4);end; - for ip=L5:L6, - kp=B(B(:,ip)>0,ip);B(:,ip)=0;B(l3-numel(kp)+1:l3,ip)=kp;end - end - function [O0,L8]=O1(l2) - [l3 l4]=size(l2);L7=[-1 l3 1 -l3];L9=l3*l4;O0=zeros(1,3);L8=l2;Q0=0;Q1=find(l2~=0)';Q2=numel(Q1); - for kc=1:Q2 - jj=Q1(kc);for Q3=1:4 - np=jj+L7(Q3);if np>0 && np<=L9 && l2(np)>0 && l2(np)~=l2(jj) - tl2=Q4(l2,jj,np);[Q5,Q6]=I9(tl2);if (~isempty(Q6)) - if (max(Q6)> Q0) - Q0=max(Q6);Q7=jj;Q8=Q3;Q9=np;end + function moves=solver(board,nMoves) + [rows, cols] = size(board); + moves = ones(nMoves,3); + count=0; + r=rand(4,1); + + delta = -0.0001; + % set move calculation parameters + if (rows*cols) > 10.5*nMoves + next_move_count = 40; + choke_factor = .847+delta; + depth_factor = 2.93; + rot = 1.566; + elseif (rows*cols) > 8.5*nMoves + next_move_count = 29; + choke_factor = .847+delta; + depth_factor = 2.93; + rot = 1.566; + elseif rows*cols >= 256 + next_move_count = 26; + choke_factor = .951+delta; + depth_factor = 2.8; + rot = 1.4; else - for k2=max(1,kc-20):min(Q2,kc+20) - W0=Q1(k2);for W1=1:4 - np2=W0+L7(W1);if np2>0 && np2<=L9 && tl2(np2)>0 && tl2(np2)~=tl2(W0) - W2=Q4(tl2,W0,np2);[W3,W4,W5]=I9(W2);if (~isempty(W4) && max(W4)>Q0) - W6=false;[W7,W8]=max(W4);Y=W3(W8); - while (W5(Y)~=Y) - Y = W5(Y);if (Y==np || Y==jj) - W6=true;end + next_move_count = 26; + choke_factor = 0.945+delta; + depth_factor = 2.97; + rot = 1.46; end - if (Y==np || Y==jj) - W6=true; + + % adjust block weights + board = nthroot(board,rot); + + depth = ceil(depth_factor*nMoves/10); + flip = floor((0.66+(rand-0.5)*0.1)*nMoves); + while 1 + % restore original colour values half-way through game + if count==flip, board = nthroot(board,2/(1+rot)); end + + % find highest value moves + [cell_list,value_list,mask] = CalculateMoves(rows,cols,board); + if isempty(cell_list), + [swapmv,board]=CheckForSwap(rows,cols,board); + if swapmv(1) + count = count+1; + moves(count,:) = swapmv; + if (count==nMoves) + break; + end + continue + end + break + end + + % check future move values and re-weight current moves + if count<nMoves-1 + [value_list,pos]=sort(value_list,2,'descend'); + cell_list=cell_list(pos); + for k=1:min(numel(cell_list),next_move_count) + new_board = ProcessMove(board,mask,cell_list(k),rows); + [new_cell_list,new_value_list] = CalculateMoves(rows,cols,new_board); + + if (~isempty(new_cell_list)) + depth_ = max(min(depth,nMoves-count-3),1); + if numel(new_value_list)<=depth_ + value_list(k) = value_list(k) + sum(new_value_list); + else + new_value_list=sort(new_value_list,2,'descend'); + value_list(k) = value_list(k) + sum(new_value_list(1:depth_)); + end + end + if (value_list(k) < choke_factor * value_list(1)) + break; + end + end + end + + % take new most-valuable move + [max_val,pos] = max(value_list); + max_cell = cell_list(pos); + count = count+1; + moves(count,1) = mod(max_cell-1,rows)+1; + moves(count,2) = ceil(max_cell/rows); + moves(count,3) = 0; + if (count==nMoves) + break; + end + board = ProcessMove(board,mask,max_cell,rows); + end - if (W6) - Q0=max(W4);Q7=jj;Q8=Q3;Q9=np; + + function [cell_list,value_list,mask] = CalculateMoves(rows,cols,board) + + mask = zeros(rows,cols); + + % find first row from top that has non-zeros + first_row = 1;% find(any(board,2),1); + + if (first_row == 1) + % first row is non-zero so build column weighted match value + bk=board(1); + if bk + mask(1)=1; + end + k=1; + for j=2:cols + m = k; + k = k + rows; + bm=bk; + bk=board(k); + if bk + mask(k) = k; + if (bm == bk) + % block to left matches current + mask(m) = k; + end + end + end end + + % Start at first row (or second if first is top and loop to bottom) + for i = max(2,first_row):rows + k=i; + bk=board(k); + if (bk) + % first block in row is non-zero + mask(k) = k; + m = k-1; + if (board(m) == bk) + % block above matches current + n = -1; + while (n ~= m) + n = m; + m = mask(n); + mask(n) = k; + end + end + end + + % loop through columns and build column+row weighted match value + for j=2:cols + % m = k; + k = k + rows; % move over one column + bm=bk; + bk=board(k); + if (bk) + mask(k) = k; + if (bm==bk) + % block to left matches current + mask(k-rows) = k; + end + if (board(k-1)==bk) + % block above matches current + n = -1; + m = k - 1; + while (n ~= m) + n = m; + m = mask(n); + mask(n) = k; + end + end + end + end end + + cell_list = zeros(1,256); + group_count = 0; + group_count_matrix = zeros(rows, cols); + + for i=rows:-1:first_row + k = i + rows*(cols-1); + for j=1:cols + if board(k) + m = mask(k); + if (m == k) + group_count_matrix(k) = 1; + else + while group_count_matrix(m) == 0 + m = mask(m); + end + mask(k) = mask(m); + group_block_count = group_count_matrix(m) + 1; + group_count_matrix(m) = group_block_count; + if (group_block_count == 2) + group_count = group_count + 1; + cell_list(1,group_count) = m; + end + mask(m)= k; + end + end + k = k - rows; + end end + + cell_list = cell_list(1:group_count); + value_list = zeros(1,group_count); + + for k = 1:group_count + m = cell_list(k); + cell_list(k) = mask(m); + value_list(k) = group_count_matrix(m) * board(m); + mask(m) = m; end + + function B=ProcessMove(B,dj,cell,R) + y=cell; + B(y)=0; + col=ceil(y/R); + minCol=col;maxCol=col; + while dj(y)~=y + y=dj(y); + B(y)=0; + col=ceil(y/R); + minCol=min(minCol, col); + maxCol=max(maxCol, col); + end; + for i=minCol:maxCol, + k=B(B(:,i)>0,i); + B(:,i)=0;B(R-numel(k)+1:R,i)=k; end + + function [swapmv,outboard]=CheckForSwap(rows,cols,board) + swapper=[-1 rows 1 -rows]; + lentmp=rows*cols; + swapmv=zeros(1,3); + outboard=board; + maxswap=0; + list=find(board~=0)'; + nList=numel(list); + for k=1:nList + jj=list(k); + for swapdir=1:4 + np=jj+swapper(swapdir); + if np>0 && np<=lentmp && board(np)>0 && board(np)~=board(jj) + tboard=board;tboard([jj,np]) = tboard([np,jj]); + [s_ceil,s_value]=CalculateMoves(rows,cols,tboard); + if (~isempty(s_value)) + if (max(s_value)> maxswap) + maxswap=max(s_value); + idxq=jj;idxr=swapdir;idxs=np; + end + else + for k2=max(1,k-10):min(nList,k+10) + jj2=list(k2); + for swapdir2=1:4 + np2=jj2+swapper(swapdir2); + if np2>0 && np2<=lentmp && tboard(np2)>0 && tboard(np2)~=tboard(jj2) + tboard2=tboard;tboard2([jj2,np2]) = tboard2([np2,jj2]); + [s_ceil2,s_value2,s_mask2]=CalculateMoves(rows,cols,tboard2); + if (~isempty(s_value2) && max(s_value2)>maxswap) + [s_max,s_idx]=max(s_value2); + Y=s_ceil2(s_idx); + while (s_mask2(Y)~=Y) + Y = s_mask2(Y); + end + if (Y==np || Y==jj) + maxswap=max(s_value2); + idxq=jj;idxr=swapdir;idxs=np; + end + end + end + end + end + end + end + end end + if (maxswap>0) + swapmv = [mod(idxq-1,rows)+1,ceil(idxq/rows),idxr]; + outboard=board;outboard([idxq,idxs]) = tboard([idxs,idxq]); end - end + - end + function board = DoSwap(board,el1,el2) - if (Q0>0) + board([el1,el2]) = board([el2,el1]);- O0 = [mod(Q7-1,l3),ceil(Q7/l3)-1,Q8];L8=Q4(l2,Q7,Q9); - end - end - function l2 = Q4(l2,el1,el2) - l2([el1,el2]) = l2([el2,el1]); - end - end - end - function y = a37(x, n) - y =(x.^(1/n)); - end