
% Update everything when the current queen position is (p, q)

eliminate(p, q) = big_number;
% vertical and horonzontal 
eliminate(p, 1:side) = big_number*ones(1, side);
eliminate(1:side, q) = big_number*ones(side, 1);
% 45-degree diagonal
index1 = p + linspace(-side, side, 2*side+1);
index2 = q + linspace(-side, side, 2*side+1);
common = abs(index1-(side+1)/2)<=(side-1)/2 & ...
	abs(index2-(side+1)/2)<=(side-1)/2;
index1 = index1(common);
index2 = index2(common);
eliminate(index1+(index2-1)*side) = big_number*ones(1, length(index1));
% -45-degree diagonal
index1 = p + linspace(-side, side, 2*side+1);
index2 = q - linspace(-side, side, 2*side+1);
common = abs(index1-(side+1)/2)<=(side-1)/2 & ...
	abs(index2-(side+1)/2)<=(side-1)/2;
index1 = index1(common);
index2 = index2(common);
eliminate(index1+(index2-1)*side) = big_number*ones(1, length(index1));

% show the territory of the queen at (p, q).
for i=1:side,
	for j=1:side,
		if ~strcmp(get(textH(i,j), 'string'), 'Q') & ...
			eliminate(i,j)==big_number,
			set(textH(i,j), 'string', num2str(100), 'color', 'r');
		end
	end
end

% update eliminate matrix 
[ii,jj] = find(eliminate~=big_number);
for k = 1:length(ii),
	p = ii(k);
	q = jj(k);
	% vertical and horizontal		
	count(1) = length(find(eliminate(p, 1:side)~=big_number));
	count(2) = length(find(eliminate(1:side, q)~=big_number));
	% 45-degree diagonal
	index1 = p + linspace(-side, side, 2*side+1);
	index2 = q + linspace(-side, side, 2*side+1);
	common = abs(index1-(side+1)/2)<=(side-1)/2 & ...
		abs(index2-(side+1)/2)<=(side-1)/2;
	index1 = index1(common);
	index2 = index2(common);
	count(3) = length(find(eliminate(index1+(index2-1)*side)~=big_number));
	% -45-degree diagonal
	index1 = p + linspace(-side, side, 2*side+1);
	index2 = q - linspace(-side, side, 2*side+1);
	common = abs(index1-(side+1)/2)<=(side-1)/2 & ...
		abs(index2-(side+1)/2)<=(side-1)/2;
	index1 = index1(common);
	index2 = index2(common);
	count(4) = length(find(eliminate(index1+(index2-1)*side)~=big_number));
	eliminate(p, q) = sum(count)-4+1;
	set(textH(p,q), 'string', num2str(eliminate(p,q)));
end
